Paul
Paul

Reputation: 3

How to drop SRID=4326 from GeoPandas MultiPolygon Geometry column

I am using shape data from .shp files and using geopandas to convert those into Multipolygons for my postgresql table with a column of type geometry(multipolygon, 4326) NULL. The issue I'm running into is I don't know how I can drop the SRID=4326 part of the data.

For example, SRID=4326;MULTIPOLYGON(((-113.82162 31.50802........, but I just need MULTIPOLYGON(((-113.82162 31.50802......... I'm fairly new to GeoPandas and any help would be appreciated. Here is the necessary code.


gdf = gpd.read_file(shapefile_path).to_crs(epsg=4326)
gdf['geometry'] = gdf['geometry'].apply(lambda x: MultiPolygon([x]) if isinstance(x, Polygon) else x)
gdf['geometry'] = gdf['geometry'].apply(lambda x: WKTElement(x.wkt, srid=4326))
gdf.to_sql(table_name, engine, schema='shapes', if_exists='append', index=False, method='multi', dtype={'geometry': Geometry('MULTIPOLYGON', srid=4326)})

Upvotes: 0

Views: 122

Answers (1)

antoine
antoine

Reputation: 578

If I understand correctly, you have a column in your shapefile with the WKT data you want to convert to a geopandas.GeoDataFrame losing the SRID=4326 part.

Something like that could do the trick (added dummy data to manipulate) :

import geopandas as gpd
from shapely import wkt

points = [{'WKT': "SRID=4326;MULTIPOLYGON(((2.35 48.85, 2.36 48.85, 2.36 48.86, 2.35 48.86, 2.35 48.85)), ((2.37 48.87, 2.38 48.87, 2.38 48.88, 2.37 48.88, 2.37 48.87)))"}]
gdf = gpd.GeoDataFrame(points)
gdf['WKT'] = gdf['WKT'].apply(lambda x: x.replace('SRID=4326;', ''))
gdf['WKT'] = gdf['WKT'].apply(wkt.loads)
gdf = gdf.set_geometry('WKT').set_crs('epsg:4326')

Upvotes: 0

Related Questions