Skip to content

Commit

Permalink
Got point xml interpolation working
Browse files Browse the repository at this point in the history
  • Loading branch information
turingtestfail committed Jun 21, 2024
1 parent 8deaee4 commit 32c7542
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.geoserver.mapml.template.MapMLMapTemplate.MAPML_FEATURE_FTL;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -20,6 +21,8 @@
import java.util.StringJoiner;
import java.util.logging.Level;
import java.util.logging.Logger;

import freemarker.template.TemplateNotFoundException;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.MetadataMap;
import org.geoserver.catalog.ResourceInfo;
Expand All @@ -30,6 +33,8 @@
import org.geoserver.mapml.xml.BodyContent;
import org.geoserver.mapml.xml.Feature;
import org.geoserver.mapml.xml.HeadContent;
import org.geoserver.mapml.xml.Interpolated;
import org.geoserver.mapml.xml.InterpolatedGeometry;
import org.geoserver.mapml.xml.Link;
import org.geoserver.mapml.xml.Mapml;
import org.geoserver.mapml.xml.Meta;
Expand All @@ -54,6 +59,9 @@
import org.geotools.util.logging.Logging;
import org.locationtech.jts.geom.Envelope;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class MapMLFeatureUtil {
private static final Logger LOGGER = Logging.getLogger(MapMLFeatureUtil.class);
public static final String STYLE_CLASS_PREFIX = ".";
Expand Down Expand Up @@ -96,9 +104,13 @@ public static Mapml featureCollectionToMapML(
}
SimpleFeatureCollection fc = (SimpleFeatureCollection) featureCollection;
boolean hasTemplate = false;
if (!mapMLMapTemplate.isTemplateEmpty(
fc.getSchema(), MAPML_FEATURE_FTL, FeatureTemplate.class, "0\n")) {
hasTemplate = true;
try {
if (!mapMLMapTemplate.isTemplateEmpty(
fc.getSchema(), MAPML_FEATURE_FTL, FeatureTemplate.class, "0\n")) {
hasTemplate = true;
}
} catch (TemplateNotFoundException e) {
LOGGER.log(Level.FINEST, MAPML_FEATURE_FTL + " Template not found", e);
}

ResourceInfo resourceInfo = layerInfo.getResource();
Expand Down Expand Up @@ -162,6 +174,15 @@ public static Mapml featureCollectionToMapML(
SimpleFeature feature = iterator.next();
if (hasTemplate) {
String templateOutput = mapMLMapTemplate.features(fc.getSchema(), feature);
StringReader reader = new StringReader(templateOutput);
try {
JAXBContext context = JAXBContext.newInstance(Interpolated.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Interpolated ig = (Interpolated) unmarshaller.unmarshal(reader);
InterpolatedGeometry igg = ig.getMapInterpolatedGeometry();
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error unmarshalling template output", e);
}
}
// convert feature to xml
if (styles != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.geoserver.mapml.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import java.util.List;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "",
propOrder = {"head", "mapInterpolatedProperties", "mapInterpolatedGeometry"})
@XmlRootElement(name = "mapml-interpolated", namespace = "http://www.w3.org/1999/xhtml")
public class Interpolated {
@XmlElement(required = false, name = "map-head", namespace = "http://www.w3.org/1999/xhtml")
protected HeadContent head;

@XmlElement(
name = "map-interpolated-property",
required = false,
namespace = "http://www.w3.org/1999/xhtml")
protected List<InterpolatedProperty> mapInterpolatedProperties;

@XmlElement(
name = "map-interpolated-geometry",
required = false,
namespace = "http://www.w3.org/1999/xhtml")
protected InterpolatedGeometry mapInterpolatedGeometry;

/**
* Gets the value of the head property.
*
* @return possible object is {@link HeadContent }
*/
public HeadContent getHead() {
return head;
}

/**
* Sets the value of the head property.
*
* @param value allowed object is {@link HeadContent }
*/
public void setHead(HeadContent value) {
this.head = value;
}

public List<InterpolatedProperty> getMapInterpolatedProperties() {
return mapInterpolatedProperties;
}

public void setMapInterpolatedProperties(List<InterpolatedProperty> mapInterpolatedProperties) {
this.mapInterpolatedProperties = mapInterpolatedProperties;
}

public InterpolatedGeometry getMapInterpolatedGeometry() {
return mapInterpolatedGeometry;
}

public void setMapInterpolatedGeometry(InterpolatedGeometry mapInterpolatedGeometry) {
this.mapInterpolatedGeometry = mapInterpolatedGeometry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.geoserver.mapml.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlType;
import java.util.ArrayList;
import java.util.List;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "",
propOrder = {"components", "coordinates"})
public class InterpolatedGeometry {
@XmlAttribute protected String type;

public String getType() {
return type;
}

public void setType(String value) {
this.type = value;
}

@XmlElements({
@XmlElement(name = "point", type = Point.class, namespace = "http://www.w3.org/1999/xhtml"),
@XmlElement(
name = "map-linestring",
type = LineString.class,
namespace = "http://www.w3.org/1999/xhtml"),
@XmlElement(
name = "map-polygon",
type = Polygon.class,
namespace = "http://www.w3.org/1999/xhtml"),
@XmlElement(
name = "map-multipoint",
type = MultiPoint.class,
namespace = "http://www.w3.org/1999/xhtml"),
@XmlElement(
name = "map-multilinestring",
type = MultiLineString.class,
namespace = "http://www.w3.org/1999/xhtml"),
@XmlElement(
name = "map-multipolygon",
type = MultiPolygon.class,
namespace = "http://www.w3.org/1999/xhtml")
})
protected List<Object> components;

public List<Object> getComponents() {
if (components == null) {
components = new ArrayList<>();
}
return components;
}

@XmlElement(name = "map-coordinates", namespace = "http://www.w3.org/1999/xhtml")
protected List<String> coordinates;

public List<String> getCoordinates() {
if (coordinates == null) {
coordinates = new ArrayList<>();
}
return this.coordinates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.geoserver.mapml.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "map-interpolated-property", namespace = "http://www.w3.org/1999/xhtml")
public class InterpolatedProperty {
@XmlAttribute protected String name;
@XmlAttribute protected String value;

public String getName() {
return name;
}

public void setName(String value) {
this.name = value;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ public Mapml createMapml() {
return new Mapml();
}

public Interpolated createInterpolated() {
return new Interpolated();
}

public InterpolatedGeometry createInterpolatedGeometry() {
return new InterpolatedGeometry();
}

/** Create an instance of {@link Tile } */
public Tile createTile() {
return new Tile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,137 @@ public void testMapMLGetStyleQuery() throws Exception {
}

@Test
public void testMapMLFeatureHasClass() throws Exception {
public void testMapMLFeaturePointHasClass() throws Exception {
File template = null;
try {
Catalog cat = getCatalog();
LayerInfo li = cat.getLayerByName(MockData.BRIDGES.getLocalPart());
li.getResource().getMetadata().put(MAPML_USE_FEATURES, true);
li.getResource().getMetadata().put(MAPML_USE_TILES, false);
cat.save(li);
String layerId = getLayerId(MockData.BRIDGES);
FeatureTypeInfo resource =
getCatalog().getResourceByName(layerId, FeatureTypeInfo.class);
File parent = getDataDirectory().get(resource).dir();
template = new File(parent, MAPML_FEATURE_FTL);
FileUtils.write(
template,
"<mapml-interpolated xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+ "<map-head>\n"
+ " <map-style>.desired {stroke-dashoffset:3}</map-style>\n"
+ "</map-head>\n"
+ " <#list attributes as attribute>\n"
+ " <#if attribute.name == \"NAME\">\n"
+ " <map-interpolated-property name=\"UPDATED ${attribute.name}\" value=\"CHANGED ${attribute.value}\"/>\n"
+ " </#if>\n"
+ " </#list>\n"
+ " <#list attributes as gattribute>\n"
+ " <#if gattribute.isGeometry>\n"
+ " <map-interpolated-geometry type=\"point\" >\n"
+ " <map-coordinates>"
+ " <#list gattribute.rawValue.coordinates as coord>"
+ " <#if coord?index == 0>"
+ " <![CDATA[<span class=\"desired\">]]>${coord.x} ${coord.y}<![CDATA[</span>]]>"
+ " <#else>"
+ " ${coord.x} ${coord.y}"
+ " </#if>"
+ " </#list>"
+ " </map-coordinates>"
+ " </map-interpolated-geometry>"
+ " </#if>\n"
+ " </#list>\n"
+ "</mapml-interpolated>\n",
"UTF-8");
Mapml mapmlFeatures =
new MapMLWMSRequest()
.name(MockData.BRIDGES.getLocalPart())
.bbox("-180,-90,180,90")
.srs("EPSG:4326")
.feature(true)
.getAsMapML();

String mapmlStyle = mapmlFeatures.getHead().getStyle();

Feature feature2 =
mapmlFeatures
.getBody()
.getFeatures()
.get(1); // get the second feature, which has a class
assertEquals("desired", feature2.getStyle());
} finally {
if (template != null) {
template.delete();
}
}
}

@Test
public void testMapMLFeatureLineHasClass() throws Exception {
File template = null;
try {
Catalog cat = getCatalog();
LayerInfo li = cat.getLayerByName(MockData.ROAD_SEGMENTS.getLocalPart());
li.getResource().getMetadata().put(MAPML_USE_FEATURES, true);
li.getResource().getMetadata().put(MAPML_USE_TILES, false);
cat.save(li);
String layerId = getLayerId(MockData.BUILDINGS);
FeatureTypeInfo resource =
getCatalog().getResourceByName(layerId, FeatureTypeInfo.class);
File parent = getDataDirectory().get(resource).dir();
template = new File(parent, MAPML_FEATURE_FTL);
FileUtils.write(
template,
"<map-head>\n"
+ " <map-style>.desired {stroke-dashoffset:3}</map-style>\n"
+ "</map-head>\n"
+ " <#list attributes as attribute>\n"
+ " <#if attribute.name == \"NAME\">\n"
+ " <map-property name=\"UPDATED ${attribute.name}\" value=\"CHANGED ${attribute.value}\"/>\n"
+ " <#elseif !attribute.isGeometry>\n"
+ " <map-property name=\"${attribute.name}\" value=\"${attribute.value}\"/>\n"
+ " </#if>\n"
+ " <#if attribute.isGeometry>\n"
+ " <map-interpolated-geometry type=\"linestring\" >\n"
+ " <map-coordinates>\n"
+ " <#list attribute.rawValue.coordinates as coord>\n"
+ " <#if coord?index == 3>\n"
+ " <span class=\"desired\">${coord.x} ${coord.y}\n"
+ " <#elseif coord?index == 4>\n"
+ " ${coord.x} ${coord.y}</span>\n"
+ " <#else>\n"
+ " ${coord.x} ${coord.y}\n"
+ " </#if>\n"
+ " </#list>\n"
+ " </map-coordinates>\n"
+ " </map-interpolated-geometry>\n"
+ " </#if>\n"
+ " </#list>\n",
"UTF-8");
Mapml mapmlFeatures =
new MapMLWMSRequest()
.name(MockData.ROAD_SEGMENTS.getLocalPart())
.bbox("-180,-90,180,90")
.srs("EPSG:4326")
.feature(true)
.getAsMapML();

String mapmlStyle = mapmlFeatures.getHead().getStyle();

Feature feature2 =
mapmlFeatures
.getBody()
.getFeatures()
.get(1); // get the second feature, which has a class
assertEquals("desired", feature2.getStyle());
} finally {
if (template != null) {
template.delete();
}
}
}

@Test
public void testMapMLFeaturePolygonHasClass() throws Exception {
File template = null;
try {
Catalog cat = getCatalog();
Expand Down

0 comments on commit 32c7542

Please sign in to comment.