[gdal-dev] OGR ExportToJson behavior & shapefile difference

Rutger kassies at gmail.com
Thu Apr 11 05:16:59 PDT 2013


Dear list,

I am trying to make a Python script which gives me the difference between
two Shapefiles. With different i mean any change in attributes or geometry,
but ignoring the FID. I have a working solution which i'll describe below.
On the way i discovered some inconsistent behavior in the ExportToJson()
function, perhaps a bug, im not sure. 

My method exists of loading all features in a Python set() and then using
new_set.difference(old_set) to get the difference. 

I dump each feature with ExportToJson, convert it to a dict, pop-out the
FID, and convert it back to json (because you cant add a dict in a set). So:

/tmpdct = json.loads(ft.ExportToJson())
ftid = tmpdct.pop('id')
old_set.add(json.dumps(tmpdct))/

This works well, but the strange thing is that the Json-code for the first
feature (with FID=0) doesnt contain the 'id', all further features do. Is
this behavior to be expected or should i file a ticket? I made a gist with
some sample code replicating the issue:
http://nbviewer.ipython.org/5362782 <http://nbviewer.ipython.org/5362782>  

The json export for the features would looks like this:
/{"geometry": null, "type": "Feature", "properties": {"Name": "Test1"}}
{"geometry": null, "type": "Feature", "properties": {"Name": "Test2"}, "id":
1}
{"geometry": null, "type": "Feature", "properties": {"Name": "Test3"}, "id":
2}
etc.../

The behavior is still there if you add a geometry, i just left the geom out
for convenience.

The resulting 'difference set' can then be used to find and copy changed
features to a new shapefile. Alltough it works well, all the converting to
json and dicts seems far from ideal to me, so i'd be glad to hear if anyone
has a better workflow to get this done.

Regards,
Rutger






--
View this message in context: http://osgeo-org.1560.x6.nabble.com/OGR-ExportToJson-behavior-shapefile-difference-tp5046049.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list