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

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

Reorganize sides/edges into their appropriate polygons

问题

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

import geopandas as gpd
import matplotlib.pyplot as plt

# 省略多边形的创建和绘制部分

# 创建线段的系列
lines = fp.geometry.apply(lambda x: list(map(
    LineString, 
    zip(x.boundary.coords[:-1], x.boundary.coords[1:]))
)).explode()

result = {line : list(fp.loc[
          (fp.geometry.touches(line)) 
          & (fp.geometry.intersection(line).length > 0), 
          'rf'].values)
          for line in lines}
result

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

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

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

list_ = []
for k, v in result.items():
    l = [l.tolist() for l in result[k]]
    if len(l) > 1:
        list_.append(sorted(set(l)))
    else:
        list_.append(l)
print(list_)

如何将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 reorganize the result (list_) into their appropriate polygons?

We have several polygons

import geopandas as gpd
import matplotlib.pyplot as plt

polys = gpd.GeoSeries([Polygon([(0,0), (2,0), (2, 1.5), (2,2), (0,2)]),
                     Polygon([(0,2), (2,2), (2,4), (0,4)]),
                     Polygon([(2,0), (5,0), (5,1.5), (2,1.5)]),
                     Polygon([(3,3), (5,3), (5,5), (3,5)])])

fp = gpd.GeoDataFrame({'geometry': polys, 'name': ['a', 'b', 'c', 'd'],
                      'grnd': [25, 25, 25, 25],
                      'rf': [29, 35, 26, 31]})

fig, ax = plt.subplots(figsize=(5, 5))
fp.plot(ax=ax, alpha=0.3, cmap='tab10', edgecolor='k',)
fp.apply(lambda x: ax.annotate(text=x['name'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1)
plt.show()

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

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

# Create a series of all the line segments
lines = fp.geometry.apply(lambda x: list(map(
    LineString, 
    zip(x.boundary.coords[:-1], x.boundary.coords[1:]))
)).explode()

result = {line : list(fp.loc[
          (fp.geometry.touches(line)) # line touches the polygon
          & (fp.geometry.intersection(line).length > 0), # And the intersection is more than just a point 
          'rf'].values)
          for line in lines}
result
{<LINESTRING (0 0, 2 0)>: [29],
 <LINESTRING (2 0, 2 1.5)>: [29, 26],
 <LINESTRING (2 1.5, 2 2)>: [29],
 <LINESTRING (2 2, 0 2)>: [29, 35],
 <LINESTRING (0 2, 0 0)>: [29],
 <LINESTRING (0 2, 2 2)>: [29, 35],
 <LINESTRING (2 2, 2 4)>: [35],
 <LINESTRING (2 4, 0 4)>: [35],
 <LINESTRING (0 4, 0 2)>: [35],
 <LINESTRING (2 0, 5 0)>: [26],
 <LINESTRING (5 0, 5 1.5)>: [26],
 <LINESTRING (5 1.5, 2 1.5)>: [26],
 <LINESTRING (2 1.5, 2 0)>: [29, 26],
 <LINESTRING (3 3, 5 3)>: [31],
 <LINESTRING (5 3, 5 5)>: [31],
 <LINESTRING (5 5, 3 5)>: [31],
 <LINESTRING (3 5, 3 3)>: [31]}

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

s = fp.geometry.apply(lambda x: len(x.exterior.coords)) - 1
s
0    5
1    4
2    4
3    4
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.

list_ = []
for k, v in result.items():
    l = [l.tolist() for l in result[k]]
    if len(l) > 1:
        list_.append(sorted(set(l)))
    else:
        list_.append(l)
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.

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

答案1

得分: 0

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

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

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

with warnings.catch_warnings():
    warnings.simplefilter(&#39;ignore&#39;)
    result = {poly_name: {
        &#39;sides&#39;: list(poly_sides),
        &#39;edges&#39;: [list(fp.loc[
            (fp.geometry.touches(line)) # line touches the polygons
            &amp; (fp.geometry.intersection(line).length &gt; 0), # And the intersection is more than just a point 
            &#39;rf&#39;].values.tolist())
                  for line in poly_sides]}
              for poly_name, poly_sides in lines.groupby(fp.name)}
result
{&#39;a&#39;: {&#39;sides&#39;: [&lt;LINESTRING (0 0, 2 0)&gt;,
   &lt;LINESTRING (2 0, 2 1.5)&gt;,
   &lt;LINESTRING (2 1.5, 2 2)&gt;,
   &lt;LINESTRING (2 2, 0 2)&gt;,
   &lt;LINESTRING (0 2, 0 0)&gt;],
  &#39;edges&#39;: [[29], [29, 26], [29], [29, 35], [29]]},
 &#39;b&#39;: {&#39;sides&#39;: [&lt;LINESTRING (0 2, 2 2)&gt;,
   &lt;LINESTRING (2 2, 2 4)&gt;,
   &lt;LINESTRING (2 4, 0 4)&gt;,
   &lt;LINESTRING (0 4, 0 2)&gt;],
  &#39;edges&#39;: [[29, 35], [35], [35], [35]]},
 &#39;c&#39;: {&#39;sides&#39;: [&lt;LINESTRING (2 0, 5 0)&gt;,
   &lt;LINESTRING (5 0, 5 1.5)&gt;,
   &lt;LINESTRING (5 1.5, 2 1.5)&gt;,
   &lt;LINESTRING (2 1.5, 2 0)&gt;],
  &#39;edges&#39;: [[26], [26], [26], [29, 26]]},
 &#39;d&#39;: {&#39;sides&#39;: [&lt;LINESTRING (3 3, 5 3)&gt;,
   &lt;LINESTRING (5 3, 5 5)&gt;,
   &lt;LINESTRING (5 5, 3 5)&gt;,
   &lt;LINESTRING (3 5, 3 3)&gt;],
  &#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:

确定