use of org.geotoolkit.data.kml.model.PolyStyle in project geotoolkit by Geomatys.
the class KmzContextInterpreter method writeSymbolizer.
/**
* Writes KML color styles mapping SLD Symbolizers.
* Color styles are written into KML Style selector.
*/
private AbstractStyleSelector writeSymbolizer(Symbolizer symbolizer, Style styleSelector) {
if (symbolizer instanceof ExtensionSymbolizer) {
} else // LineSymbolizer mapping
if (symbolizer instanceof LineSymbolizer) {
final LineSymbolizer lineSymbolizer = (LineSymbolizer) symbolizer;
final LineStyle lineStyle = ((styleSelector.getLineStyle() == null) ? KML_FACTORY.createLineStyle() : styleSelector.getLineStyle());
lineStyle.setWidth((Double) this.writeExpression(lineSymbolizer.getStroke().getWidth(), Double.class, null));
lineStyle.setColor((Color) this.writeExpression(lineSymbolizer.getStroke().getColor(), Color.class, null));
styleSelector.setLineStyle(lineStyle);
} else // PointSymbolizezr mapping
if (symbolizer instanceof PointSymbolizer) {
// PointSymbolizer pointSymbolizer = (PointSymbolizer) symbolizer;
// IconStyle iconStyle = KML_FACTORY.createIconStyle();
// GraphicalSymbol gs = ((GraphicalSymbol) pointSymbolizer.getGraphic().graphicalSymbols().get(0));
// gs.
} else // PolygonSymbolizer mapping
if (symbolizer instanceof PolygonSymbolizer) {
final PolygonSymbolizer polygonSymbolizer = (PolygonSymbolizer) symbolizer;
final PolyStyle polyStyle = KML_FACTORY.createPolyStyle();
// Fill
if (polygonSymbolizer.getFill() == null) {
polyStyle.setFill(false);
} else {
polyStyle.setFill(true);
polyStyle.setColor((Color) this.writeExpression(polygonSymbolizer.getFill().getColor(), Color.class, null));
}
// Outline
if (polygonSymbolizer.getStroke() == null) {
polyStyle.setOutline(false);
} else if (styleSelector.getLineStyle() == null) {
polyStyle.setOutline(true);
final LineStyle lineStyle = KML_FACTORY.createLineStyle();
lineStyle.setColor((Color) this.writeExpression(polygonSymbolizer.getStroke().getColor(), Color.class, null));
lineStyle.setWidth((Double) this.writeExpression(polygonSymbolizer.getStroke().getWidth(), Double.class, null));
styleSelector.setLineStyle(lineStyle);
}
styleSelector.setPolyStyle(polyStyle);
} else if (symbolizer instanceof RasterSymbolizer) {
} else if (symbolizer instanceof TextSymbolizer) {
final TextSymbolizer textSymbolizer = (TextSymbolizer) symbolizer;
final LabelStyle labelStyle = KML_FACTORY.createLabelStyle();
if (textSymbolizer.getFont() != null) {
textSymbolizer.getFont().getSize();
}
if (textSymbolizer.getFill() != null) {
labelStyle.setColor((Color) this.writeExpression(textSymbolizer.getFill().getColor(), Color.class, null));
}
styleSelector.setLabelStyle(labelStyle);
}
return styleSelector;
}
use of org.geotoolkit.data.kml.model.PolyStyle in project geotoolkit by Geomatys.
the class KmlReader method readStyle.
private Style readStyle() throws XMLStreamException, KmlException, URISyntaxException {
// AbstractObject
List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
IdAttributes idAttributes = readIdAttributes();
// AbstractStyleSelector
List<SimpleTypeContainer> styleSelectorSimpleExtensions = new ArrayList<>();
List<Object> styleSelectorObjectExtensions = new ArrayList<>();
// Style
IconStyle iconStyle = null;
LabelStyle labelStyle = null;
LineStyle lineStyle = null;
PolyStyle polyStyle = null;
BalloonStyle balloonStyle = null;
ListStyle listStyle = null;
List<SimpleTypeContainer> styleSimpleExtensions = new ArrayList<>();
List<Object> styleObjectExtensions = new ArrayList<>();
boucle: while (reader.hasNext()) {
switch(reader.next()) {
case XMLStreamConstants.START_ELEMENT:
{
final String eName = reader.getLocalName();
final String eUri = reader.getNamespaceURI();
if (equalsNamespace(eUri)) {
switch(eName) {
case TAG_ICON_STYLE:
iconStyle = readIconStyle();
break;
case TAG_LABEL_STYLE:
labelStyle = readLabelStyle();
break;
case TAG_LINE_STYLE:
lineStyle = readLineStyle();
break;
case TAG_POLY_STYLE:
polyStyle = readPolyStyle();
break;
case TAG_BALLOON_STYLE:
balloonStyle = readBalloonStyle();
break;
case TAG_LIST_STYLE:
listStyle = readListStyle();
break;
}
} else // EXTENSIONS
{
KmlExtensionReader r;
if ((r = this.getComplexExtensionReader(TAG_STYLE, eUri, eName)) != null) {
Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_STYLE, eUri, eName);
Object ext = result.getKey();
Extensions.Names extensionLevel = result.getValue();
if (Extensions.Names.STYLE_SELECTOR.equals(extensionLevel)) {
styleSelectorObjectExtensions.add(ext);
} else if (Extensions.Names.STYLE.equals(extensionLevel)) {
styleObjectExtensions.add(ext);
}
} else if ((r = getSimpleExtensionReader(TAG_STYLE, eUri, eName)) != null) {
Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_STYLE, eUri, eName);
Object ext = result.getKey();
Extensions.Names extensionLevel = result.getValue();
if (Extensions.Names.OBJECT.equals(extensionLevel)) {
objectSimpleExtensions.add((SimpleTypeContainer) ext);
} else if (Extensions.Names.STYLE_SELECTOR.equals(extensionLevel)) {
styleSelectorSimpleExtensions.add((SimpleTypeContainer) ext);
} else if (Extensions.Names.STYLE.equals(extensionLevel)) {
styleSimpleExtensions.add((SimpleTypeContainer) ext);
}
}
}
break;
}
case XMLStreamConstants.END_ELEMENT:
{
if (TAG_STYLE.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
break boucle;
}
break;
}
}
}
return KmlReader.KML_FACTORY.createStyle(objectSimpleExtensions, idAttributes, styleSelectorSimpleExtensions, styleSelectorObjectExtensions, iconStyle, labelStyle, lineStyle, polyStyle, balloonStyle, listStyle, styleSimpleExtensions, styleObjectExtensions);
}
use of org.geotoolkit.data.kml.model.PolyStyle in project geotoolkit by Geomatys.
the class StyleTest method styleWriteTest.
@Test
public void styleWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
final Feature placemark0 = kmlFactory.createPlacemark();
placemark0.setPropertyValue(KmlConstants.TAG_NAME, "Google Earth - New Polygon");
placemark0.setPropertyValue(KmlConstants.TAG_DESCRIPTION, "Here is some descriptive text");
placemark0.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#myDefaultStyles"));
final Feature placemark1 = kmlFactory.createPlacemark();
placemark1.setPropertyValue(KmlConstants.TAG_NAME, "Google Earth - New Path");
placemark1.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#myDefaultStyles"));
final IconStyle iconStyle = kmlFactory.createIconStyle();
final BasicLink icon = kmlFactory.createBasicLink();
icon.setHref("http://myserver.com/icon.jpg");
iconStyle.setIcon(icon);
iconStyle.setColor(new Color(255, 0, 255, 161));
iconStyle.setScale(1.399999976158142);
final LabelStyle labelStyle = kmlFactory.createLabelStyle();
labelStyle.setColor(new Color(255, 170, 255, 127));
labelStyle.setScale(1.5);
final LineStyle lineStyle = kmlFactory.createLineStyle();
lineStyle.setColor(new Color(255, 0, 0, 255));
lineStyle.setWidth(15);
final PolyStyle polyStyle = kmlFactory.createPolyStyle();
polyStyle.setColor(new Color(170, 170, 127, 127));
polyStyle.setColorMode(ColorMode.RANDOM);
final IdAttributes idAttributes = kmlFactory.createIdAttributes("myDefaultStyles", null);
final Style style = kmlFactory.createStyle();
style.setIdAttributes(idAttributes);
style.setIconStyle(iconStyle);
style.setLabelStyle(labelStyle);
style.setLineStyle(lineStyle);
style.setPolyStyle(polyStyle);
final Feature document = kmlFactory.createDocument();
document.setPropertyValue(KmlConstants.TAG_STYLE_SELECTOR, style);
document.setPropertyValue(KmlConstants.TAG_FEATURES, Arrays.asList(placemark0, placemark1));
final Kml kml = kmlFactory.createKml(null, document, null, null);
final File temp = File.createTempFile("testStyle", ".kml");
temp.deleteOnExit();
final KmlWriter writer = new KmlWriter();
writer.setOutput(temp);
writer.write(kml);
writer.dispose();
DomCompare.compare(new File(pathToTestFile), temp);
}
use of org.geotoolkit.data.kml.model.PolyStyle in project geotoolkit by Geomatys.
the class PolyStyleTest method polyStyleReadTest.
@Test
public void polyStyleReadTest() throws IOException, XMLStreamException, URISyntaxException, KmlException {
final KmlReader reader = new KmlReader();
reader.setInput(new File(pathToTestFile));
final Kml kmlObjects = reader.read();
reader.dispose();
final Feature document = kmlObjects.getAbstractFeature();
assertEquals(KmlModelConstants.TYPE_DOCUMENT, document.getType());
assertEquals("PolygonStyle.kml", document.getPropertyValue(KmlConstants.TAG_NAME));
assertEquals(Boolean.TRUE, document.getPropertyValue(KmlConstants.TAG_OPEN));
Iterator<?> i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_STYLE_SELECTOR)).iterator();
assertTrue("Expected at least one element.", i.hasNext());
Style style = (Style) i.next();
assertEquals("examplePolyStyle", style.getIdAttributes().getId());
PolyStyle polyStyle = style.getPolyStyle();
assertEquals(new Color(204, 0, 0, 255), polyStyle.getColor());
assertEquals(ColorMode.RANDOM, polyStyle.getColorMode());
assertFalse("Expected exactly one element.", i.hasNext());
i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_FEATURES)).iterator();
assertTrue("Expected at least one element.", i.hasNext());
Feature placemark = (Feature) i.next();
assertEquals("hollow box", placemark.getPropertyValue(KmlConstants.TAG_NAME));
assertEquals(new URI("#examplePolyStyle"), placemark.getPropertyValue(KmlConstants.TAG_STYLE_URL));
Polygon polygon = (Polygon) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
assertTrue(polygon.getExtrude());
assertEquals(EnumAltitudeMode.RELATIVE_TO_GROUND, polygon.getAltitudeMode());
Boundary outerBoundaryIs = polygon.getOuterBoundary();
LinearRing linearRing = outerBoundaryIs.getLinearRing();
CoordinateSequence coordinates = linearRing.getCoordinateSequence();
assertEquals(5, coordinates.size());
Coordinate coordinate0 = coordinates.getCoordinate(0);
assertEquals(-122.3662784465226, coordinate0.x, DELTA);
assertEquals(37.81884427772081, coordinate0.y, DELTA);
assertEquals(30, coordinate0.z, DELTA);
Coordinate coordinate1 = coordinates.getCoordinate(1);
assertEquals(-122.3652480684771, coordinate1.x, DELTA);
assertEquals(37.81926777010555, coordinate1.y, DELTA);
assertEquals(30, coordinate1.z, DELTA);
Coordinate coordinate2 = coordinates.getCoordinate(2);
assertEquals(-122.365640222455, coordinate2.x, DELTA);
assertEquals(37.81986126286519, coordinate2.y, DELTA);
assertEquals(30, coordinate2.z, DELTA);
Coordinate coordinate3 = coordinates.getCoordinate(3);
assertEquals(-122.36666937925, coordinate3.x, DELTA);
assertEquals(37.81942987753481, coordinate3.y, DELTA);
assertEquals(30, coordinate3.z, DELTA);
Coordinate coordinate4 = coordinates.getCoordinate(4);
assertEquals(-122.3662784465226, coordinate4.x, DELTA);
assertEquals(37.81884427772081, coordinate4.y, DELTA);
assertEquals(30, coordinate4.z, DELTA);
List<Boundary> innerBoundariesAre = polygon.getInnerBoundaries();
assertEquals(1, innerBoundariesAre.size());
Boundary innerBoundaryIs0 = innerBoundariesAre.get(0);
LinearRing linearRing0 = innerBoundaryIs0.getLinearRing();
CoordinateSequence coordinates0 = linearRing0.getCoordinateSequence();
assertEquals(5, coordinates0.size());
Coordinate coordinate00 = coordinates0.getCoordinate(0);
assertEquals(-122.366212593918, coordinate00.x, DELTA);
assertEquals(37.81897719083808, coordinate00.y, DELTA);
assertEquals(30, coordinate00.z, DELTA);
Coordinate coordinate01 = coordinates0.getCoordinate(1);
assertEquals(-122.3654241733188, coordinate01.x, DELTA);
assertEquals(37.81929450992014, coordinate01.y, DELTA);
assertEquals(30, coordinate01.z, DELTA);
Coordinate coordinate02 = coordinates0.getCoordinate(2);
assertEquals(-122.3657048517827, coordinate02.x, DELTA);
assertEquals(37.81973175302663, coordinate02.y, DELTA);
assertEquals(30, coordinate02.z, DELTA);
Coordinate coordinate03 = coordinates0.getCoordinate(3);
assertEquals(-122.3664882465854, coordinate03.x, DELTA);
assertEquals(37.81940249291773, coordinate03.y, DELTA);
assertEquals(30, coordinate03.z, DELTA);
Coordinate coordinate04 = coordinates0.getCoordinate(4);
assertEquals(-122.366212593918, coordinate04.x, DELTA);
assertEquals(37.81897719083808, coordinate04.y, DELTA);
assertEquals(30, coordinate04.z, DELTA);
assertFalse("Expected exactly one element.", i.hasNext());
}
use of org.geotoolkit.data.kml.model.PolyStyle in project geotoolkit by Geomatys.
the class PolyStyleTest method polyStyleWriteTest.
@Test
public void polyStyleWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
final Coordinate coordinate0 = kmlFactory.createCoordinate(-122.3662784465226, 37.81884427772081, 30);
final Coordinate coordinate1 = kmlFactory.createCoordinate(-122.3652480684771, 37.81926777010555, 30);
final Coordinate coordinate2 = kmlFactory.createCoordinate(-122.365640222455, 37.81986126286519, 30);
final Coordinate coordinate3 = kmlFactory.createCoordinate(-122.36666937925, 37.81942987753481, 30);
final Coordinate coordinate4 = kmlFactory.createCoordinate(-122.3662784465226, 37.81884427772081, 30);
final CoordinateSequence coordinates = kmlFactory.createCoordinates(Arrays.asList(coordinate0, coordinate1, coordinate2, coordinate3, coordinate4));
final Coordinate coordinate00 = kmlFactory.createCoordinate(-122.366212593918, 37.81897719083808, 30);
final Coordinate coordinate01 = kmlFactory.createCoordinate(-122.3654241733188, 37.81929450992014, 30);
final Coordinate coordinate02 = kmlFactory.createCoordinate(-122.3657048517827, 37.81973175302663, 30);
final Coordinate coordinate03 = kmlFactory.createCoordinate(-122.3664882465854, 37.81940249291773, 30);
final Coordinate coordinate04 = kmlFactory.createCoordinate(-122.366212593918, 37.81897719083808, 30);
final CoordinateSequence coordinates0 = kmlFactory.createCoordinates(Arrays.asList(coordinate00, coordinate01, coordinate02, coordinate03, coordinate04));
final LinearRing linearRing = kmlFactory.createLinearRing(coordinates);
final LinearRing linearRing0 = kmlFactory.createLinearRing(coordinates0);
final Boundary outerBoundaryIs = kmlFactory.createBoundary();
outerBoundaryIs.setLinearRing(linearRing);
final Boundary innerBoundaryIs = kmlFactory.createBoundary();
innerBoundaryIs.setLinearRing(linearRing0);
final Polygon polygon = kmlFactory.createPolygon(outerBoundaryIs, Arrays.asList(innerBoundaryIs));
polygon.setExtrude(true);
polygon.setAltitudeMode(EnumAltitudeMode.RELATIVE_TO_GROUND);
final Feature placemark = kmlFactory.createPlacemark();
placemark.setPropertyValue(KmlConstants.TAG_NAME, "hollow box");
placemark.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#examplePolyStyle"));
placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, polygon);
final Style style = kmlFactory.createStyle();
final IdAttributes idAttributes = kmlFactory.createIdAttributes("examplePolyStyle", null);
style.setIdAttributes(idAttributes);
final PolyStyle polyStyle = kmlFactory.createPolyStyle();
polyStyle.setColor(new Color(204, 0, 0, 255));
polyStyle.setColorMode(ColorMode.RANDOM);
style.setPolyStyle(polyStyle);
final Feature document = kmlFactory.createDocument();
document.setPropertyValue(KmlConstants.TAG_STYLE_SELECTOR, style);
document.setPropertyValue(KmlConstants.TAG_FEATURES, placemark);
document.setPropertyValue(KmlConstants.TAG_NAME, "PolygonStyle.kml");
document.setPropertyValue(KmlConstants.TAG_OPEN, Boolean.TRUE);
final Kml kml = kmlFactory.createKml(null, document, null, null);
final File temp = File.createTempFile("testPolyStyle", ".kml");
temp.deleteOnExit();
final KmlWriter writer = new KmlWriter();
writer.setOutput(temp);
writer.write(kml);
writer.dispose();
DomCompare.compare(new File(pathToTestFile), temp);
}
Aggregations