[gdal-dev] OGR behaves differently after matplotlib call
Even Rouault
even.rouault at spatialys.com
Mon Aug 10 05:18:57 PDT 2015
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
--
Spatialys - Geospatial professional services
http://www.spatialys.com
More information about the gdal-dev
mailing list