Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
How to calculate centre of polygon when points are distributed unevenly ?
In below kml file 30% of lat/lons are on one side hence average does not produce a good result. Does any one knows any good libraries to achieve it ?
<?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>
You can utilize geoPHP
Here is an example
$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();
I've created a geologic units map. Each geologic unit is composed of multiple placemark polygons. I currently have the same geologic unit description repeated in each placemark composing the geologic unit in order to get the functionality I want (click on the polygon and get a description).
Is there a way to include the geologic unit description once in the KML file and use it on multiple polygons?
Here's an example of what I currently have:
<?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:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Geologic Units</name>
<open>1</open>
<Style id="style22">
<LineStyle>
<color>40000000</color>
</LineStyle>
<PolyStyle>
<color>996ca66c</color>
</PolyStyle>
</Style>
<Folder>
<name>Cokeville Formation (Lower Cretaceous)</name>
<Placemark>
<name>Cokeville Formation</name>
<description><![CDATA[<p>UNITSYMBOL: Kc</p><p>UNITNAME: Cokeville Formation</p><p>AGE: Lower Cretaceous</p><p>Description: Interbedded dark-gray, carbonaceous, shaly mudstone and siltstone, tan-weathering sandstone, and gray to tan limestone and coquina containing gastropod (Pyrgulifera) and pelecypod fauna. hickness about 650 m. Mapped only along southeastern edge of map area</p>]]></description>
<styleUrl>#style22</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
-111.001621832,41.6548237803,0 -111.000744207,41.6560784513,0 -111.00074419,41.6548059965,0 -111.001123716,41.6547959594,0 -111.001621832,41.6548237803,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Placemark>
<name>Cokeville Formation</name>
<description><![CDATA[<p>UNITSYMBOL: Kc</p><p>UNITNAME: Cokeville Formation</p><p>AGE: Lower Cretaceous</p><p>Description: Interbedded dark-gray, carbonaceous, shaly mudstone and siltstone, tan-weathering sandstone, and gray to tan limestone and coquina containing gastropod (Pyrgulifera) and pelecypod fauna. hickness about 650 m. Mapped only along southeastern edge of map area</p>]]></description>
<styleUrl>#style22</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
-111.010074925,41.6544088836,0 -111.009817106,41.6540759648,0 -111.009537791,41.6538429717,0 -111.009365835,41.6535267043,0 -111.009150955,41.6532271007,0 -111.008914576,41.653010725,0 -111.008549317,41.6527943596,0 -111.00861367,41.6521452029,0 -111.008506241,41.6516790986,0 -111.008466501,41.6515573391,0 -111.009350608,41.6513600036,0 -111.01081154,41.6510769006,0 -111.011498939,41.6510102244,0 -111.011406521,41.6517787096,0 -111.011019912,41.6519285808,0 -111.010590221,41.6523613799,0 -111.01028951,41.6528275229,0 -111.010246703,41.6535765651,0 -111.010160817,41.6540093659,0 -111.010074925,41.6544088836,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Folder>
</Document>
</kml>
I figured out how to do it with BalloonStyle Text. Is this the only way or is there another/better way?
<?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:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Geologic Units</name>
<open>1</open>
<Style id="style22">
<BalloonStyle>
<text><![CDATA[<p>UNITSYMBOL: Kc</p><p>UNITNAME: Cokeville Formation</p><p>AGE: Lower Cretaceous</p><p>Description: Interbedded dark-gray, carbonaceous, shaly mudstone and siltstone, tan-weathering sandstone, and gray to tan limestone and coquina containing gastropod (Pyrgulifera) and pelecypod fauna. hickness about 650 m. Mapped only along southeastern edge of map area</p>]]></text>
</BalloonStyle>
<LineStyle>
<color>40000000</color>
</LineStyle>
<PolyStyle>
<color>996ca66c</color>
</PolyStyle>
</Style>
<Folder>
<name>Cokeville Formation (Lower Cretaceous)</name>
<Placemark>
<name>Cokeville Formation</name>
<styleUrl>#style22</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
-111.001621832,41.6548237803,0 -111.000744207,41.6560784513,0 -111.00074419,41.6548059965,0 -111.001123716,41.6547959594,0 -111.001621832,41.6548237803,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Placemark>
<name>Cokeville Formation</name>
<styleUrl>#style22</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
-111.010074925,41.6544088836,0 -111.009817106,41.6540759648,0 -111.009537791,41.6538429717,0 -111.009365835,41.6535267043,0 -111.009150955,41.6532271007,0 -111.008914576,41.653010725,0 -111.008549317,41.6527943596,0 -111.00861367,41.6521452029,0 -111.008506241,41.6516790986,0 -111.008466501,41.6515573391,0 -111.009350608,41.6513600036,0 -111.01081154,41.6510769006,0 -111.011498939,41.6510102244,0 -111.011406521,41.6517787096,0 -111.011019912,41.6519285808,0 -111.010590221,41.6523613799,0 -111.01028951,41.6528275229,0 -111.010246703,41.6535765651,0 -111.010160817,41.6540093659,0 -111.010074925,41.6544088836,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Folder>
</Document>
</kml>
For your use-case, a shared Style with a BalloonStyle containing a balloon template is probably the best way to go. You can take this a step further and include placeholder variables in the BalloonStyle template, which get replaced by contents of the ExtendedData section in each Placemark. That said, another way it can be done is to combine the relevant polygons into one Placemark, using a MultiGeometry, but only if you want the combined polygons to act as one on the map.
I have KML code that will fill a polygon. I do not want the polygon filled. I have 0 and 1 but still displays a filled polygon. The polygon is approximately 10m radius. Is this so small that the line width of 1.5 is actually causing the polygon to be filled?
Here is the KML (I stripped it to only 1 point rather than the multiple of the original source)(this is operational so you should be able to copy it and see what I mean):
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://earth.google.com/kml/2.1'>
<Document>
<name>
CO0xxxxxx
</name>
<open>1</open>
<Style id='a'>
<IconStyle>
<Icon>
http://maps.google.com/mapfiles/kml/paddle/grn-blank.png
</Icon>
</IconStyle>
</Style>
<Folder>
<open>0</open>
<name>Names</name>
<Placemark>
<name>WELL NO 2</name>
<description><![CDATA[]]></description>
<Point>
<altitudeMode>clampToGround</altitudeMode>
<coordinates>-107.676117, 39.586253,0</coordinates>
</Point>
</Placemark>
<open>0</open>
<name>Polygons on-off</name>
<Placemark>
<Style>
<IconStyle>
<Icon>
<href>root://icons/palette-s.png</href>
<x>224</x>
<y>224</y>
<w>32</w>
<h>32</h>
</Icon>
</IconStyle>
<PolyStyle>
<Fill>0</Fill>
<Outline>1</Outline>
</PolyStyle>
<LineStyle>
<Color>ff0000ff</Color>
<Width>1.5</Width>
</LineStyle>
</Style>
<Polygon>
<extrude>0</extrude>
<altitudeMode>clampToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-107.676029,39.586323,0
-107.675996,39.586227,0
-107.676085,39.586158,0
-107.676205,39.586183,0
-107.676238,39.586279,0
-107.676149,39.586348,0
-107.676029,39.586323,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Folder>
</Document>
</kml>
KML is a dialect of XML. Tags are case sensitive.
(from the posted KML)
<PolyStyle>
<Fill>0</Fill>
<Outline>1</Outline>
</PolyStyle>
is not the same as:
<PolyStyle>
<fill>0</fill>
<outline>1</outline>
</PolyStyle>
(updated KML)
KML Reference