79094276

Date: 2024-10-16 13:32:14
Score: 0.5
Natty:
Report link

Thanks to @Clarence Kuo, this is the helper function I created for when I had the same question as the OP: extracting the latitude and longitude from a WKT LINESTRING Geometry object in GeoPandas:

import pandas as pd
from shapely import wkt
from typing import Tuple

def expand_max_min_lat_long_from_wkt(df: pd.DataFrame, wkt_col_name: str) -> pd.DataFrame:
    
    def _check_n_correct_wkt(df: pd.DataFrame, wkt_col_name: str) -> Tuple[pd.DataFrame, str]:
        if str(df[wkt_col_name].dtype) != 'geometry':
            new_wkt_col_name = f"{wkt_col_name}__WKT"
            df[new_wkt_col_name] = df[wkt_col_name].apply(wkt.loads)
            wkt_col_name = new_wkt_col_name
        return df, wkt_col_name

    def _expand_max_min_lat_long_from_wkt(wkt_val) -> tuple:
        if not wkt_val:
            return (None, None, None, None)
        lon, lat = wkt_val.coords.xy
        return (
            min(lat),
            max(lat),
            min(lon),
            max(lon),
        )

    df, wkt_col_name = _check_n_correct_wkt(df, wkt_col_name)

    (
        df[f"{wkt_col_name}__LAT_MIN"], 
        df[f"{wkt_col_name}__LAT_MAX"], 
        df[f"{wkt_col_name}__LONG_MIN"], 
        df[f"{wkt_col_name}__LONG_MAX"],
    ) = zip(*df[wkt_col_name].apply(_expand_max_min_lat_long_from_wkt))

    return df

With usage of:

gdf = expand_max_min_lat_long_from_wkt(gdf, 'geometry')

and results like: enter image description here

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Whitelisted phrase (-1): I had the same
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Clarence
  • Low reputation (0.5):
Posted by: Weston A. Greene