0

enter image description here

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?

gene
  • 54,868
  • 3
  • 110
  • 187
user1592380
  • 143
  • 4

1 Answers1

2

The precision (number of decimal places) was rounded by geopandas for display when you printed gdf2. Internally, the precision has not been rounded and your geodataframe retains the full precision of the coordinates you created it with.

You can use geopandas.options.display_precision = 6 to specify 6 decimal places are displayed when printing the geodataframe to match the geojson you printed.

See GeoPandas display options:

display_precision: None [default: None]

  The precision (maximum number of decimals) of the coordinates in the
  WKT representation in the Series/DataFrame display. By default (None),
  it tries to infer and use 3 decimals for projected coordinates and 5
  decimals for geographic coordinates.

Related question:

Example:

import geopandas as gpd

geojson = { "type": "Feature", "properties": {}, "geometry": { "coordinates": [ 1.23456789, 0.987654321 ], "type": "Point" } }

print(geojson["geometry"]["coordinates"]) gdf = gpd.GeoDataFrame.from_features([geojson])

print(gdf)

gpd.options.display_precision = 2 print(gdf)

gpd.options.display_precision = 9 print(gdf)

Output:

[1.23456789, 0.987654321]
                  geometry
0  POINT (1.23457 0.98765)
            geometry
0  POINT (1.23 0.99)
                          geometry
0  POINT (1.234567890 0.987654321)
user2856
  • 65,736
  • 6
  • 115
  • 196