Adding shapely polygon to geopandas

38 views Asked by At

enter image description here

I'm working with jupyterlab . I'm trying to save the points of a Ipyleaflet polygon to geopandas (like in https://gregfeliu.medium.com/turning-coordinates-into-a-geopandas-shape-58edd55dc2c1). I have the following in a cell:

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)

However when I run "empty_gdf.head()" in the next cell, I get an empty geodataframe. What am I doing wrong?

0

There are 0 answers