Skip to content

Commit

Permalink
version that uses lists of coordinates instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
turingtestfail committed Jun 30, 2024
1 parent 156b83b commit 32c267c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@ private static Optional<Mapml> getInterpolatedFromTemplate(
try {
String templateOutput = mapMLMapTemplate.features(fc.getSchema(), feature);
Mapml out = encoder.decode(new StringReader(templateOutput));
if (out != null) {
replaceSpacePlaceholder(out.getBody().getFeatures());
}
return Optional.of(out);
} catch (IOException e) {
LOGGER.info(
Expand All @@ -241,41 +238,6 @@ private static Optional<Mapml> getInterpolatedFromTemplate(
}
}

private static void replaceSpacePlaceholder(List<Feature> features) {
for (Feature feature : features) {
if (feature.getGeometry() != null) {
if (feature.getGeometry().getGeometryContent().getValue() instanceof Point) {
Point point = (Point) feature.getGeometry().getGeometryContent().getValue();
point.getCoordinates().replaceAll(c -> c.replace(COORDINATES_BLANK, " "));
} else if (feature.getGeometry().getGeometryContent().getValue()
instanceof LineString) {
LineString lineString =
(LineString) feature.getGeometry().getGeometryContent().getValue();
lineString.getCoordinates().replaceAll(c -> c.replace(COORDINATES_BLANK, " "));
} else if (feature.getGeometry().getGeometryContent().getValue()
instanceof org.geoserver.mapml.xml.MultiLineString) {
org.geoserver.mapml.xml.MultiLineString multiLineString =
(org.geoserver.mapml.xml.MultiLineString)
feature.getGeometry().getGeometryContent().getValue();
multiLineString
.getTwoOrMoreCoordinatePairs()
.replaceAll(
c -> {
c.getValue()
.replaceAll(f -> f.replace(COORDINATES_BLANK, " "));
return c;
});
} else if (feature.getGeometry().getGeometryContent().getValue()
instanceof org.geoserver.mapml.xml.MultiPoint) {
org.geoserver.mapml.xml.MultiPoint multiPoint =
(org.geoserver.mapml.xml.MultiPoint)
feature.getGeometry().getGeometryContent().getValue();
multiPoint.getCoordinates().replaceAll(c -> c.replace(COORDINATES_BLANK, " "));
}
}
}
}

/**
* Append the CSS style from the template to the feature
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,11 @@ private org.geoserver.mapml.xml.MultiPolygon buildMultiPolygon(MultiPolygon mpg)
private org.geoserver.mapml.xml.MultiLineString buildMultiLineString(MultiLineString ml) {
org.geoserver.mapml.xml.MultiLineString multiLine =
new org.geoserver.mapml.xml.MultiLineString();
List<JAXBElement<List<String>>> coordLists = multiLine.getTwoOrMoreCoordinatePairs();
List<Coordinates> coordLists = multiLine.getTwoOrMoreCoordinatePairs();
for (int i = 0; i < ml.getNumGeometries(); i++) {
coordLists.add(
factory.createMultiLineStringCoordinates(
buildCoordinates(
((LineString) (ml.getGeometryN(i))).getCoordinateSequence(),
null)));
LineString ls = (LineString) ml.getGeometryN(i);
String coordList = buildCoordinates(ls.getCoordinateSequence());
coordLists.add(new Coordinates(coordList));
}
return multiLine;
}
Expand All @@ -424,7 +422,7 @@ private org.geoserver.mapml.xml.MultiLineString buildMultiLineString(MultiLineSt
*/
private org.geoserver.mapml.xml.LineString buildLineString(LineString l) {
org.geoserver.mapml.xml.LineString lineString = new org.geoserver.mapml.xml.LineString();
List<String> lsCoords = lineString.getCoordinates();
List<Coordinates> lsCoords = lineString.getCoordinates();
buildCoordinates(l.getCoordinateSequence(), lsCoords);
return lineString;
}
Expand All @@ -435,7 +433,7 @@ private org.geoserver.mapml.xml.LineString buildLineString(LineString l) {
*/
private org.geoserver.mapml.xml.MultiPoint buildMultiPoint(MultiPoint mp) {
org.geoserver.mapml.xml.MultiPoint multiPoint = new org.geoserver.mapml.xml.MultiPoint();
List<String> mpCoords = multiPoint.getCoordinates();
List<Coordinates> mpCoords = multiPoint.getCoordinates();
buildCoordinates(new CoordinateArraySequence(mp.getCoordinates()), mpCoords);
return multiPoint;
}
Expand All @@ -447,7 +445,11 @@ private org.geoserver.mapml.xml.MultiPoint buildMultiPoint(MultiPoint mp) {
private org.geoserver.mapml.xml.Point buildPoint(Point p) {
org.geoserver.mapml.xml.Point point = new org.geoserver.mapml.xml.Point();
point.getCoordinates()
.add(this.formatter.format(p.getX()) + SPACE + this.formatter.format(p.getY()));
.add(
new Coordinates(
this.formatter.format(p.getX())
+ SPACE
+ this.formatter.format(p.getY())));
return point;
}

Expand Down Expand Up @@ -508,7 +510,7 @@ private Object buildTaggedCoordinateSequence(TaggedPolygon.TaggedCoordinateSeque
} else {
return new Span(
"bbox",
buildCoordinates(
buildSpanCoordinates(
new CoordinateArraySequence(
cs.getCoordinates().toArray(n -> new Coordinate[n])),
null));
Expand All @@ -520,7 +522,7 @@ private Object buildTaggedCoordinateSequence(TaggedPolygon.TaggedCoordinateSeque
* @param coordList a list of coordinate strings to add to
* @return
*/
private List<String> buildCoordinates(CoordinateSequence cs, List<String> coordList) {
private List<String> buildSpanCoordinates(CoordinateSequence cs, List<String> coordList) {
if (coordList == null) {
coordList = new ArrayList<>(cs.size());
}
Expand All @@ -531,6 +533,25 @@ private List<String> buildCoordinates(CoordinateSequence cs, List<String> coordL
return coordList;
}

/**
* @param cs a JTS CoordinateSequence
* @param coordList a list of coordinate strings to add to
* @return
*/
private List<Coordinates> buildCoordinates(CoordinateSequence cs, List<Coordinates> coordList) {
if (coordList == null) {
coordList = new ArrayList<>(cs.size());
}
for (int i = 0; i < cs.size(); i++) {
coordList.add(
new Coordinates(
this.formatter.format(cs.getX(i))
+ SPACE
+ this.formatter.format(cs.getY(i))));
}
return coordList;
}

/**
* Builds a space separated representation of the coordinate sequence
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlType;

/**
Expand All @@ -37,12 +39,12 @@
propOrder = {"coordinates"})
public class LineString {

@XmlList
@XmlElement(
required = true,
@XmlMixed
@XmlElementRef(
name = "map-coordinates",
type = Coordinates.class,
namespace = "http://www.w3.org/1999/xhtml")
protected List<String> coordinates;
protected List<Coordinates> coordinates;

/**
* Gets the value of the coordinates property.
Expand All @@ -61,7 +63,7 @@ public class LineString {
*
* @return list of coordinates elements
*/
public List<String> getCoordinates() {
public List<Coordinates> getCoordinates() {
if (coordinates == null) {
coordinates = new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlType;

/**
Expand All @@ -37,11 +38,12 @@
propOrder = {"twoOrMoreCoordinatePairs"})
public class MultiLineString {

@XmlMixed
@XmlElementRef(
name = "map-coordinates",
type = JAXBElement.class,
type = Coordinates.class,
namespace = "http://www.w3.org/1999/xhtml")
protected List<JAXBElement<List<String>>> twoOrMoreCoordinatePairs;
protected List<Coordinates> twoOrMoreCoordinatePairs;

/**
* Gets the value of the twoOrMoreCoordinatePairs property.
Expand All @@ -61,7 +63,7 @@ public class MultiLineString {
*
* @return two or more coordinate pairs
*/
public List<JAXBElement<List<String>>> getTwoOrMoreCoordinatePairs() {
public List<Coordinates> getTwoOrMoreCoordinatePairs() {
if (twoOrMoreCoordinatePairs == null) {
twoOrMoreCoordinatePairs = new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlType;

/**
Expand All @@ -37,12 +39,12 @@
propOrder = {"coordinates"})
public class MultiPoint {

@XmlList
@XmlElement(
required = true,
@XmlMixed
@XmlElementRef(
name = "map-coordinates",
type = Coordinates.class,
namespace = "http://www.w3.org/1999/xhtml")
protected List<String> coordinates;
protected List<Coordinates> coordinates;

/**
* Gets the value of the map-coordinates property. Exception Description: The property or field
Expand All @@ -63,7 +65,7 @@ public class MultiPoint {
*
* @return list of coordinates strings
*/
public List<String> getCoordinates() {
public List<Coordinates> getCoordinates() {
if (coordinates == null) {
coordinates = new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ public JAXBElement<List> createPolygonCoordinates(List value) {
namespace = "http://www.w3.org/1999/xhtml",
name = "map-coordinates",
scope = MultiLineString.class)
public JAXBElement<List<String>> createMultiLineStringCoordinates(List<String> value) {
public JAXBElement<List> createMultiLineStringCoordinates(List value) {
return new JAXBElement<>(
_MultiPointCoordinates_QNAME,
((Class) List.class),
MultiLineString.class,
((List<String>) value));
((List<Coordinates>) value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlType;

/**
Expand All @@ -37,12 +39,12 @@
propOrder = {"coordinates"})
public class Point {

@XmlList
@XmlElement(
required = true,
@XmlMixed
@XmlElementRef(
name = "map-coordinates",
type = Coordinates.class,
namespace = "http://www.w3.org/1999/xhtml")
protected List<String> coordinates;
protected List<Coordinates> coordinates;

/**
* Gets the value of the coordinates property.
Expand All @@ -61,7 +63,7 @@ public class Point {
*
* @return list of coordinates strings
*/
public List<String> getCoordinates() {
public List<Coordinates> getCoordinates() {
if (coordinates == null) {
coordinates = new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.geoserver.catalog.LayerInfo;
import org.geoserver.data.test.MockData;
import org.geoserver.data.test.SystemTestData;
import org.geoserver.mapml.xml.Coordinates;
import org.geoserver.mapml.xml.Feature;
import org.geoserver.mapml.xml.LineString;
import org.geoserver.mapml.xml.Mapml;
Expand Down Expand Up @@ -253,12 +254,12 @@ public void testCoordinateSimplification() throws Exception {
// all lines are small enough that they are simplified to start/end
if (geometry instanceof LineString) {
LineString ls = (LineString) geometry;
assertEquals(4, ls.getCoordinates().size());
assertEquals(1, ls.getCoordinates().get(0).getCoordinates().size());
assertEquals(1, ls.getCoordinates().get(1).getCoordinates().size());
} else if (geometry instanceof MultiLineString) {
MultiLineString mls = (MultiLineString) geometry;
for (JAXBElement je : mls.getTwoOrMoreCoordinatePairs()) {
List<String> coordinates = (List<String>) je.getValue();
assertEquals(4, coordinates.size());
for (Coordinates je : mls.getTwoOrMoreCoordinatePairs()) {
assertEquals(1, je.getCoordinates().size());
}
}
}
Expand Down Expand Up @@ -367,7 +368,7 @@ public void testMapMLFeaturePointHasClass() throws Exception {
+ " <map-geometry>"
+ " <map-point>"
+ " <map-coordinates><#list gattribute.rawValue.coordinates as coord>"
+ " <#if coord?index == 0><![CDATA[<span${space}class=\"desired\">]]>${coord.x}${space}${coord.y}<![CDATA[</span>]]><#else>${coord.x}${space}${coord.y}</#if></#list></map-coordinates></map-point>"
+ " <#if coord?index == 0><![CDATA[<span class=\"desired\">]]>${coord.x} ${coord.y}<![CDATA[</span>]]><#else>${coord.x} ${coord.y}</#if></#list></map-coordinates></map-point>"
+ " </map-geometry>"
+ " </#if>\n"
+ " </#list>\n"
Expand All @@ -391,8 +392,8 @@ public void testMapMLFeaturePointHasClass() throws Exception {
.getFeatures()
.get(0); // get the second feature, which has a class
Point featurePoint = (Point) feature2.getGeometry().getGeometryContent().getValue();
String coords = featurePoint.getCoordinates().get(1);
assertTrue(coords.contains("class=\"desired\">"));
String coords = featurePoint.getCoordinates().get(0).getCoordinates().get(0).toString();
assertTrue(coords.contains("<span class=\"desired\">"));
} finally {
if (template != null) {
template.delete();
Expand All @@ -405,11 +406,11 @@ public void testMapMLFeatureLineHasClass() throws Exception {
File template = null;
try {
Catalog cat = getCatalog();
LayerInfo li = cat.getLayerByName(MockData.ROAD_SEGMENTS.getLocalPart());
LayerInfo li = cat.getLayerByName(MockData.MLINES.getLocalPart());
li.getResource().getMetadata().put(MAPML_USE_FEATURES, true);
li.getResource().getMetadata().put(MAPML_USE_TILES, false);
cat.save(li);
String layerId = getLayerId(MockData.ROAD_SEGMENTS);
String layerId = getLayerId(MockData.MLINES);
FeatureTypeInfo resource =
getCatalog().getResourceByName(layerId, FeatureTypeInfo.class);
File parent = getDataDirectory().get(resource).dir();
Expand All @@ -423,7 +424,7 @@ public void testMapMLFeatureLineHasClass() throws Exception {
+ "<map-feature>\n"
+ " <#list attributes as attribute>\n"
+ " <#if attribute.isGeometry>\n"
+ " <map-geometry><map-linestring><map-coordinates><#list attribute.rawValue.coordinates as coord><#if coord?index == 3>${space}<![CDATA[<span${space}class=\"desired\">]]>${coord.x}${space}${coord.y}<#elseif coord?index == 4>${coord.x}${space}${coord.y}<![CDATA[</span>]]><#else>${space}${coord.x}${space}${coord.y}</#if></#list></map-coordinates></map-linestring></map-geometry>\n"
+ " <map-geometry><map-linestring><map-coordinates><#list attribute.rawValue.coordinates as coord><#if coord?index == 3> <![CDATA[<span class=\"desired\">]]>${coord.x} ${coord.y}<#elseif coord?index == 4>${coord.x} ${coord.y}<![CDATA[</span>]]><#else> ${coord.x} ${coord.y}</#if></#list></map-coordinates></map-linestring></map-geometry>\n"
+ " </#if>\n"
+ " </#list>\n"
+ "</map-feature>\n"
Expand All @@ -432,9 +433,9 @@ public void testMapMLFeatureLineHasClass() throws Exception {
"UTF-8");
Mapml mapmlFeatures =
new MapMLWMSRequest()
.name(MockData.ROAD_SEGMENTS.getLocalPart())
.bbox("-180,-90,180,90")
.srs("EPSG:4326")
.name(MockData.MLINES.getLocalPart())
.bbox("500000,500000,500999,500999")
.srs("EPSG:32615")
.feature(true)
.getAsMapML();

Expand All @@ -445,9 +446,8 @@ public void testMapMLFeatureLineHasClass() throws Exception {
.get(0); // get the second feature, which has a class
LineString featureLine =
(LineString) feature2.getGeometry().getGeometryContent().getValue();
String coords = featureLine.getCoordinates().get(7);
assertTrue(coords.contains("class=\"desired\">"));
template.delete();
String coords = featureLine.getCoordinates().get(0).getCoordinates().get(0).toString();
assertTrue(coords.contains("<span class=\"desired\">"));
} finally {
if (template != null) {
template.delete();
Expand Down

0 comments on commit 32c267c

Please sign in to comment.