Problems with format guessing when writing Shapefile/GeoJSON
See original GitHub issueI am sorry in advance, because this is dumb, but it is a bug.
If I have a shapefile of name foo.shp
(plus auxiliary files) and try to create a GeoJSON file named foo.shp
(because I forgot to update the filename), I will get a Shapefile named OgrGeoJSON.shp
(plus aux. files) instead. Here is a test script:
import fiona
from fiona.crs import from_epsg
import glob
outdir = "/tmp"
file = "O" # "O" so that we will see the OgrGeoJSON files in globbing
p = {
'geometry': {'coordinates': (370262, 5615696), 'type': 'Point'},
'id': '0', 'properties': {}, 'type': 'Feature'
}
schema = {'geometry': 'Point', 'properties': {}}
with fiona.open(f"{outdir}/{file}.shp", "w", crs=from_epsg(25832), schema=schema, driver="ESRI Shapefile") as sink:
sink.write(p)
print(glob.glob(f"{outdir}/{file}*"))
# -> ['/tmp/O.prj', '/tmp/O.cpg', '/tmp/O.dbf', '/tmp/O.shx', '/tmp/O.shp']
# here we have .shp but driver="GeoJSON"
with fiona.open(f"{outdir}/{file}.shp", "w", crs=from_epsg(25832), schema=schema, driver="GeoJSON") as sink:
sink.write(p)
print(glob.glob(f"{outdir}/{file}*"))
# -> ['/tmp/OgrGeoJSON.prj', '/tmp/OgrGeoJSON.cpg', '/tmp/OgrGeoJSON.dbf', '/tmp/OgrGeoJSON.shx', '/tmp/OgrGeoJSON.shp', '/tmp/O.prj', '/tmp/O.cpg', '/tmp/O.dbf', '/tmp/O.shx', '/tmp/O.shp']
I would have expected either an error or the foo.shp file being overwritten (aux. files left in place for extra confusion because screw the Shapefile format clutter).
The other way around is not much better as Fiona now tries to overwrite the existing foo.geojson with another GeoJSON even though ESRI Shapefile was set as the driver.
import fiona
from fiona.crs import from_epsg
import glob
outdir = "/tmp"
file = "O" # "O" so that we will see the OgrGeoJSON files in globbing
p = {
'geometry': {'coordinates': (370262, 5615696), 'type': 'Point'},
'id': '0', 'properties': {}, 'type': 'Feature'
}
schema = {'geometry': 'Point', 'properties': {}}
with fiona.open(f"{outdir}/{file}.geojson", "w", crs=from_epsg(25832), schema=schema, driver="GeoJSON") as sink:
sink.write(p)
print(glob.glob(f"{outdir}/{file}*"))
# -> ['/tmp/O.geojson']
# here we have .geojson but driver="ESRI Shapefile"
with fiona.open(f"{outdir}/{file}.geojson", "w", crs=from_epsg(25832), schema=schema, driver="ESRI Shapefile") as sink:
sink.write(p)
print(glob.glob(f"{outdir}/{file}*"))
# -> DriverIOError: GeoJSON driver doesn't support creating a layer on a read-only datasource
To me, the driver parameter should have precedence as it is such an explicite declaration of the user.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Common Writing Problems & How to Fix Them
Here are brief descriptions of common writing problems. Check them and email any questions ... Example of choppy effects: "This is the ultimate...
Read more >It it normal to second guess your writing? - Reddit
I'm actually writing my second draft as I write my first. I'll write 10 chapter of my first draft, wait a while, and...
Read more >Why Children With Dyslexia Struggle With Writing and How to ...
Dyslexia and writing difficulties co-occur for two overarching reasons. First, reading and writing rely on related underlying processes (Graham & Hebert, 2010, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m going to close this one as we are many GDAL versions on. We can reopen or create a new issue if needed.
Sure thing! If someone runs into it again they will hopefully find the issue and shout.