Search code examples
pythonprojectiongeopandas

how to assign units to geometries and projection to polygon in python3?


I created a multipolygon (shapefile) from a PNG image. The PNG image was originally a GeoTIFF. The geometries of my multipolygon are values of a normal x,y axis (with 0,0 as origin). In other words, the geometries are not lat,long. See sample:

geometry  
0  POLYGON ((0.000 0.000, 0.000 310.000, 5.000 31...  
1  POLYGON ((160.000 0.000, 160.000 3.000, 159.00...  
2  POLYGON ((794.000 219.000, 794.000 0.000, 443....  
3  POLYGON ((555.000 451.000, 793.000 451.000, 79...  
4  POLYGON ((417.000 451.000, 555.000 451.000, 55...  
5  POLYGON ((237.000 196.000, 237.000 205.000, 23...

I want to project my polygon in a specific CRS and location on Earth. This polygon should match the bbox and CRS of the original GeoTIFF. My multipolygon has no CRS defined.

How can I project my multipolygon? I tried the following, and got the error: AttributeError: 'GeoDataFrame' object has no attribute 'set_crs'

import geopandas as gpd

fp = r"C:\foo\intersection_all2.shp" 

data = gpd.read_file(fp)
data['geometry'].head()


# Let's make a copy of our data
orig = data.copy()

# Reproject the data
data = data.set_crs(epsg=32618)

I am searching a solution for Python 3.7, Windows 10.


Solution

  • it says no attribute, so set crs at the object property like this

    gdf.crs = {"init":"epsg:4326"}
    

    then you can project it to another by to_crs(), for example

    gdf.to_crs(epsg=3857)