I'm working with Jupiter and ipywidgets and Ipyleaflet , trying to draw polygons on a map and saving to a geodataframe. I have the following in a notebook cell:
myDrawControl = DrawControl(
rectangle={'shapeOptions':{'color':rect_color}},
polygon={'shapeOptions':{'color':poly_color}}) #,polyline=None)
def handle_draw(self, action, geo_json):
handle_interaction("action ")
handle_interaction(action)
handle_interaction("geojson ")
handle_interaction(geo_json)
handle_interaction("geojson ")
handle_interaction(geo_json['geometry']['coordinates'])
global rects,polys
global gdf2
coordinates = geo_json['geometry']['coordinates'][0]
polygon_coordinate_tuples = [(lon, lat) for lon, lat in coordinates]
handle_interaction("tuples ")
handle_interaction(polygon_coordinate_tuples)
if geo_json['properties']['style']['color'] == '#00F': # poly
if action == 'created':
handle_interaction(" created")
polygon_geom = geometry.Polygon(polygon_coordinate_tuples)
handle_interaction((polygon_geom))
# Create GeoDataFrame if it doesn't exist
if gdf2 is None:
gdf2 = gpd.GeoDataFrame(geometry=[polygon_geom])
else:
handle_interaction(" exists GDF2")
gdf2 = gdf2.append({'geometry': polygon_geom}, ignore_index=True)
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)
I printed out the coordinates when selected using the next cell:
action
created
geojson
{'type': 'Feature', 'properties': {'style': {'stroke': True, 'color': '#00F', 'weight': 4, 'opacity': 0.5, 'fill': True, 'fillColor': None, 'fillOpacity': 0.2, 'clickable': True}}, 'geometry': {'type': 'Polygon', 'coordinates': [[[-88.433717, 31.659469], [-88.430242, 31.659505], [-88.430456, 31.657898], [-88.433331, 31.657679], [-88.434876, 31.658665], [-88.433717, 31.659469]]]}}
geojson
[[[-88.433717, 31.659469], [-88.430242, 31.659505], [-88.430456, 31.657898], [-88.433331, 31.657679], [-88.434876, 31.658665], [-88.433717, 31.659469]]]
tuples
[(-88.433717, 31.659469), (-88.430242, 31.659505), (-88.430456, 31.657898), (-88.433331, 31.657679), (-88.434876, 31.658665), (-88.433717, 31.659469)]
created
POLYGON ((-88.433717 31.659469, -88.430242 31.659505, -88.430456 31.657898, -88.433331 31.657679, -88.434876 31.658665, -88.433717 31.659469))
In the next cell, I run "print(gdf2)" I get:
geometry
0 POLYGON ((-88.43372 31.65947, -88.43024 31.65950, -88.43046 31.65790, -88.43333 31.65768, -88.43488 31.65866, -88.43372 31.65947))
which does not appear to match the printed out coordinates in output.
Why not?
