Created
July 8, 2022 14:56
-
-
Save harshithjv/bcd3fef5661ce0a2ec20224e8e4ac415 to your computer and use it in GitHub Desktop.
To convert any WKT string to Polygon WKT if buffer is provided in meters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code reference: https://gis.stackexchange.com/a/327046 | |
| import json | |
| import pyproj | |
| import shapely.wkt as shp_wkt | |
| import shapely.geometry as shp_geo | |
| from shapely.geometry import Polygon as ShpPolygon | |
| PROJECTION_IN = pyproj.Proj( | |
| proj="utm", | |
| zone=45, # UTM Zone for India | |
| ellps="WGS84", | |
| datum="WGS84" | |
| ) | |
| def toFromUTM(shp, proj, inv=False): | |
| """ | |
| How to use? | |
| >>> import shapely.wkt | |
| >>> import shapely.geometry | |
| >>> proj = PROJECTION_IN # constant declared above the function definition | |
| >>> shp_obj = shapely.wkt.loads('LINESTRING(76.46019279956818 15.335048625850606,76.46207302808762 15.334717526558398)') | |
| >>> meters = 10 | |
| >>> init_shape_utm = toFromUTM(shp_obj, proj) | |
| >>> buffer_shape_utm = init_shape_utm.buffer(meters) | |
| >>> buffer_shape_lonlat = toFromUTM(buffer_shape_utm, proj, inv=True) | |
| >>> out = shapely.geometry.mapping(buffer_shape_lonlat) | |
| >>> geojson = json.loads(json.dumps(out)) | |
| Note: shp_obj is shapely object of type: Polygon, MultiPolygon, LineString and Point | |
| """ | |
| geoInterface = shp.__geo_interface__ | |
| shpType = geoInterface['type'] | |
| coords = geoInterface['coordinates'] | |
| if shpType == 'Polygon': | |
| newCoord = [[proj(*point, inverse=inv) for point in linring] for linring in coords] | |
| elif shpType == 'MultiPolygon': | |
| newCoord = [[[proj(*point, inverse=inv) for point in linring] for linring in poly] for poly in coords] | |
| elif shpType == 'LineString': | |
| newCoord = [proj(*point, inverse=inv) for point in coords] | |
| elif shpType == 'Point': | |
| newCoord = proj(*coords, inverse=inv) | |
| return shp_geo.shape({'type': shpType, 'coordinates': tuple(newCoord)}) | |
| def transform_wkt_with_buffer(wkt_str: str, buffer: float) -> str: | |
| """Transform WKT string to WKT Polygon with buffer. | |
| Parameters: | |
| wkt_str (str): WKT or Well-known text representation of geometry | |
| buffer (float): Buffer length(in meters) surrounding give shape. | |
| Returns: | |
| dict: JSON style dict object | |
| How to use? | |
| >>> output_wkt = transform_wkt_with_buffer('LINESTRING(76.46019279956818 15.335048625850606,76.46207302808762 15.334717526558398)', 10) | |
| """ | |
| shp_obj = shp_wkt.loads(wkt_str) | |
| init_shape_utm = toFromUTM(shp_obj, PROJECTION_IN) | |
| buffer_shape_utm = init_shape_utm.buffer(buffer) | |
| buffer_shape_lonlat = toFromUTM(buffer_shape_utm, PROJECTION_IN, inv=True) | |
| return buffer_shape_lonlat.wkt | |
| def convert_shapely_to_geojson(shp:ShpPolygon) -> str: | |
| shp_polygon = shp_geo.mapping(shp) | |
| return json.dumps(shp_polygon) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment