6

I'd like to be able to create a new GeoPandas object and inherit the CRS from Rasterio, and save a shapefile. The current projection format is seen as invalid when writing an ESRI driver.

From here, it would suggest GeoPandas requires CRS in dict format: https://geopandas.org/projections.html and Reprojecting data with GeoPandas using 'to_crs'?, though this hints as dataset.crs.data (How can I superimpose a geopandas dataframe on a raster plot?).

It seems to be known that you must pass the crs_wkt directly to geopandas.to_file method: GeoPandas to_file() saves GeoDataFrame without coordinate system

simple psuedo code

boxes = geopandas.GeoDataFrame()
with rasterio.open(raster_path) as dataset:
    boxes.crs = dataset.crs.to_dict()

boxes.to_file(<filename>,driver="ESRI Shapefile",crs_wkt=boxes.crs)

yields

TypeError: invalid crs_wkt

The specific projection i'm trying is:

dataset.crs.to_dict()

{'proj': 'lcc',
 'lat_1': 43,
 'lat_2': 45.5,
 'lat_0': 41.75,
 'lon_0': -120.5,
 'x_0': 399999.9999999999,
 'y_0': 0,
 'ellps': 'GRS80',
 'units': 'ft',
 'no_defs': True}

Though I've played around with others, so I think its a workflow issue.

Rasterio version: 1.1.2 GeoPandas version: 0.6.2 Fiona version: 1.8.13

As an added comment, opening the raster in R and extracting the proj 4 string

> proj4string(a)
[1] "+proj=lcc +lat_1=43 +lat_2=45.5 +lat_0=41.75 +lon_0=-120.5 +x_0=399999.9999999999 +y_0=0 +ellps=GRS80 +units=ft +no_defs"

and assigning the string into boxes.crs above yields no error.

bw4sz
  • 363
  • 1
  • 9

1 Answers1

6

You should try to preserve the WKT form if possible. See: https://proj.org/faq.html#what-is-the-best-format-for-describing-coordinate-reference-systems

It all depends on the version of geopandas.

When version 0.7.0 comes out, you can do:

boxes = geopandas.GeoDataFrame()
with rasterio.open(raster_path) as dataset:
    boxes.crs = dataset.crs

boxes.to_file(<filename>,driver="ESRI Shapefile")

With version 0.6 and pyproj 2.2+, you can do:

boxes = geopandas.GeoDataFrame()
with rasterio.open(raster_path) as dataset:
    boxes.crs = dataset.crs.to_wkt()

boxes.to_file(<filename>,driver="ESRI Shapefile")

With pyproj 1.x, you can do:

boxes = geopandas.GeoDataFrame()
with rasterio.open(raster_path) as dataset:
    boxes.crs = dataset.crs.to_dict()

boxes.to_file(<filename>,driver="ESRI Shapefile")
snowman2
  • 7,321
  • 12
  • 29
  • 54