计算多边形kml的centorid

时间:2018-06-01 03:06:11

标签: geometry polygon kml computational-geometry

如果点分布不均匀,如何计算多边形的中心? 在下面kml文件中,30%的lat/lons位于一侧,因此平均值不会产生良好的结果。有没有人知道要实现它的好库?

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" 
xmlns:gx="http://www.google.com/kml/ext/2.2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.opengis.net/kml/2.2 
http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd 
http://www.google.com/kml/ext/2.2 
http://code.google.com/apis/kml/schema/kml22gx.xsd">
  <Document id="DLS_Parcels">
    <name>DLS_Parcels</name>
   <Snippet/>
<Folder id="FeatureLayer0">
  <name>DLS_Parcels</name>
  <Snippet/>
  <Placemark id="ID_00000">
    <name>189</name>
    <Snippet/>
    <description><![CDATA[<html><body><table border="1"><tr><th>Field Name</th><th>Field Value</th></tr><tr><td>OBJECTID</td><td>240106</td></tr><tr><td>SBPI_ID_NO</td><td>1076078</td></tr><tr><td>District Code</td><td>6</td></tr><tr><td>Municipality/Community Code</td><td>343</td></tr><tr><td>Quarter Code</td><td>1</td></tr><tr><td>Block Code</td><td>0</td></tr><tr><td>Parcel Number</td><td>189</td></tr><tr><td>Sheet</td><td>26</td></tr><tr><td>Plan</td><td>50</td></tr><tr><td>SRC_SL_CODE</td><td>5</td></tr><tr><td>SOURCE</td><td>DCDB</td></tr><tr><td>Parcel Area</td><td>2516</td></tr><tr><td>CadastralPlan</td><td>Null</td></tr></table></body></html>]]></description>
    <styleUrl>#PolyStyle00</styleUrl>
    <MultiGeometry>
      <Polygon>
        <extrude>0</extrude>
        <altitudeMode>clampToGround</altitudeMode>
        <tessellate>1</tessellate>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates> 32.394743,35.040539,0.000000 
32.394946,35.040474,0.000000 32.395228,35.040376,0.000000 
32.395274,35.040366,0.000000 32.395306,35.040379,0.000000 
32.395318,35.040406,0.000000 32.395308,35.040847,0.000000 
32.394855,35.040830,0.000000 32.394861,35.040659,0.000000 
32.394642,35.040669,0.000000 32.394506,35.040674,0.000000 
32.394505,35.040673,0.000000 32.394505,35.040672,0.000000 
32.394505,35.040671,0.000000 32.394504,35.040670,0.000000 
32.394504,35.040669,0.000000 32.394504,35.040668,0.000000 
32.394503,35.040667,0.000000 32.394503,35.040666,0.000000 
32.394503,35.040666,0.000000 32.394503,35.040665,0.000000 
32.394502,35.040664,0.000000 32.394502,35.040663,0.000000 
32.394502,35.040662,0.000000 32.394502,35.040661,0.000000 
32.394502,35.040660,0.000000 32.394502,35.040659,0.000000 
32.394501,35.040658,0.000000 32.394501,35.040657,0.000000 
32.394501,35.040656,0.000000 32.394501,35.040655,0.000000 
32.394501,35.040654,0.000000 32.394501,35.040653,0.000000 
32.394501,35.040652,0.000000 32.394501,35.040651,0.000000 
32.394501,35.040650,0.000000 32.394501,35.040649,0.000000 
32.394502,35.040649,0.000000 32.394502,35.040648,0.000000 
32.394502,35.040647,0.000000 32.394502,35.040646,0.000000 
32.394502,35.040645,0.000000 32.394502,35.040644,0.000000 
32.394503,35.040643,0.000000 32.394503,35.040642,0.000000 
32.394503,35.040641,0.000000 32.394503,35.040640,0.000000 
32.394504,35.040639,0.000000 32.394504,35.040638,0.000000 
32.394504,35.040637,0.000000 32.394505,35.040636,0.000000 
32.394505,35.040636,0.000000 32.394505,35.040635,0.000000 
32.394506,35.040634,0.000000 32.394506,35.040633,0.000000 
32.394507,35.040632,0.000000 32.394507,35.040631,0.000000 
32.394508,35.040630,0.000000 32.394508,35.040629,0.000000 
32.394509,35.040629,0.000000 32.394509,35.040628,0.000000 
32.394510,35.040627,0.000000 32.394510,35.040626,0.000000 
32.394511,35.040625,0.000000 32.394512,35.040624,0.000000 
32.394512,35.040624,0.000000 32.394513,35.040623,0.000000 
32.394514,35.040622,0.000000 32.394514,35.040621,0.000000 
32.394515,35.040621,0.000000 32.394516,35.040620,0.000000 
32.394516,35.040619,0.000000 32.394517,35.040618,0.000000 
32.394518,35.040618,0.000000 32.394519,35.040617,0.000000 
32.394519,35.040616,0.000000 32.394520,35.040616,0.000000 
32.394521,35.040615,0.000000 32.394522,35.040614,0.000000 
32.394523,35.040614,0.000000 32.394524,35.040613,0.000000 
32.394624,35.040579,0.000000 32.394743,35.040539,0.000000</coordinates>
            </LinearRing>
        </outerBoundaryIs>
      </Polygon>
    </MultiGeometry>
  </Placemark>
</Folder>
<Style id="PolyStyle00">
  <LabelStyle>
    <color>00000000</color>
    <scale>0.000000</scale>
  </LabelStyle>
  <LineStyle>
    <color>ff0000e6</color>
    <width>3.000000</width>
  </LineStyle>
  <PolyStyle>
    <color>00000000</color>
    <outline>1</outline>
  </PolyStyle>
</Style>
</Document>
</kml>

1 个答案:

答案 0 :(得分:2)

您可以使用geoPHP

这是一个例子

$kml = '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark>
    <name>The Pentagon</name>
    <Polygon>
      <extrude>1</extrude>
      <altitudeMode>relativeToGround</altitudeMode>
      <outerBoundaryIs>
        <LinearRing>
          <coordinates>
            -77.05788457660967,38.87253259892824,100 
            -77.05465973756702,38.87291016281703,100 
            -77.05315536854791,38.87053267794386,100 
            -77.05552622493516,38.868757801256,100 
            -77.05844056290393,38.86996206506943,100 
            -77.05788457660967,38.87253259892824,100
          </coordinates>
        </LinearRing>
      </outerBoundaryIs>
      <innerBoundaryIs>
        <LinearRing>
          <coordinates>
            -77.05668055019126,38.87154239798456,100 
            -77.05542625960818,38.87167890344077,100 
            -77.05485125901024,38.87076535397792,100 
            -77.05577677433152,38.87008686581446,100 
            -77.05691162017543,38.87054446963351,100 
            -77.05668055019126,38.87154239798456,100
          </coordinates>
        </LinearRing>
      </innerBoundaryIs>
    </Polygon>
  </Placemark>
</kml>';

$polygon = geoPHP::load($kml, 'kml');

$centroid = $polygon->getCentroid();

$centX = $centroid->getX();

$centY = $centroid->getY();