Reputation: 49
I want to create a line with custom defined width (km) using shapely. But it seems like line basically defined as a geometry without depth. In that I am more than happy to create a line with width in the form of rectangular polygon PROVIDED only start and end point of the line is available.
Is it possible to use buffer option in linestring to add the width?
Upvotes: 1
Views: 1398
Reputation: 31226
import pandas as pd
import numpy as np
import geopandas as gpd
import shapely.geometry
import requests, json
import plotly.express as px
# source some points and polygons
# fmt: off
dfp = pd.read_html("https://www.latlong.net/category/cities-235-15.html")[0]
dfp = gpd.GeoDataFrame(
dfp,
geometry=dfp.loc[:,["Longitude","Latitude"]].apply(shapely.geometry.Point, axis=1),
crs="EPSG: 4326",
)
# fmt: on
# ramdonly select just two points
dfp = dfp.sample(2)
# construct a LineString from points
line = shapely.geometry.LineString(dfp.loc[:, ["Longitude", "Latitude"]].values)
# add a buffer to LineString (hence becomes a polygon)
DISTANCE = 10 ** 4 # 10km
lineb = (
gpd.GeoSeries([line], crs="EPSG: 4326")
.to_crs(dfp.estimate_utm_crs())
.buffer(DISTANCE ,cap_style=2)
.to_crs("EPSG: 4326")
)
# plot the points, the buffered line (polygon) and the line
px.scatter_mapbox(dfp, lat="Latitude", lon="Longitude").update_layout(
mapbox={
"style": "carto-positron",
"zoom": 4,
"layers": [
{
"source": json.loads(lineb.to_json()),
"below": "traces",
"type": "fill",
"color": "lightgrey",
},
{
"source": json.loads(gpd.GeoSeries(line).to_json()),
"type": "line",
"color": "green",
},
],
}
)
Upvotes: 1