question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems with format guessing when writing Shapefile/GeoJSON

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sgilliescommented, May 18, 2022

I’m going to close this one as we are many GDAL versions on. We can reopen or create a new issue if needed.

0reactions
kannescommented, May 18, 2022

Sure thing! If someone runs into it again they will hopefully find the issue and shout.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found