无法从 geojson 列创建 geopandas 几何图形
Unable to create geopandas geometry from geojson column
我有一个 csv,其中包含一个有效的 geojson 对象几何表示形式的字段。我能够将它作为 json 字典加载,然后我在 geopandas set_geometry 中设置它而没有错误。虽然一切都正确执行,但我无法弄清楚为什么我的几何图形是空的。这是 csv:
代码如下:
from shapely.geometry import shape
df =pd.read_csv('c:/random_polygon.csv')
def parse_geom(geom_str):
try:
return shape(json.loads(geom_str))
except (TypeError, AttributeError):
return None
["geometry"] = df["geo_properties"].apply(parse_geom)
df = df[df.geo_properties != None]
gdf = gpd.GeoDataFrame(df, geometry="geometry")
gdf = gdf.set_crs('epsg:4326')
print(gdf.head())
这是地理位置json:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-89.77632522583008,
34.07143110146331
],
[
-89.78070259094238,
34.05422388685686
],
[
-89.76662635803223,
34.04825031787262
],
[
-89.74491119384766,
34.04917482631512
],
[
-89.74748611450195,
34.0705068357665
],
[
-89.75503921508789,
34.08095756019248
],
[
-89.77726936340332,
34.0810286491402
],
[
-89.78233337402344,
34.08053102525307
],
[
-89.77632522583008,
34.07143110146331
]
]
]
}
}
]
}
您的 DataFrame geo_properties
列中的每个单元格似乎都包含一个完整的 GeoJSON 字符串。这不起作用,因为 Features and FeatureCollections are not supported by shapely.
尝试用 geopandas 解析每个单元格并提取 GeometryArray:
In [18]: df.geo_properties.apply(
...: lambda x: gpd.read_file(x, driver="GeoJSON").geometry.values
...: )
...:
Out[18]:
0 [POLYGON ((-89.77632522583008 34.0714311014633...
Name: geo_properties, dtype: object
我有一个 csv,其中包含一个有效的 geojson 对象几何表示形式的字段。我能够将它作为 json 字典加载,然后我在 geopandas set_geometry 中设置它而没有错误。虽然一切都正确执行,但我无法弄清楚为什么我的几何图形是空的。这是 csv:
代码如下:
from shapely.geometry import shape
df =pd.read_csv('c:/random_polygon.csv')
def parse_geom(geom_str):
try:
return shape(json.loads(geom_str))
except (TypeError, AttributeError):
return None
["geometry"] = df["geo_properties"].apply(parse_geom)
df = df[df.geo_properties != None]
gdf = gpd.GeoDataFrame(df, geometry="geometry")
gdf = gdf.set_crs('epsg:4326')
print(gdf.head())
这是地理位置json:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-89.77632522583008,
34.07143110146331
],
[
-89.78070259094238,
34.05422388685686
],
[
-89.76662635803223,
34.04825031787262
],
[
-89.74491119384766,
34.04917482631512
],
[
-89.74748611450195,
34.0705068357665
],
[
-89.75503921508789,
34.08095756019248
],
[
-89.77726936340332,
34.0810286491402
],
[
-89.78233337402344,
34.08053102525307
],
[
-89.77632522583008,
34.07143110146331
]
]
]
}
}
]
}
您的 DataFrame geo_properties
列中的每个单元格似乎都包含一个完整的 GeoJSON 字符串。这不起作用,因为 Features and FeatureCollections are not supported by shapely.
尝试用 geopandas 解析每个单元格并提取 GeometryArray:
In [18]: df.geo_properties.apply(
...: lambda x: gpd.read_file(x, driver="GeoJSON").geometry.values
...: )
...:
Out[18]:
0 [POLYGON ((-89.77632522583008 34.0714311014633...
Name: geo_properties, dtype: object