[gdal-dev] OGR behaves differently after matplotlib call

Amaury Dehecq v1adehec at staffmail.ed.ac.uk
Mon Aug 10 05:46:24 PDT 2015


Hi Evan,

thank you very much, this is indeed a problem related to locale.
Adding the 2 lines you recommended solved the problem. I looked for a 
more permanent way to solve this problem and found this post : 
http://stefaanlippens.net/pylabllocaleproblem

A way to fix the problem more permanently is to set the environment 
variable LC_NUMERIC to C :
 > export LC_NUMERIC="C"
for example in the .bashrc.
It solves the problem for this script, now I hope it won't create other 
problems!

Amaury





Le 10/08/2015 14:18, Even Rouault a écrit :
> Le lundi 10 août 2015 12:27:49, Amaury Dehecq a écrit :
>> Hi all,
>>
>> I noticed that osgeo.ogr.Feature.GetField behaves differently when a
>> matplotlib function is called. In particular, fields of type "Real"
>> don't return the same value when called before or after the use of
>> matplotlib functions. See for example the script below that extract the
>> fields values from an ESRI shapefile :
>>
>> """
>> import matplotlib.pyplot as plt
>> from osgeo import ogr
>>
>> #Read ESRI shapefile
>> filename='/disk/L0data/GLIMS/RGI_5.0/all_asia.shp'
>> ds = ogr.Open(filename,0)
>> layer = ds.GetLayer()
>>
>> #Comment/uncomment this line
>> #plt.figure()
>> #plt.show()
>>
>> #Extract field number 5 information (central longitude)
>> index=5
>> layerDefinition = layer.GetLayerDefn()
>> name= layerDefinition.GetFieldDefn(index).GetName()
>> print "Name : %s " %name
>> fieldTypeCode = layerDefinition.GetFieldDefn(index).GetType()
>> print "Type : %s"
>> %layerDefinition.GetFieldDefn(index).GetFieldTypeName(fieldTypeCode)
>> print "Width : %i" %layerDefinition.GetFieldDefn(index).GetWidth()
>>
>> #Extract feature field values
>> for i in xrange(5):
>>       feat = layer.GetNextFeature()
>>       print feat.GetField(name)
>> """
>>
>> This script returns :
>>
>> /Name : CenLon //
>> //Type : Real//
>> //Width : 14//
>> //79.4939//
>> //77.9513//
>> //77.9295//
>> //77.9237//
>> //77.9141/
>>
>>
>> But when the 2 matplotlib lines are uncommented, it returns :
>>
>> /Name : CenLon //
>> //Type : Real//
>> //Width : 14//
>> //79.0//
>> //77.0//
>> //77.0//
>> //77.0//
>> //77.0//
>> /
>> It seems that the values are truncated. Has anyone observed the same
>> problem? Are there any incompatibilities between matplotlib and OGR?
> Amaury,
>
> This looks like a locale related problem where matplotlib would install a non
> C locale with a decimal separator that isn't point. There have been a lot of
> work done in GDAL 2.0 to be immune to the locale. However the shapefile driver
> in reading should have been immune to non C locales for even a longer time, so
> I'm not sure why you see this.
>
> As a potential workaround you could try to do the following:
>
> import locale
> locale.setlocale(locale.LC_ALL, 'C')
>
> just after the matplotlib calls.
>
> Even
>


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



More information about the gdal-dev mailing list