Give label to KML polygon

987 views
Skip to first unread message

burnin...@gmail.com

unread,
Jun 12, 2017, 5:44:16 PM6/12/17
to cesium-dev
I have been trying to figure out how to add a label to a polygon from a KML file, but I'm having no luck. Since the polygon doesn't have a position attribute, I'm unsure how to place the labels, since I want them approximately in the center of the polygons.

Scott Reynolds

unread,
Jun 13, 2017, 9:14:39 AM6/13/17
to cesium-dev, burnin...@gmail.com
You have to specify both a Polygon and a Point inside a MultiGeometry.

<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Style id="polygonStyle">
<IconStyle>
<!-- Hide the pushpin -->>
<scale>0.0</scale>
</IconStyle>
<LabelStyle>
<color>ffffff00</color>
<colorMode>normal</colorMode>
</LabelStyle>
<PolyStyle>
<color>4400ff00</color>
<colorMode>normal</colorMode>
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
</Style>
<Placemark>
<name>A Polygon with label</name>
<styleUrl>#polygonStyle</styleUrl>
<MultiGeometry>
<Point>
<coordinates>0.0,0.0,0.0</coordinates>
</Point>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
10.0,10.0,0.0
10.0,-10.0,0.0
-10.0,-10.0,0.0
-10.0,10.0,0.0
10.0,10.0,0.0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Document>
</kml>

burnin...@gmail.com

unread,
Jun 13, 2017, 9:22:42 AM6/13/17
to cesium-dev
Is the point supposed to be the "center" of the polygon? This is not ideal, as this would then have to be manual

burnin...@gmail.com

unread,
Jun 13, 2017, 9:33:42 AM6/13/17
to cesium-dev
I should also add I'm getting the label from the ExtendedData

Rachel Hwang

unread,
Jun 13, 2017, 12:40:50 PM6/13/17
to cesium-dev, burnin...@gmail.com
Hi there,

Unfortunately manual is the way to go here, since polygons only take a list of positions in world space. The computation shouldn't be bad though -- you can do something simple like average positions. 

Hope that helps,
- Rachel

burnin...@gmail.com

unread,
Jun 13, 2017, 12:55:21 PM6/13/17
to cesium-dev, burnin...@gmail.com
Even doing it manually doesn't center the label in the polygon, it is skewed towards where there are more coordinate points

Rachel Hwang

unread,
Jun 13, 2017, 12:59:34 PM6/13/17
to cesium-dev, burnin...@gmail.com
That makes sense if you're doing a simple average. To center, try computing the minimum and maximum coordinates and placing your label in the center (like a bounding box).

Scott Reynolds

unread,
Jun 14, 2017, 8:37:19 AM6/14/17
to cesium-dev, burnin...@gmail.com
Placement of the label is a complicated problem.  Think about where you would want to label a horseshoe or a polygon with interior holes.  Also, in a dynamic viewer you might need to re-position the label if it is off-screen but part of the polygon is not.


On Tuesday, June 13, 2017 at 12:55:21 PM UTC-4, burnin...@gmail.com wrote:

Matthew Amato

unread,
Jun 19, 2017, 3:23:54 PM6/19/17
to cesiu...@googlegroups.com
Check out Mapbox's polylabel library: https://github.com/mapbox/polylabel I've had some success in using it with Cesium to generate ideal label positions inside of a polygon.  It's mean for 2D so large polygons sometimes don't have the ideal solution, but I find that it's much more reliable than coming up with your own algorithm.

--
You received this message because you are subscribed to the Google Groups "cesium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cesium-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages