import pandas as pd
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString
我想从我拥有的Geojson文件中获取坐标数组。
下面是我打开的文件中的内容示例。
gdf1 = gpd.read_file(fn)
print(gdf1)
N02_001 N02_002 N02_003 N02_004 geometry
0 23 5 沖縄都市モノレール線 沖縄都市モノレール LINESTRING (127.67948 26.21454, 127.67970 26.2...
1 12 5 いわて銀河鉄道線 アイジーアールいわて銀河鉄道 LINESTRING (141.29139 40.33740, 141.29176 40.3...
2 12 5 いわて銀河鉄道線 アイジーアールいわて銀河鉄道 LINESTRING (141.27554 40.23936, 141.27567 40.2...
3 12 5 いわて銀河鉄道線 アイジーアールいわて銀河鉄道 LINESTRING (141.28659 40.26092, 141.28538 40.2...
4 12 5 いわて銀河鉄道線 アイジーアールいわて銀河鉄道 LINESTRING (141.29082 40.28615, 141.29089 40.2...
我过滤了它。
all_wthcl = gdf1[gdf1.N02_003==linename]
only_cor =all_wthcl["geometry"]
print(only_cor)
15597 LINESTRING (140.20076 35.68626, 140.19987 35.6...
15598 LINESTRING (140.68460 35.72969, 140.68210 35.7...
15599 LINESTRING (140.49187 35.66233, 140.48968 35.6...
15600 LINESTRING (140.82610 35.72950, 140.82596 35.7...
15601 LINESTRING (140.31649 35.66363, 140.31628 35.6...
...
15700 LINESTRING (140.10267 35.62336, 140.10214 35.6...
15701 LINESTRING (139.96020 35.70700, 139.96272 35.7...
15702 LINESTRING (139.96430 35.70510, 139.96536 35.7...
15703 LINESTRING (139.96004 35.70682, 139.96239 35.7...
15704 LINESTRING (139.96430 35.70510, 139.96478 35.7...
但是我无法获得想要的结果(想要使其成为数组或从中获取值)。
testvar = np.array(only_cor)
print(testvar)
Name: geometry, Length: 108, dtype: geometry
[<shapely.geometry.linestring.LineString object at 0x0000021E039E7188>
<shapely.geometry.linestring.LineString object at 0x0000021E039E7308>
<shapely.geometry.linestring.LineString object at 0x0000021E039E7488>
<shapely.geometry.linestring.LineString object at 0x0000021E039E7608>
<shapely.geometry.linestring.LineString object at 0x0000021E039E7788>
<shapely.geometry.linestring.LineString object at 0x0000021E039E7908>
<shapely.geometry.linestring.LineString object at 0x0000021E039E7A88>
<shapely.geometry.linestring.LineString object at 0x0000021E039E7C08>
<shapely.geometry.linestring.LineString object at 0x0000021E039E5E88>
<shapely.geometry.linestring.LineString object at 0x0000021E039E5FC8>
....
]
我最后一次尝试从过滤数据中获取值
testvar = only_cor.values
print(testvar)
<GeometryArray>
[<shapely.geometry.linestring.LineString object at 0x0000016528336188>,
<shapely.geometry.linestring.LineString object at 0x0000016528336308>,
<shapely.geometry.linestring.LineString object at 0x0000016528336488>,
<shapely.geometry.linestring.LineString object at 0x0000016528336608>,
<shapely.geometry.linestring.LineString object at 0x0000016528336788>,
请让我知道我该怎么办。
谢谢。
通过使用json(而不是geopandas的geoseries)以“ dict”格式打开文件,这可能是一种更轻松的功能管理方法, 我敢肯定有解决此问题的更有效方法(就像下面对这个问题的回答一样),但我希望这可以帮助其他遭受相同问题的人。
with open(filename, encoding='utf-8') as f:
data = json.load(f)['features']
coords = []
for iv in data:
lanename = iv["properties"]["N02_003"]
if lanename == "総武線":
templst = iv['geometry']['coordinates']
for i in templst:
coords.append(i)
print(coords)
print("end")
答案 0 :(得分:0)
Shapely的LineString
对象具有一个coords
property,该对象在转换为列表时将包含一个坐标元组列表:
>>> from shapely.geometry import LineString
>>> line = LineString([(0, 0), (1, 1)])
...
>>> list(line.coords)
[(0.0, 0.0), (1.0, 1.0)]
您可以遍历testvar
并从每个属性中获取该属性:
coordinates = []
for _ in testvar:
coordinates.append(list(_.coords))