mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-20 12:50:47 +08:00
Co-authored-by: sangarshanan <sangarshanan1998@gmail.com>
17 KiB
17 KiB
In [ ]:
import geopandas
path_to_data = geopandas.datasets.get_path("nybb")
gdf = geopandas.read_file(path_to_data)
gdfIn [ ]:
gdf.to_file("my_file.geojson", driver="GeoJSON")In [ ]:
gdf = gdf.set_index("BoroName")In [ ]:
gdf["area"] = gdf.area
gdf["area"]In [ ]:
gdf['boundary'] = gdf.boundary
gdf['boundary']In [ ]:
gdf['centroid'] = gdf.centroid
gdf['centroid']In [ ]:
first_point = gdf['centroid'].iloc[0]
gdf['distance'] = gdf['centroid'].distance(first_point)
gdf['distance']In [ ]:
gdf['distance'].mean()In [ ]:
gdf.plot("area", legend=True)In [ ]:
gdf.explore("area", legend=False)In [ ]:
gdf = gdf.set_geometry("centroid")
gdf.plot("area", legend=True)In [ ]:
ax = gdf["geometry"].plot()
gdf["centroid"].plot(ax=ax, color="black")In [ ]:
gdf = gdf.set_geometry("geometry")In [ ]:
gdf["convex_hull"] = gdf.convex_hullIn [ ]:
ax = gdf["convex_hull"].plot(alpha=.5) # saving the first plot as an axis and setting alpha (transparency) to 0.5
gdf["boundary"].plot(ax=ax, color="white", linewidth=.5) # passing the first plot and setting linewitdth to 0.5In [ ]:
# buffering the active geometry by 10 000 feet (geometry is already in feet)
gdf["buffered"] = gdf.buffer(10000)
# buffering the centroid geometry by 10 000 feet (geometry is already in feet)
gdf["buffered_centroid"] = gdf["centroid"].buffer(10000)In [ ]:
ax = gdf["buffered"].plot(alpha=.5) # saving the first plot as an axis and setting alpha (transparency) to 0.5
gdf["buffered_centroid"].plot(ax=ax, color="red", alpha=.5) # passing the first plot as an axis to the second
gdf["boundary"].plot(ax=ax, color="white", linewidth=.5) # passing the first plot and setting linewitdth to 0.5In [ ]:
brooklyn = gdf.loc["Brooklyn", "geometry"]
brooklynIn [ ]:
type(brooklyn)In [ ]:
gdf["buffered"].intersects(brooklyn)In [ ]:
gdf["within"] = gdf["buffered_centroid"].within(gdf)
gdf["within"]In [ ]:
gdf = gdf.set_geometry("buffered_centroid")
ax = gdf.plot("within", legend=True, categorical=True, legend_kwds={'loc': "upper left"}) # using categorical plot and setting the position of the legend
gdf["boundary"].plot(ax=ax, color="black", linewidth=.5) # passing the first plot and setting linewitdth to 0.5In [ ]:
gdf.crsIn [ ]:
gdf = gdf.set_geometry("geometry")
boroughs_4326 = gdf.to_crs("EPSG:4326")
boroughs_4326.plot()In [ ]:
boroughs_4326.crs