file = gpd.read_file(r"E:\code-study\class-test\test.geojson") #获取每条线段的端点坐标 for index,row in file.iterrows(): if row["geometry"].geom_type == "MultiLineString": for line in row["geometry"].geoms: start = line.coords[0] end = line.coords[-1] print("Line start:", start) print("Line end", end),Line start: (345888.8377459495, 3449498.849251645) Line end (345629.97464499675, 3449407.506965276) Line start: (345280.09157031064, 3449030.0337291397) Line end (345272.49128222495, 3449049.808989464) Line start: (345272.49128222495, 3449049.808989464) Line end (345207.97659461905, 3449222.778335579) Line start: (346000.16949852963, 3449534.6728273346) Line end (345945.905299675, 3449515.56654852) Line start: (345912.594155262, 3449503.8347858437) Line end (345630.9943146321, 3449404.431732289) Line start: (346072.48027534503, 3450395.8932189597) Line end (346108.97338958416, 3450251.398985895) Line start: (346118.59858503146, 3450213.518651368) Line end (346259.0512482276, 3449646.0243423935) Line start: (346273.0178897714, 3449593.106077165) Line end (346335.5867677608, 3449370.609103672) Line start: (346265.8854519349, 3449647.9424282) Line end (346122.1064710339, 3450214.6475669923) Line start: (346112.2891860679, 3450252.2834272203) Line end (346075.36625823507, 3450396.1075029722) Line start: (345411.5247303593, 3450021.971671897) Line end (345556.4158206382, 3450076.3672987036) Line start: (345517.817400123, 3450059.154634558) Line end (345770.503485893, 3450140.345187006) Line start: (345804.2092067172, 3450150.3849952365) Line end (345874.2574445517, 3450167.3248460926) Line start: (345924.80312931805, 3450186.197005902) Line end (346102.13061896793, 3450230.692243254) Line start: (345766.5523878782, 3450156.3483928964) Line end (345409.48140833457, 3450028.96165739) Line start: (346069.2903592914, 3450395.792547785) Line end (346105.8483254915, 3450250.5602219505) Line start: (346115.2730931371, 3450212.4484372856) Line end (346255.98977489606, 3449645.165328472) Line start: (346269.80789709534, 3449591.9630052154) Line end (346332.0645057148, 3449369.94193096) Traceback (most recent call last): File "E:\code-study\coda\cross_nostopline.py", line 10, in <module> if row["geometry"].geom_type == "MultiLineString": AttributeError: 'NoneType' object has no attribute 'geom_type'
时间: 2024-03-02 08:52:36 浏览: 277
这段代码出现了错误,因为有些行的几何类型不是`MultiLineString`,所以在访问`geom_type`属性时出现了`NoneType`错误。要解决这个问题,可以在判断几何类型之前,先判断当前行的`geometry`是否为空,即:
```
for index, row in file.iterrows():
if row["geometry"] is not None and row["geometry"].geom_type == "MultiLineString":
for line in row["geometry"].geoms:
start = line.coords[0]
end = line.coords[-1]
print("Line start:", start)
print("Line end:", end)
```
这样就可以避免空几何对象的错误了。
相关问题
file = gpd.read_file(r"E:\code-study\class-test\test.geojson") #获取每条线段的端点坐标 for index,row in file.iterrows(): if row["geometry"].geom_type == "MultiLineString": for line in row["geometry"].geoms: start = line.coords[0] end = line.coords[-1] print("Line start:", start) print("Line end", end)
这段代码是用来读取一个GeoJSON文件,并提取其中每条线段的起点和终点坐标。首先使用`gpd.read_file`函数读取文件,然后使用`iterrows`方法遍历每一行数据。如果当前行的几何类型是`MultiLineString`,则使用`geoms`属性获取其中的每条线段,并分别提取其起点和终点坐标,最后输出。
UPDATE goods_copy1 SET supplier_id = temp.supplier_id FROM goods_copy1 g1 INNER JOIN ( SELECT gpd.file_id, gpd.goods_id, im.supplier_id, g.supplier_id supplier_id2 FROM import_file im LEFT JOIN `goods_ppt_details` gpd ON gpd.file_id = im.file_id LEFT JOIN goods_copy1 g ON gpd.goods_id = g.goods_id WHERE gpd.tenant_id = 174 AND g.is_delete = '2' AND im.supplier_id != 0 AND g.supplier_id = 0 GROUP BY gpd.goods_id ) temp ON temp.goods_id = g1.goods_id这个执行不成功是什么问题
这个 SQL 语句的问题可能是在 `UPDATE` 子句中没有指定要更新的表别名。你可以尝试以下修改:
```
UPDATE goods_copy1 g1
SET g1.supplier_id = temp.supplier_id
FROM (
SELECT gpd.goods_id, im.supplier_id
FROM import_file im
LEFT JOIN goods_ppt_details gpd ON gpd.file_id = im.file_id
LEFT JOIN goods_copy1 g ON gpd.goods_id = g.goods_id
WHERE gpd.tenant_id = 174 AND g.is_delete = '2' AND im.supplier_id != 0 AND g.supplier_id = 0
GROUP BY gpd.goods_id
) temp
WHERE temp.goods_id = g1.goods_id;
```
这里使用了别名 `g1` 指定要更新的表,同时将子查询的结果用作临时表来更新 `goods_copy1` 表。
阅读全文