KML polygon, altitude mode destroys color - kml

I am attempting to generate a KML overlay. I have a grid I am mapping over an area, with colored squares. When I have the altitude mode as 'clampToGround' everything about the polygons I am generating, including their color, works as expected. However, it's not desirable to have the grid deform to map to the ground. When I change the altitude mode such that the overlay hovers a few meters off the ground, the shape of the grid sections is correct, but the color information is discarded and the grid sections are colored black.
Here is a testable excerpt of my KML code. Here is shows the colors correct but the grid deformed to the ground. Changing the value of altitudeMode to:
absolute produces the other behavior.
Is there a way to have this at the correct altitude AND with the correct color at the same time?
<?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">
<Document id="1">
<Document id="2">
<Style id="10">
<PolyStyle id="10">
<color>0xFF0000FF</color>
<colorMode>normal</colorMode>
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
</Style>
<name>Antenna Data</name>
<Camera>
<longitude>-122.30201334502573</longitude>
<latitude>37.87244901754293</latitude>
<altitude>200</altitude>
</Camera>
<Placemark id="5">
<name>region</name>
<styleUrl>#10</styleUrl>
<Polygon id="4">
<altitudeMode>clampToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing id="8">
<coordinates>-122.30243999999999,37.871973499999996,10 -122.30243999999999,37.8719265,10 -122.30238,37.8719265,10 -122.30238,37.871973499999996,10</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</Document>
</kml>

Your KML has several issues.
The polygon is missing the closing vertex. In order to be a valid KML polygon, the first coordinate set (lon,lat,alt) must be repeated as the last coordinate set, so that the polygon closes itself. For your example of a square, you would need 5 coordinate sets, with the first and the last being the same.
Try fixing that first, and if you still see coloring issues, try making sure that the vertex winding direction is correct (anti-clockwise). If it's backwards, then the darker "bottom" of the polygon will be facing upwards, and might appear dark/black. Yours appears correct from looking at the coordinates, but I could be mistaken.
Also, your color is not a valid KML color, as it appears to have an extra "0x" in front. A valid KML color needs four two-character values (each between 00 and ff), representing Alpha, Blue, Green, Red (AABBGGRR, and yes, it's backwards from HTML colors). A solid red would be: FF0000FF.
Below is an updated copy of your KML sample with altitudeMode = absolute, the closing vertex added, and the color corrected... this works for me in Earth Pro.
<?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">
<Document id="1">
<Document id="2">
<Style id="10">
<PolyStyle id="10">
<color>FF0000FF</color>
<colorMode>normal</colorMode>
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
</Style>
<name>Antenna Data</name>
<Camera>
<longitude>-122.30201334502573</longitude>
<latitude>37.87244901754293</latitude>
<altitude>200</altitude>
</Camera>
<Placemark id="5">
<name>region</name>
<styleUrl>#10</styleUrl>
<Polygon id="4">
<altitudeMode>absolute</altitudeMode>
<outerBoundaryIs>
<LinearRing id="8">
<coordinates>-122.30243999999999,37.871973499999996,10 -122.30243999999999,37.8719265,10 -122.30238,37.8719265,10 -122.30238,37.871973499999996,10 -122.30243999999999,37.871973499999996,10</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</Document>
</kml>

The solution I came across was that I could simple de-activate terrain in Google Earth. This seems to work now.

Related

Google kml gx:drawOrder

<?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">
<Document>
<Style id="Line1">
<LineStyle>
<color>ffff0000</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="Line2">
<LineStyle>
<color>ff00ff00</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="Poly1">
<PolyStyle>
<color>ff0000ff</color>
</PolyStyle>
</Style>
<Style id="Poly2">
<PolyStyle>
<color>ffffffff</color>
</PolyStyle>
</Style>
<Placemark>
<name>Line 1</name>
<styleUrl>#Line1</styleUrl>
<LineString>
<coordinates>
-112.265654928602,36.09447672602546,2357
-112.2660384528238,36.09342608838671,2357
-112.2668139013453,36.09251058776881,2357
-112.2677826834445,36.09189827357996,2357
-112.2688557510952,36.0913137941187,2357
-112.2694810717219,36.0903677207521,2357
-112.2695268555611,36.08932171487285,2357
-112.2690144567276,36.08850916060472,2357
-112.2681528815339,36.08753813597956,2357
-112.2670588176031,36.08682685262568,2357
-112.2657374587321,36.08646312301303,2357
</coordinates>
<gx:drawOrder>2</gx:drawOrder>
</LineString>
</Placemark>
<Placemark>
<name>Polygon 1</name>
<styleUrl>#Poly1</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-112.265654928602,36.09447672602546,2357
-112.2660384528238,36.09342608838671,2357
-112.2668139013453,36.09251058776881,2357
-112.2677826834445,36.09189827357996,2357
-112.2688557510952,36.0913137941187,2357
-112.2694810717219,36.0903677207521,2357
-112.2695268555611,36.08932171487285,2357
-112.2690144567276,36.08850916060472,2357
-112.2681528815339,36.08753813597956,2357
-112.2670588176031,36.08682685262568,2357
-112.2657374587321,36.08646312301303,2357
</coordinates>
</LinearRing>
</outerBoundaryIs>
<gx:drawOrder>4</gx:drawOrder>
</Polygon>
</Placemark>
<Placemark>
<name>Line 2</name>
<styleUrl>#Line2</styleUrl>
<LineString>
<coordinates>
-112.265654928602,36.09447672602546,2357
-112.2660384528238,36.09342608838671,2357
-112.2668139013453,36.09251058776881,2357
-112.2677826834445,36.09189827357996,2357
-112.2688557510952,36.0913137941187,2357
-112.2694810717219,36.0903677207521,2357
-112.2695268555611,36.08932171487285,2357
-112.2690144567276,36.08850916060472,2357
-112.2681528815339,36.08753813597956,2357
-112.2670588176031,36.08682685262568,2357
-112.2657374587321,36.08646312301303,2357
</coordinates>
<gx:drawOrder>4</gx:drawOrder>
</LineString>
</Placemark>
<Placemark>
<name>Polygon 2</name>
<styleUrl>#Poly2</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-112.265654928602,36.09447672602546,2357
-112.2660384528238,36.09342608838671,2357
-112.2668139013453,36.09251058776881,2357
-112.2677826834445,36.09189827357996,2357
-112.2688557510952,36.0913137941187,2357
-112.2694810717219,36.0903677207521,2357
-112.2695268555611,36.08932171487285,2357
-112.2690144567276,36.08850916060472,2357
-112.2681528815339,36.08753813597956,2357
-112.2670588176031,36.08682685262568,2357
-112.2657374587321,36.08646312301303,2357
</coordinates>
</LinearRing>
</outerBoundaryIs>
<gx:drawOrder>8</gx:drawOrder>
</Polygon>
</Placemark>
</Document>
</kml>
So As you can see in the above kml I've tried to use the gx:drawOrder element to sort the line strings & the polygons into drawing layers. BUT... if you load this in the kml interactive sampler or even Google Earth (free desktop edition) the geometry primitives only z order sort among "like primitives". That is, the line strings are sorted only against other line strings and the polygons are only sorted against other polygons.
In this case you can see that both "line 1" and "line 2" have draw orders of < "Polygon 2" & I as such I was expecting "polygon 2" to draw over the top of everything. But that is NOT the case.
In the end I've had to use altitude values to implement the z order that I want, but it's quite an unsatisfactory solution as most people view kml geometry using an isometric projection & the altitude values cause my geometry to skew.
What I'd like to know is this:
Is this the expected behavior of gx:drawOrder? The document around the element is sparse & it even says that it's only applicable to the line string, but according to the xsd is should be applicable to all geometry primitives (which it appears to be... well at least to polygons as well anyway...)
Is there a way to get the gx:drawOrder to be applicable over all
geometry primitives?
Thanks in advance!
The KML documentation only mentions <gx:drawOrder> with respect to line strings not polygons nor does it mention the behavior of drawOrder across different geometries (lines, rings, and polygons). The drawOrder support for Polygons is undocumented but was reported as an issue in the bug tracker.
You would naturally expect the drawOrder to work the same over all geometry primitives, but looks like Google Earth draws the features in groups by type: polygons, then ground overlays, followed by lines and point data where drawOrder is applied only within a group. ScreenOverlays are drawn last so they are always on top.
This means that a polygon with drawOrder=2 overlapping a line with drawOrder=1 still shows the polygon under the line. Also, even if a polygon has a higher value drawOrder and overlaps a GroundOverlay (aka Image Overlay), the polygon is drawn first and hidden under the GroundOverlay.
gx:DrawOrder is not part of the OGC KML 2.2 standard. It's a Google KML Extension so Google defines how it works. Also means Google can change how it works.

How do you make the color or shading on a polygon constant in kml?

I'd like to display spatial data in Google Earth and I think the best way to do it is with polygons. I'd like both the height of a polygon as well as its color to both represent the data. The height is no problem, but I can't figure out how to force the polygon to have a constant color and/or shading. If I set a polygon's color to blue (FFFF0000), it appears light blue when viewed from one direction, but dark blue from another. For example, in the code below I have two polygons at 90 degrees to each other. Both of them should be the same color. However, when viewed in Google Earth, they clearly have a different color (or perhaps just different shading because they are at a different angle with respect to the viewer). Presumably this is by design, but I'd like to make polygons that have a constant color (or shading), irrespective of viewing angle. Does anybody know if this can be done?
Thanks
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2">
<Document>
<name>kml toolbox tests</name>
<Folder>
<name>kml.poly3</name>
<id>kml.poly3</id>
<Placemark id="kml_poly3tpfc275749_9c60_474d_b419_d29a903da8be">
<name>30</name>
<visibility>1</visibility>
<description/>
<Style>
<LineStyle>
<color>FFFFFFFF</color>
<width>5</width>
</LineStyle>
<PolyStyle>
<color>FFFF0000</color>
</PolyStyle>
</Style>
<Polygon id="Polygon_kml_poly3tpfc275749_9c60_474d_b419_d29a903da8be">
<extrude>0</extrude>
<tesselate>1</tesselate>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing id="LinearRing_kml_poly3tpfc275749_9c60_474d_b419_d29a903da8be">
<coordinates>-111.98,40.6,0
-111.97,40.6,0
-111.97,40.6,1500
-111.99,40.6,1500
-111.99,40.6,0 </coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Placemark id="kml_poly3tpb12d0de1_b554_45ed_b9fd_63d120582f23">
<name>31</name>
<visibility>1</visibility>
<description/>
<Style>
<LineStyle>
<color>FFFFFFFF</color>
<width>5</width>
</LineStyle>
<PolyStyle>
<color>FFFF0000</color>
</PolyStyle>
</Style>
<Polygon id="Polygon_kml_poly3tpb12d0de1_b554_45ed_b9fd_63d120582f23">
<extrude>0</extrude>
<tesselate>1</tesselate>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing id="LinearRing_kml_poly3tpb12d0de1_b554_45ed_b9fd_63d120582f23">
<coordinates>-111.97,40.6,0
-111.97,40.62,0
-111.97,40.62,2000
-111.97,40.6,2000
-111.97,40.6,0 </coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Camera id="ID">
<TimePrimitive>...</TimePrimitive>
<gx:ViewerOptions>
<option> name="camera" type="boolean"></option>
</gx:ViewerOptions>
<longitude>-111.94</longitude> <!-- kml:angle180 -->
<latitude>40.53</latitude> <!-- kml:angle90 -->
<altitude>7500</altitude> <!-- double -->
<heading>340</heading> <!-- kml:angle360 -->
<tilt>60</tilt> <!-- kml:anglepos180 -->
<roll>0</roll> <!-- kml:angle180 -->
<altitudeMode>absolute</altitudeMode>
</Camera>
</Folder>
</Document>
</kml>
This polygon shading issue is much worse on more recent versions of Google Earth. In recent versions a comment was added saying that now only one side of polygons is rendered so my next step will be to place polygons back to back with opposite point order rotation. This way, all polygons viewed from the sun angle direction can appear constantly shaded at least.
The KML standard as well as Google Earth doesn't allow you to specify advanced rendering options such shading options. The current Google Earth KML extensions do not allow this to be changed and setting slightly transparent color also has no effect.
If the viewing angle and the face of polygon are perpendicular to each other then the color is at maximum but as you the angle tilts and angle approaches 0 degrees then the color blackens. At less than 5-10 degrees the color is nearly completely black.
Only get uniform coloring if polygons are on the ground and view is looking straight down. That allows you to create heat map visualizations.
To request a new feature you can try the following steps:
Click "Send Feedback" under the Help menu and fill out a detailed
response to Google.
Start a discussion of the feature in the Google Earth forum.
Submit feature request via https://code.google.com/p/earth-issues/issues/list

Can Draw Order of Polygons Be Controlled in KML?

I have adjacent polygons (clampToGround) where a portion of the boundaries overlap. I would like to be able to control which one appears on top. The only method I've found to work thus far is to list the polygon placemarks in stack order (last on list appears on top). However, I'd like to organize the placemarks based on other logic and would like a coding method to specify which polygon is on top.
Note, I've also tried using relativeToGround and small elevation differences, but that leads to potions of the polygons fill to disappear beneath the topography.
You can specify <gx:drawOrder> element (which is a Google Earth KML extension) to order polygons as well as lines and rings in your KML.
Note the documentation (see below) only specifies LineStrings but also works for Lines, LinearRings, and Polygons. Anywhere "LineStrings" is mentioned below just replace it with Lines, Rings, and Polygons. The drawOrder support for Polygons is undocumented but was reported as an issue.
KML Documentation:
<gx:drawOrder>
"An integer value that specifies the order for drawing multiple line
strings. LineStrings drawn first may be partially or fully obscured by LineStrings with a later (or higher) draw order. This element may be required in
conjunction with the <gx:outerColor> and <gx:outerWidth> elements in
<LineStyle> when dual-colored lines cross each other."
Note this will only work in clients that support the Google Earth KML extensions.
In example below, the Red polygon is drawn over the blue polygon. To make the blue polygon draw over the red one change its drawOrder value to a higher number.
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>
<Placemark>
<name>Red Polygon</name>
<description>gx:drawOrder=3</description>
<Style>
<LineStyle>
<color>ff000000</color>
</LineStyle>
<PolyStyle>
<color>ff0000ff</color>
</PolyStyle>
</Style>
<Polygon>
<gx:drawOrder>3</gx:drawOrder>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
14.456906,37.345497,0 14.949769,37.346531,0
14.960918,37.987563,0 14.45089,37.987521,0
14.456906,37.3455,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Placemark>
<name>Blue Polygon</name>
<description>gx:drawOrder=2</description>
<Style>
<LineStyle>
<color>ff000000</color>
</LineStyle>
<PolyStyle>
<color>ffff0000</color>
</PolyStyle>
</Style>
<Polygon>
<gx:drawOrder>2</gx:drawOrder>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
14.83626,38.016863,0 14.835535,37.645589,0
15.47025,37.589266,0 15.470457,38.019158,0
14.83626,38.016862,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>

how to keep polygon and name of the polygon over the polygon surface in kml

I have asked to keep polygon, name of the polygon and the corresponding icon in the places panel.
Can any suggest me how to do this in kml?
By default you only see the name label if your placemark has a Point geometry so for lines and polygons the name is not shown.
You can however create a MultiGeometry with both a Polygon and a Point (typically the center point or where you want to label to appear near). If you don't want the default yellow pushpin to appear just add a Style with an empty IconStyle href element to the Placemark.
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>polygon</name>
<Style>
<IconStyle>
<Icon/>
</IconStyle>
</Style>
<MultiGeometry>
<Point>
<coordinates>-122.4317195,37.801848</coordinates>
</Point>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-122.43193945401,37.801983684521
-122.431564131101,37.8020327731402
-122.431499536494,37.801715236748
-122.43187136387,37.8016634915437
-122.43193945401,37.801983684521
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</kml>
This multi-geometry method can be HIGHLY tedious if you have a couple hundred polygons (say, a grid/index) and you want to show their names.
My preference is to use something like QGIS/Arc, where I create a new vector file of polygon centroids (point file) that has the polygon attributes (including name/description). I save the new point file as a point KML and import into Google Earth. From their, I set a shared style for the point file, with the icon set to 0% opacity.
Example of Google Earth Polygons with Labels using above method

delaying balloon component in a KML code

I have just started to learn how to manipulate the KML files. here is a problem that I do not know how to overcome.... I created a polygon and added a description to be shown as a balloon, but it appears as soon as Google Earth starts working before the polygon can be seen. what I want is to show the balloon from a specific distance where the polygon appears(for example in the range distance of LookAt element).
anybody knows how to manage that?
my code follows:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" mlns: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">
<LookAt>
<longitude>17.99514610290434</longitude>
<latitude>59.36293893395309</latitude>
<altitude>0</altitude>
<range>597.51212259908</range>
<tilt>52.34415598649959</tilt>
<heading>105.3974737169693</heading>
</LookAt>
<Placemark>
<name>Stadium</name>
<description>
<![CDATA[
this is <b><i>RĂ¥sunda soccer stadium</i></b>
]]>
</description>
<gx:balloonVisibility>1</gx:balloonVisibility>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Polygon>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
17.99514610290434,59.36293893395309,100 17.99651951950199,59.36209399425741,100 17.99752330705672,59.36252751885282,100 17.99613146514916,59.36335387902954,100 17.99514610290434,59.36293893395309,100
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
Normally you can skip rendering the polygon until you get "close" enough to it as defined by a Region element which is determined by calculating when a given area maps to a min or max # of pixels.
Also, the <gx:balloonVisibility> tag forces the description balloon to appear when the KML is loaded regardless of whether Region is active. Adding a Region direct in the KML still shows the popup balloon.
To do what you want to do, you must wrap the KML file with a second KML file with a NetworkLink with a Region that loads the seconds KML only when the region is active (aka close enough) at which time the description is displayed along with the polygon.
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<LookAt>
<longitude>17.99514610290434</longitude>
<latitude>59.36293893395309</latitude>
<altitude>0</altitude>
<heading>105.3974737169693</heading>
<tilt>52.34415598649959</tilt>
<range>597.51212259908</range>
</LookAt>
<NetworkLink>
<Region>
<LatLonAltBox>
<north>59.363792</north>
<south>59.361556</south>
<east>17.998029</east>
<west>17.994443</west>
</LatLonAltBox>
<Lod>
<minLodPixels>128</minLodPixels>
<maxLodPixels>-1</maxLodPixels>
</Lod>
</Region>
<Link>
<href>target.kml</href>
</Link>
</NetworkLink>
</Document>
</kml>
And target.kml file contains the original KML you gave:
<?xml version="1.0" encoding="ISO-8859-1"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Stadium</name>
...
</Placemark>
</kml>
You can change the distance the feature + balloon appears by adjusting the minLodPixels value and/or the size of the region. At present the feature will display when the region defined by area surrounding the polygon is at least 128 pixels on the screen. Change to 32 or 64 and/or make the region area larger to make it appear quicker.
Note Google Earth client doesn't give you a tool to edit or even see the Region bounding boxes on the map so debugging this is tricky. You can paste your KML into this tool to generate KML making the Region bounding area visible. This helps to debug Regions more easily.

Resources