-
I have a bunch of Shapefiles which I've read with Fiona. I've then converted each feature to a Shapely shape, run any CRS conversion that's necessary (ending in WGS84/EPSG:4326) and done some other cleanup (like holes under certain conditions). I have then serialized the features to JSON using My goal is to make GeoJSON files which are the same as the files that would be generated using QGIS. The idea is that I should be able to open one of these GeoJSON files in QGIS, make a change (like adjusting the value of a feature's property, modifying a feature, or even adding a new feature) and then save the file. The new file should then be diffable in Git and all that should show up as having changed is the line(s) representing the feature(s) that were modified. The first hurdle to resolving this was that QGIS has some ideosyncratic whitespace rules. I've written a small (75 line) QGISGeoJSONEncoder to resolve this, and that part works well. But there's a second hurdle: QGIS always seems to choose a different level of precision (more significant figures) when saving features back to the GeoJSON file. This means that nearly every point in the file changes, and thus nearly every line. I'm not sure at what point in the process I should look to address this (Fiona, Shapely, JSON encoding). I'm ultimately hoping to allow anyone to submit GeoJSON files to this repo, so I'd really prefer to have the Thanks in advance for any suggestions you may have! ExampleShapely-generated points:
After saving, these are the QGIS-generated points:
I've added carets to the added/deleted numerals in both versions, as noticed by git diff. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm not sure if it does for geojson, but QGIS uses GDAL/OGR for many file formats to write them to file. If you use |
Beta Was this translation helpful? Give feedback.
Indeed! When I write the file with Fiona, making a change to a single feature in QGIS and then saving only changes the line of the feature I modified. This is also just way less fragile than the GeoJSON Encoder I wrote. Easier and more robust, love it! Thanks a bunch.
For anyone who wants to do this, just follow the Writing new files from scratch section in the Fiona docs. Note that if you want to write several features to a single file (all of which need to be the same type), you need to use
writerecords
instead of justwrite
. Also, I had to manually provide the geometry type in the schema, instead of usingshapely.mapping()
. This is because my shapely objects, which were converted from …