【847】create geoDataFrame from dataframe

发布时间 2023-06-30 08:58:59作者: McDelfino

Ref: From WKT format

Firstly, we already have a dataframe, and there is a column of geometry. But this column is in the format of the string, therefore, we should change the data format from the string to the polygon.

There are two ways to implement this method.

The first method,

df = pd.DataFrame(
    {
        "City": ["Buenos Aires", "Brasilia", "Santiago", "Bogota", "Caracas"],
        "Country": ["Argentina", "Brazil", "Chile", "Colombia", "Venezuela"],
        "Coordinates": [
            "POINT(-58.66 -34.58)",
            "POINT(-47.91 -15.78)",
            "POINT(-70.66 -33.45)",
            "POINT(-74.08 4.60)",
            "POINT(-66.86 10.48)",
        ],
    }
)

from shapely import wkt

df["Coordinates"] = wkt.loads(df["Coordinates"])

gdf = geopandas.GeoDataFrame(df, geometry="Coordinates")

The second method,

df["Coordinates"] = geopandas.GeoSeries.from_wkt(df["Coordinates"])

gdf = geopandas.GeoDataFrame(df, geometry="Coordinates")