Toblerity / Fiona

Fiona reads and writes geographic data files
https://fiona.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.15k stars 202 forks source link

crs not appearing on first read of a file #762

Closed micahcochran closed 5 years ago

micahcochran commented 5 years ago

Expected behavior and actual behavior.

crs is empty when reading a file (for the first time). If file is opened to read a 2nd time, the crs appears normally.

Steps to reproduce the problem.

In [1]: import fiona

In [2]: amb = fiona.open('amb.shp')

In [3]: amb.crs
Out[3]: {}

In [4]: amb = fiona.open('amb.shp')

In [5]: amb.crs
Out[5]:
{'proj': 'tmerc',  'lat_0': 30,  'lon_0': -87.5,  'k': 0.9999333333333333,  'x_0': 600000,  'y_0': 0,
 'datum': 'NAD83', 'units': 'us-ft', 'no_defs': True}

Note: I also tried it with the preferred with fiona.open syntax and the problem is unchanged by that.

amb.zip shapefile

Also, strangely enough the crs_wkt changes when the file is reopened. I'm just reporting strange (potentially related) behavior. crs_wkt is not something that I necessarily care if it is fixed.

In [2]: amb = fiona.open('amb.shp')

In [3]: amb.crs_wkt
Out[3]: 'PROJCS["NAD_1983_StatePlane_Alabama_West_FIPS_0102_Feet",GEOGCS["GCS_No
rth_American_1983",DATUM["North_American_Datum_1983",SPHEROID["GRS_1980",6378137
.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PR
OJECTION["Transverse_Mercator"],PARAMETER["False_Easting",1968500.0],PARAMETER["
False_Northing",0.0],PARAMETER["Central_Meridian",-87.5],PARAMETER["Scale_Factor
",0.9999333333333333],PARAMETER["Latitude_Of_Origin",30.0],UNIT["Foot_US",0.3048
006096012192],AUTHORITY["Esri","102630"]]'

In [5]: amb = fiona.open('amb.shp')

In [7]: amb.crs_wkt
Out[7]: 'PROJCS["NAD_1983_StatePlane_Alabama_West_FIPS_0102_Feet",GEOGCS["GCS_No
rth_American_1983",DATUM["North_American_Datum_1983",SPHEROID["GRS_1980",6378137
.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433],AUT
HORITY["EPSG","4269"]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Eastin
g",1968500.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-87.5
],PARAMETER["Scale_Factor",0.9999333333333333],PARAMETER["Latitude_Of_Origin",30
.0],UNIT["Foot_US",0.3048006096012192]]'

When the file is reopened, crs_wkt appears to be the same as the contents of the .PRJ file.

Operating system

Windows 7

Fiona and GDAL version and provenance

Fiona 1.8.6 installed via binaries from https://www.lfd.uci.edu/~gohlke/pythonlibs/#fiona.

GDAL 2.4.1 installed via OSGeo4W

Note: I tested this on an older setup on Linux (using Fiona 1.7.7 and GDAL 1.11.3), when that was tested it gave the same results for crs and crs_wkt when opened and reopened. This was discovered when updating an old ETL process script that uses geopandas.

cgohlke commented 5 years ago

FWIW, I can reproduce this issue.

micahcochran commented 5 years ago

I've retesting this on a Linux box and the results are the same.

Fiona 1.8.4 works fine, but in Fiona 1.8.5 and above exhibit the same crs issue. (And the crs_wkt behavior also occurs.)

I've made it into a script that is easier to run on the command line instead of the console:

import fiona

with fiona.open('amb.shp') as amb:
    print('CRS {}'.format(amb.crs))

with fiona.open('amb.shp') as amb:
    print('CRS {}'.format(amb.crs))

Setup


Ubuntu Linux 18.04 GDAL version 2.3.2.

micahcochran commented 5 years ago

I retested on the Linux box with the repo version and it fixed the problem (and the strange behavior with crs_wkt).

snowman2 commented 5 years ago

Interesting. I wonder if it has to do with the wheel. Does the behavior persist with: pip install fiona --no-binary fiona --ignore-installed?

sgillies commented 5 years ago

I can't reproduce this issue. I suspect that missing or unlocated projection data files are the cause. Closing, hope that's okay.

micahcochran commented 5 years ago

Does the behavior persist with: pip install fiona --no-binary fiona --ignore-installed?

I performed that install, which installed Fiona 1.8.6 from source (--verbose). (This is on my Linux box.) In this installation, crs behaved normally.

snowman2 commented 5 years ago

I bet it has to do with having GDAL installed on the system mixed with the binary wheel. If you have GDAL already installed on the system, I usually install from source as I have seen weird behavior if I don't.

cgohlke commented 5 years ago

Another observation: if GDAL's SRS cache at ~/.gdal/X.Y/srs_cache does not exist, amb.crs is always empty, not just on first read.

joehuanguf commented 5 years ago

I just got this bug as well, on Fiona 1.8.6, GDAL 2.4.0

root@bcef0d1a5373:/code# ls -a ~/.gdal/2.4/srs_cache/
ls: cannot access '/root/.gdal/2.4/srs_cache/': No such file or directory

root@bcef0d1a5373:/code# ./manage.py test -vv -t uf_api/dataset/importer_test.py::TestImportShapeFile::test_invalid_testdata

root@bcef0d1a5373:/code# ls -a ~/.gdal/2.4/srs_cache/
.  ..  pcs.esri.gz  pcs.wkt.gz

It appears that you need to attempt to access a file's CRS first to force GDAL to create the cache.

sgillies commented 5 years ago

This SRS cache is news to me!