代码之家  ›  专栏  ›  技术社区  ›  mikezang

如何在gpd中绘制LinsString。GeoDataFrame?

  •  0
  • mikezang  · 技术社区  · 1 年前

    我有一个kml文件,里面有LinrString和Polygon,我可以绘制多边形,但我不知道如何绘制LineString,我试过了 polys.boundary.plot(ax=axs, facecolor=color, color=color, label=legend, zorder=3) ,这似乎不起作用,我如何在gpd中绘制LineString。GeoDataFrame?

    myPolys = gpd.read_file('myKmkFile.kml', driver='KML')
    myPolys.exterior.plot(ax=axs, facecolor=color, color=color, label=legend, zorder=3)
    

    enter image description here KML文件的部分

    <Placemark id="3">
        <name>ZSHA/A2585/19</name>
        <styleUrl>#4</styleUrl>
        <LineString id="2">
            <coordinates>121.40909651396505,34.89924748390615,0.0 121.40799622296342,34.88157892127923,0.0 121.40480056606769,34.864084191851276,0.0 121.39954160563234,34.846931616598226,0.0 121.39227117062804,34.83028612270202,0.0 121.38306031244876,34.81430766540144,0.0 121.37199858143134,34.799149701081674,0.0 121.35919313252089,34.784957725778504,0.0 121.34476766987393,34.77186789253947,0.0 121.32886124141642,34.76000572024479,0.0 121.31162689546245,34.74948490555849,0.0 121.29323021245763,34.740406248662744,0.0 121.27384772574081,34.732856702339355,0.0 121.25366524592614,34.72690855280921,0.0 121.23287610409945,34.72261873953367,0.0 121.21167932950523,34.72002831993127,0.0 121.19027777777801,34.71916208367624,0.0 121.16887622605077,34.72002831993127,0.0 121.14767945145657,34.72261873953367,0.0 121.12689030962986,34.72690855280921,0.0 121.10670782981519,34.732856702339355,0.0 121.08732534309837,34.740406248662744,0.0 121.06892866009355,34.74948490555849,0.0 121.05169431413958,34.76000572024479,0.0 121.03578788568207,34.77186789253947,0.0 121.02136242303511,34.784957725778504,0.0 121.00855697412466,34.799149701081674,0.0 120.99749524310725,34.81430766540144,0.0 120.98828438492797,34.83028612270202,0.0 120.98101394992366,34.846931616598226,0.0 120.97575498948831,34.864084191851276,0.0 120.97255933259258,34.88157892127923,0.0 120.97145904159095,34.89924748390615,0.0 120.97246605425434,34.91691977955479,0.0 120.97557201734512,34.93442556459562,0.0 120.98074831530587,34.95159609320779,0.0 120.98794629578126,34.96826574829586,0.0 120.99709769174416,34.9842736461484,0.0 121.0081152379774,34.99946519902468,0.0 121.02089347758957,35.01369362012356,0.0 121.03530975214778,35.02682135582214,0.0 121.05122536692012,35.03872143067552,0.0 121.06848692066855,35.04927869143998,0.0 121.08692778745268,35.05839093731496,0.0 121.10636973603305,35.06596992468892,0.0 121.1266246707295,35.071942235909546,0.0 121.14749647603564,35.0762500029669,0.0 121.16878294593866,35.078851478464635,0.0 121.19027777777801,35.079721447840576,0.0 121.21177260961736,35.078851478464635,0.0 121.23305907952036,35.0762500029669,0.0 121.2539308848265,35.071942235909546,0.0 121.27418581952296,35.06596992468892,0.0 121.29362776810332,35.058390937314954,0.0 121.31206863488747,35.04927869143999,0.0 121.32933018863588,35.038721430675515,0.0 121.34524580340822,35.02682135582214,0.0 121.35966207796643,35.01369362012357,0.0 121.37244031757862,34.99946519902468,0.0 121.38345786381184,34.9842736461484,0.0 121.39260925977474,34.96826574829587,0.0 121.39980724025013,34.95159609320778,0.0 121.40498353821089,34.93442556459563,0.0 121.40808950130166,34.91691977955479,0.0 121.40909651396505,34.89924748390615,0.0</coordinates>
        </LineString>
    </Placemark>
    <Placemark>
      <name>ZSHA/A2637/19</name>
      <styleUrl>#poly-3949AB-1-255-nodesc</styleUrl>
      <Polygon>
        <outerBoundaryIs>
          <LinearRing>
            <tessellate>1</tessellate>
            <coordinates>
              124,33.6805556,0
              123.6288889,33.2355556,0
              124.5986522,32.6877416,0
              124.9666942,33.1189613,0
              124,33.6805556,0
            </coordinates>
          </LinearRing>
        </outerBoundaryIs>
      </Polygon>
    </Placemark>
    
    1 回复  |  直到 1 年前
        1
  •  1
  •   Timeless    1 年前

    你的 KML 具有混合几何形状。至少有一个 LineString 和一个 Polygon :

                Name description timestamp  ... drawOrder  icon                  geometry
    0  ZSHA/A2585/19        None       NaT  ...      None  None  LINESTRING Z (121.409...
    1  ZSHA/A2637/19        None       NaT  ...      None  None  POLYGON Z ((124 33.68...
    

    尽管如此,不需要计算边界或外部,就可以将所有几何图形都放在同一个图形中 myPolys.plot(ax=ax) :

    enter image description here

    或者,如果你想对情节有更多的控制权,可以分两步进行分解:

    # is it a LineString ?
    is_ls = myPolys.geom_type.eq("LineString")
    
    fig, ax = plt.subplots()
    
    myPolys.loc[is_ls].plot(color="lightcoral", ax=ax)
    myPolys.loc[~is_ls].plot(color="lightgreen", ec="k", ax=ax)
    

    enter image description here