I've a geopandas dataframe stored with X, Y coordinates, their latitude and longitude equivalent and their geometry as points. Data snippet:
long lat X Y geometry
55898 -45.520195 -18.571566 151 179 POINT (-45.52019 -18.57157)
55902 -45.520227 -18.582375 151 183 POINT (-45.52023 -18.58238)
55910 -45.520293 -18.603994 151 191 POINT (-45.52029 -18.60399)
56267 -45.517361 -18.571574 152 179 POINT (-45.51736 -18.57157)
What I'd like to do is to unite 4 points sequentially, that is following the order in the dataframe, so I can get the coordinates of a square. The desired output doesn't really need to contain the other columns, just the geometry of a polygon, it would be similar to this:
1 POLYGON ((977855.4451904297 188082.3223876953,...
2 POLYGON ((1017949.977600098 225426.8845825195,...
3 POLYGON ((988872.8212280273 146772.0317993164,...
4 POLYGON ((1000721.531799316 136681.776184082, ...
5 POLYGON ((915517.6877458114 120121.8812543372,...
One should provide the lat and lon coordinates as a nested list to the Shapely Polygon object. See https://stackoverflow.com/a/30461816/12987768
With gdf
being the GeoDataFrame:
from shapely.geometry import Polygon
rows_numbers= [i for i in range(0, len(gdf), 4)]
polygons = []
for idx, _ in enumerate(rows_numbers):
if idx == len(rows_numbers)-1:
print("end of gdf")
break
polygon_coords = []
for point in gdf.geometry.values[rows_numbers[idx]: rows_numbers[idx+1]]:
polygon_coords.append([point.x, point.y])
polygons.append(Polygon(polygon_coords))
with the resulting polygons
list containing the 4-point Polygons
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.