重组边缘/边角到它们相应的多边形。

huangapple go评论193阅读模式
英文:

Reorganize sides/edges into their appropriate polygons

问题

如何将结果(list_)重新组织为它们相应的多边形?我们有几个多边形。

  1. import geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. # 省略多边形的创建和绘制部分
  4. # 创建线段的系列
  5. lines = fp.geometry.apply(lambda x: list(map(
  6. LineString,
  7. zip(x.boundary.coords[:-1], x.boundary.coords[1:]))
  8. )).explode()
  9. result = {line : list(fp.loc[
  10. (fp.geometry.touches(line))
  11. & (fp.geometry.intersection(line).length > 0),
  12. 'rf'].values)
  13. for line in lines}
  14. result

可能确定每个多边形有多少边/边缘:

  1. s = fp.geometry.apply(lambda x: len(x.exterior.coords)) - 1
  2. s

我可以将属性组合(去重),但不知道如何将边/边缘分组到它们的多边形中。

  1. list_ = []
  2. for k, v in result.items():
  3. l = [l.tolist() for l in result[k]]
  4. if len(l) > 1:
  5. list_.append(sorted(set(l)))
  6. else:
  7. list_.append(l)
  8. print(list_)

如何将list_分组到它们的多边形中,其中一个列表(从每个边缘处上升一级)是一个多边形?类似于以下结构。

  1. [[[29], [26, 29], [29], [29, 35], [29]],
  2. [[29, 35], [35], [35], [35]],
  3. [[26], [26], [26], [26, 29]],
  4. [[31], [31], [31], [31]]
  5. ]
英文:

How do I reorganize the result (list_) into their appropriate polygons?

We have several polygons

  1. import geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. polys = gpd.GeoSeries([Polygon([(0,0), (2,0), (2, 1.5), (2,2), (0,2)]),
  4. Polygon([(0,2), (2,2), (2,4), (0,4)]),
  5. Polygon([(2,0), (5,0), (5,1.5), (2,1.5)]),
  6. Polygon([(3,3), (5,3), (5,5), (3,5)])])
  7. fp = gpd.GeoDataFrame({'geometry': polys, 'name': ['a', 'b', 'c', 'd'],
  8. 'grnd': [25, 25, 25, 25],
  9. 'rf': [29, 35, 26, 31]})
  10. fig, ax = plt.subplots(figsize=(5, 5))
  11. fp.plot(ax=ax, alpha=0.3, cmap='tab10', edgecolor='k',)
  12. fp.apply(lambda x: ax.annotate(text=x['name'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1)
  13. plt.show()

重组边缘/边角到它们相应的多边形。

And we want to harvest some attribute (grnd or name or rf) on either side of an edge.

  1. # Create a series of all the line segments
  2. lines = fp.geometry.apply(lambda x: list(map(
  3. LineString,
  4. zip(x.boundary.coords[:-1], x.boundary.coords[1:]))
  5. )).explode()
  6. result = {line : list(fp.loc[
  7. (fp.geometry.touches(line)) # line touches the polygon
  8. & (fp.geometry.intersection(line).length > 0), # And the intersection is more than just a point
  9. 'rf'].values)
  10. for line in lines}
  11. result
  1. {<LINESTRING (0 0, 2 0)>: [29],
  2. <LINESTRING (2 0, 2 1.5)>: [29, 26],
  3. <LINESTRING (2 1.5, 2 2)>: [29],
  4. <LINESTRING (2 2, 0 2)>: [29, 35],
  5. <LINESTRING (0 2, 0 0)>: [29],
  6. <LINESTRING (0 2, 2 2)>: [29, 35],
  7. <LINESTRING (2 2, 2 4)>: [35],
  8. <LINESTRING (2 4, 0 4)>: [35],
  9. <LINESTRING (0 4, 0 2)>: [35],
  10. <LINESTRING (2 0, 5 0)>: [26],
  11. <LINESTRING (5 0, 5 1.5)>: [26],
  12. <LINESTRING (5 1.5, 2 1.5)>: [26],
  13. <LINESTRING (2 1.5, 2 0)>: [29, 26],
  14. <LINESTRING (3 3, 5 3)>: [31],
  15. <LINESTRING (5 3, 5 5)>: [31],
  16. <LINESTRING (5 5, 3 5)>: [31],
  17. <LINESTRING (3 5, 3 3)>: [31]}

It's possible to determine the number of sides/edges each polygon has:

  1. s = fp.geometry.apply(lambda x: len(x.exterior.coords)) - 1
  2. s
  3. 0 5
  4. 1 4
  5. 2 4
  6. 3 4
  7. Name: geometry, dtype: int64

I can combine (without duplicates) and sort the attributes but do not know how to group the sides/edges into their polygons.

  1. list_ = []
  2. for k, v in result.items():
  3. l = [l.tolist() for l in result[k]]
  4. if len(l) > 1:
  5. list_.append(sorted(set(l)))
  6. else:
  7. list_.append(l)
  8. print(list_)

[[29], [26, 29], [29], [29, 35], [29], [29, 35], [35], [35], [35], [26], [26], [26], [26, 29], [31], [31], [31], [31]]

How do I group list_ into their polygons; where a list (one level up from each edge) is a polygon? Something like.

  1. [[[29], [26, 29], [29], [29, 35], [29]],
  2. [[29, 35], [35], [35], [35]],
  3. [[26], [26], [26], [26, 29]],
  4. [[31], [31], [31], [31]]]

答案1

得分: 0

在原始创建“具有公共边缘的”字典的函数内重新排列:

  1. with warnings.catch_warnings():
  2. warnings.simplefilter('ignore')
  3. result = {poly_name: {
  4. 'sides': list(poly_sides),
  5. 'edges': [list(fp.loc[
  6. (fp.geometry.touches(line)) # 线与多边形相接触
  7. & (fp.geometry.intersection(line).length > 0), # 交点不仅仅是一个点
  8. 'rf'].values.tolist())
  9. for line in poly_sides]}
  10. for poly_name, poly_sides in lines.groupby(fp.name)}
  11. result
  1. {'a': {'sides': [<LINESTRING (0 0, 2 0)>,
  2. <LINESTRING (2 0, 2 1.5)>,
  3. <LINESTRING (2 1.5, 2 2)>,
  4. <LINESTRING (2 2, 0 2)>,
  5. <LINESTRING (0 2, 0 0)>],
  6. 'edges': [[29], [29, 26], [29], [29, 35], [29]]},
  7. 'b': {'sides': [<LINESTRING (0 2, 2 2)>,
  8. <LINESTRING (2 2, 2 4)>,
  9. <LINESTRING (2 4, 0 4)>,
  10. <LINESTRING (0 4, 0 2)>],
  11. 'edges': [[29, 35], [35], [35], [35]]},
  12. 'c': {'sides': [<LINESTRING (2 0, 5 0)>,
  13. <LINESTRING (5 0, 5 1.5)>,
  14. <LINESTRING (5 1.5, 2 1.5)>,
  15. <LINESTRING (2 1.5, 2 0)>],
  16. 'edges': [[26], [26], [26], [29, 26]]},
  17. 'd': {'sides': [<LINESTRING (3 3, 5 3)>,
  18. <LINESTRING (5 3, 5 5)>,
  19. <LINESTRING (5 5, 3 5)>,
  20. <LINESTRING (3 5, 3 3)>],
  21. 'edges': [[31], [31], [31], [31]]}}
英文:

Reorder within the original function that creates the 'sides with common edges' dict

  1. with warnings.catch_warnings():
  2. warnings.simplefilter(&#39;ignore&#39;)
  3. result = {poly_name: {
  4. &#39;sides&#39;: list(poly_sides),
  5. &#39;edges&#39;: [list(fp.loc[
  6. (fp.geometry.touches(line)) # line touches the polygons
  7. &amp; (fp.geometry.intersection(line).length &gt; 0), # And the intersection is more than just a point
  8. &#39;rf&#39;].values.tolist())
  9. for line in poly_sides]}
  10. for poly_name, poly_sides in lines.groupby(fp.name)}
  11. result
  1. {&#39;a&#39;: {&#39;sides&#39;: [&lt;LINESTRING (0 0, 2 0)&gt;,
  2. &lt;LINESTRING (2 0, 2 1.5)&gt;,
  3. &lt;LINESTRING (2 1.5, 2 2)&gt;,
  4. &lt;LINESTRING (2 2, 0 2)&gt;,
  5. &lt;LINESTRING (0 2, 0 0)&gt;],
  6. &#39;edges&#39;: [[29], [29, 26], [29], [29, 35], [29]]},
  7. &#39;b&#39;: {&#39;sides&#39;: [&lt;LINESTRING (0 2, 2 2)&gt;,
  8. &lt;LINESTRING (2 2, 2 4)&gt;,
  9. &lt;LINESTRING (2 4, 0 4)&gt;,
  10. &lt;LINESTRING (0 4, 0 2)&gt;],
  11. &#39;edges&#39;: [[29, 35], [35], [35], [35]]},
  12. &#39;c&#39;: {&#39;sides&#39;: [&lt;LINESTRING (2 0, 5 0)&gt;,
  13. &lt;LINESTRING (5 0, 5 1.5)&gt;,
  14. &lt;LINESTRING (5 1.5, 2 1.5)&gt;,
  15. &lt;LINESTRING (2 1.5, 2 0)&gt;],
  16. &#39;edges&#39;: [[26], [26], [26], [29, 26]]},
  17. &#39;d&#39;: {&#39;sides&#39;: [&lt;LINESTRING (3 3, 5 3)&gt;,
  18. &lt;LINESTRING (5 3, 5 5)&gt;,
  19. &lt;LINESTRING (5 5, 3 5)&gt;,
  20. &lt;LINESTRING (3 5, 3 3)&gt;],
  21. &#39;edges&#39;: [[31], [31], [31], [31]]}}

huangapple
  • 本文由 发表于 2023年3月21日 02:13:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定