我正在使用 jupyterlab 。我正在尝试将 Ipyleaflet 多边形的点保存到 geopandas (如 https://gregfeliu.medium.com/turning-coordinates-into-a-geopandas-shape-58edd55dc2c1 中)。我的单元格中有以下内容:
zoom = 15
import ipywidgets
from __future__ import print_function
import ipyleaflet
import geopandas as gpd
from shapely.geometry import Point, LineString, Polygon
from ipyleaflet import (
Map,
Marker,
TileLayer, ImageOverlay,
Polyline, Polygon, Rectangle, Circle, CircleMarker,
GeoJSON,
DrawControl)
c = ipywidgets.Box()
# Define the columns and their data types
columns = ['geometry', 'column1', 'column2'] # Add more columns as needed
data_types = [Polygon, int, str] # Example data types, adjust as needed
# Create an empty GeoDataFrame
empty_gdf = gpd.GeoDataFrame(columns=columns)
# empty_gdf.crs = 'EPSG:4326' # For example, setting CRS to WGS84
# topo_background = True # Use topo as background rather than map?
# if topo_background:
# m = Map(width='1000px',height='600px', center=center, zoom=zoom, \
# default_tiles=TileLayer(url=u'http://otile1.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg'))
# else:
# m = Map(width='1000px',height='600px', center=center, zoom=zoom)
c.children = [m]
# keep track of rectangles and polygons drawn on map:
def clear_m():
global rects,polys
rects = set()
polys = set()
clear_m()
rect_color = '#a52a2a'
poly_color = '#00F'
myDrawControl = DrawControl(
rectangle={'shapeOptions':{'color':rect_color}},
polygon={'shapeOptions':{'color':poly_color}}) #,polyline=None)
def handle_draw(self, action, geo_json):
global rects,polys
polygon=[]
for coords in geo_json['geometry']['coordinates'][0][:-1][:]:
print(coords)
polygon.append(tuple(coords))
polygon = tuple(polygon)
if geo_json['properties']['style']['color'] == '#00F': # poly
if action == 'created':
polys.add(polygon)
polygon1 = shapely.geometry.Polygon(polygon)
empty_gdf.append(polygon1)
elif action == 'deleted':
polys.discard(polygon)
if geo_json['properties']['style']['color'] == '#a52a2a': # rect
if action == 'created':
rects.add(polygon)
elif action == 'deleted':
rects.discard(polygon)
myDrawControl.on_draw(handle_draw)
m.add_control(myDrawControl)
但是,当我在下一个单元格中运行“empty_gdf.head()”时,我得到一个空的地理数据框。我做错了什么?
在您的
handle_draw
函数中,您需要捕获 empty_gdf.append()
结果,因为它返回一个新的 DataFrame
对象,并且不会修改现有对象。
类似的东西:
import geopandas as gpd
def handle_draw(self, action, geo_json):
global rects,polys
polygon=[]
for coords in geo_json['geometry']['coordinates'][0][:-1][:]:
print(coords)
polygon.append(tuple(coords))
polygon = tuple(polygon)
if geo_json['properties']['style']['color'] == '#00F': # poly
if action == 'created':
polys.add(polygon)
polygon1 = shapely.geometry.Polygon(polygon)
temp_gdf = gpd.GeoDataFrame([[polygon1, 'Example1', 'Example2']],
columns=columns,
crs='epsg:4326')
empty_gdf = empty_gdf.append(temp_gdf, ignore_index=True)
此外,您可以考虑使用
pd.concat
,在对原始数据进行多次添加的情况下,这是首选:
import pandas as pd
import geopandas as gpd
temp_gdf = gpd.GeoDataFrame([[polygon1, 'Example1', 'Example2']],
columns=columns,
crs='epsg:4326')
empty_gdf = gpd.GeoDataFrame(pd.concat([empty_gdf, temp_gdf], ignore_index=True),
crs='epsg:4326')
pd.concat
可能会在第一次调用该函数时发出警告,因为 empty_gdf
将为空。