[GIS] Reprojecting data with GeoPandas using ‘to_crs’

coordinate systemgeopandaspythonrasterio

I am trying to reproject a polygon so that it has the same projection than a raster dataset.

raster.crs
CRS({'proj': 'tmerc', 'lat_0': 0, 'lon_0': -183, 'k': 0.9996, 'x_0': 500000, 'y_0': 0, 'datum': 'WGS84', 'units': 'm', 'no_defs': True})

polygon.crs
{'init': 'epsg:3035', 'no_defs': True}

I believe I should be using an instruction like the one below:

polygon = polygon.to_crs(epsg=XXXX) 

where XXXX is the epsg code that tells the coordinate system of the raster dataset. However, I don't know hot to obtain such code. (I know that the epsg code of WGS84 is 4326, but I would like to know how to obtain this number programmatically)

Best Answer

The epsg code is only one way to reproject the dataset, but is not the only way. As mentioned here you can also use PROJ strings or dictionaries. You can use the to_dict() method of the rasterio CRS class.

In your use case, you can reproject your geopandas dataframe like so:

polygon = polygon.to_crs(raster.crs.to_dict()) 

In future versions of geopandas, you will be able to pass in the raster.crs directly to the to_crs() function. This will occur after this PR is merged (https://github.com/geopandas/geopandas/pull/998).