您知道如何解决Geovornoi天真几何问题吗?

问题描述

import numpy as np
import geopandas as gpd
import contextily as ctx
import matplotlib.pyplot as plt
from shapely.ops import cascaded_union
from geovoronoi.plotting import subplot_for_map,plot_voronoi_polys_with_points_in_area
from geovoronoi import voronoi_regions_from_coords,points_to_coords
gdf = gpd.read_file("C:/Users/heejb/Desktop/R&E/gwangju/point.shp")
gdf.head()

boundary = gpd.read_file("C:/Users/heejb/Desktop/R&E/gwangju/gj.shp")
fig,ax = plt.subplots(figsize=(12,10))
boundary.plot(ax=ax,color="gray")
gdf.plot(ax=ax,markersize=3.5,color="black")
ax.axis("off")
plt.axis("equal")
plt.show()

boundary = boundary.to_crs(epsg=3857)
gdf_proj = gdf.to_crs(boundary.crs)

boundary_shape = cascaded_union(boundary.geometry)
coords = points_to_coords(gdf_proj.geometry)

poly_shapes,pts,poly_to_pt_assignments = voronoi_regions_from_coords(coords,boundary_shape)

fig,ax = subplot_for_map()
plot_voronoi_polys_with_points_in_area(ax,boundary_shape,poly_shapes,poly_to_pt_assignments)
ax.set_title('Voronoi regions of Schools in Uppsala')
plt.tight_layout()
plt.show()

fig,ax = plt.subplots(figsize=(14,12))
plot_voronoi_polys_with_points_in_area(ax,poly_to_pt_assignments,voronoi_and_points_cmap="tab20c",points_markersize=20)
ax.axis("off")
plt.tight_layout()
plt.show()

我试图在地图上制作voronoi图,但是每次尝试都会收到此错误消息:

Traceback (most recent call last):
  File "C:/Users/heejb/Desktop/rne.py",line 20,in <module>
    gdf_proj = gdf.to_crs(boundary.crs)
  File "C:\Users\heejb\AppData\Local\Programs\Python\python36\lib\site-packages\geopandas\geodataframe.py",line 816,in to_crs
    geom = df.geometry.to_crs(crs=crs,epsg=epsg)
  File "C:\Users\heejb\AppData\Local\Programs\Python\python36\lib\site-packages\geopandas\geoseries.py",line 527,in to_crs
    "Cannot transform naive geometries.  "
ValueError: Cannot transform naive geometries.  Please set a crs on the object first.

有人知道是什么问题吗?

解决方法

您的shapefile尚未分配CRS(投影)。使用to_crs之前,您必须手动执行此操作。不幸的是,您必须知道应该是哪个。

boundary = gpd.read_file("C:/Users/heejb/Desktop/R&E/gwangju/gj.shp")
boundary.crs = <add CRS here>