python Geopandas不能很好地消化我的geojson:TypeError:字符串索引必须是整数,而不是'str'

zwghvu4y  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(126)

我从一个传入的POST请求中接收到一些geojson,我想用geopandas计算它的总平方英里。但由于某种原因,Geopandas给了我一个错误:

"geometry": shape(feature["geometry"]) if feature["geometry"] else None
                                              ~~~~~~~^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'

我的Python代码:

import functions_framework
import geopandas as gpd
from shapely.geometry import shape

@functions_framework.http
def calculate_sq_miles(request):

    geojson_dict = request.get_json()

    # Create a geopandas DataFrame from the dictionary
    df = gpd.GeoDataFrame.from_features(geojson_dict)

    # Calculate the area of the polygon in square meters
    area = df.geometry.area

    # Convert the area from square meters to square miles
    area_in_square_miles = area / 2589988.1103

    return area_in_square_miles

geojson输入数据:

{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[30.377407078486545,-112.12297812500002],[42.68629683675748,-112.12297812500002],[42.68629683675748,-126.71282187500002],[30.377407078486545,-126.71282187500002],[30.377407078486545,-112.12297812500002]]}}

起初我以为geojson输入格式不正确,但我没有看到任何问题。我做错了什么?

kqlmhetl

kqlmhetl1#

geojson中缺少额外的[]。再次将几何图形括在方括号中。

{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[30.377407078486545,-112.12297812500002],[42.68629683675748,-112.12297812500002],[42.68629683675748,-126.71282187500002],[30.377407078486545,-126.71282187500002],[30.377407078486545,-112.12297812500002]]]}}

相关问题