Skip to content

Commit

Permalink
Merge pull request #917 from ZakarFin/metadata-wkt
Browse files Browse the repository at this point in the history
Cleanup and add sanity check for coordinate handling from CSW
  • Loading branch information
ZakarFin authored Jan 30, 2023
2 parents 461d8cb + de42329 commit 2ddfc0a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.net.HttpURLConnection;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static fi.nls.oskari.csw.service.CSWService.PROP_SERVICE_URL;
Expand Down Expand Up @@ -157,24 +158,16 @@ private Stream<Element> getResults(Element root) {
}

public ChannelSearchResult parseResults(Element root, SearchCriteria searchCriteria) {

ChannelSearchResult channelSearchResult = new ChannelSearchResult();
final String srs = searchCriteria.getSRS();
try {
final long start = System.currentTimeMillis();
getResults(root).forEach(metadata -> {
try {
SearchResultItem item = RESULT_PARSER.parseResult(metadata);
channelSearchResult.addItem(item);
// add coverage area if we can transform it
item.addValue("geom", getWKT(item, WKTHelper.PROJ_EPSG_4326, srs));
} catch (Exception e) {
String msg = "Error parsing metadata search result item or transform coverage area";
log.info(msg, ":", e.getMessage());
log.debug(e, msg);
}
});

List<SearchResultItem> results = getResults(root)
.map(metadata -> RESULT_PARSER.parseResult(metadata))
.collect(Collectors.toList());
channelSearchResult.getSearchResultItems().addAll(results);
// enhance results with coverage geometry
results.forEach(item -> item.addValue("geom", getWKT(item, WKTHelper.PROJ_EPSG_4326, srs)));
final long end = System.currentTimeMillis();
log.debug("Parsing metadata results took", (end-start), "ms");
channelSearchResult.setQueryFailed(false);
Expand Down Expand Up @@ -213,11 +206,12 @@ private String getWKT(final SearchResultItem item, final String sourceSRS, final
Geometry projected = JTS.transform(polygon, mt);

return WKTHelper.getWKT(projected);
} catch(Exception e) {
} catch (Exception e) {
log.error("Unable to transform BBOX WKT:", e.getMessage());
} catch (OutOfMemoryError oom) {
log.warn("OutOfMemoryError with bbox:",
"w:", x1, "s:", y1, "e:", x2, "n:", y2);
throw oom;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import fi.mml.portti.service.search.SearchResultItem;
import fi.nls.oskari.control.metadata.MetadataField;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.util.JSONHelper;
import org.oskari.xml.XmlHelper;
import org.json.JSONObject;
import org.w3c.dom.Element;
Expand All @@ -20,6 +19,8 @@ public class MetadataCatalogueResultParser {
public static final String KEY_IDENTIFICATION_DATE = "date";
public static final String KEY_IDENTIFICATION_CODELIST = "code";
public static final String KEY_NATUREOFTHETARGET = "natureofthetarget";
private static final int MAX_LONGITUDE = 180;
private static final int MAX_LATITUDE = 90;
// we need to map languages from 3-letter codes to 2-letter codes so initialize a global codeMapping property
private final static Map<String, String> ISO3letterOskariLangMapping = new HashMap<>();

Expand All @@ -33,7 +34,7 @@ public MetadataCatalogueResultParser() {
}
}

public SearchResultItem parseResult(final Element elem) throws Exception {
public SearchResultItem parseResult(final Element elem) {
final SearchResultItem item = new SearchResultItem();
// id / uuid
String uuid = XmlHelper.getChildValue(
Expand Down Expand Up @@ -94,8 +95,8 @@ public SearchResultItem parseResult(final Element elem) throws Exception {
"codeListValue");

JSONObject identification = new JSONObject();
identification.put(KEY_IDENTIFICATION_CODELIST, dateType);
identification.put(KEY_IDENTIFICATION_DATE, date);
JSONHelper.putValue(identification, KEY_IDENTIFICATION_CODELIST, dateType);
JSONHelper.putValue(identification, KEY_IDENTIFICATION_DATE, date);
item.addValue(KEY_IDENTIFICATION, identification);

item.setDescription(XmlHelper.getChildValue(
Expand Down Expand Up @@ -137,24 +138,70 @@ public SearchResultItem parseResult(final Element elem) throws Exception {
<gmd:northBoundLatitude>
<gco:Decimal>60.29783894</gco:Decimal>
</gmd:northBoundLatitude>
*/
private void setupBBox(final SearchResultItem item, final Element bbox) {
if (bbox == null) {
return;
}
item.setWestBoundLongitude(XmlHelper.getChildValue(
XmlHelper.getFirstChild(bbox, "westBoundLongitude"),
"Decimal"));
item.setEastBoundLongitude(XmlHelper.getChildValue(
XmlHelper.getFirstChild(bbox, "eastBoundLongitude"),
"Decimal"));

item.setSouthBoundLatitude(XmlHelper.getChildValue(
XmlHelper.getFirstChild(bbox, "southBoundLatitude"),
"Decimal"));

item.setNorthBoundLatitude(XmlHelper.getChildValue(
XmlHelper.getFirstChild(bbox, "northBoundLatitude"),
"Decimal"));
item.setWestBoundLongitude(getSanitizedValue(getBboxValue(bbox, "westBoundLongitude"), MAX_LONGITUDE));
item.setEastBoundLongitude(getSanitizedValue(getBboxValue(bbox, "eastBoundLongitude"), MAX_LONGITUDE));
item.setSouthBoundLatitude(getSanitizedValue(getBboxValue(bbox, "southBoundLatitude"), MAX_LATITUDE));
item.setNorthBoundLatitude(getSanitizedValue(getBboxValue(bbox, "northBoundLatitude"), MAX_LATITUDE));
}
private String getBboxValue(Element bbox, String coord) {
return XmlHelper.getChildValue(
XmlHelper.getFirstChild(bbox, coord),
"Decimal");
}
/*
Some services might have wacky values though so let's filter them out.
Missing values:
<gmd:geographicElement>
<gmd:EX_GeographicBoundingBox>
<gmd:westBoundLongitude>
<gco:Decimal />
</gmd:westBoundLongitude>
<gmd:eastBoundLongitude>
<gco:Decimal />
</gmd:eastBoundLongitude>
<gmd:southBoundLatitude>
<gco:Decimal />
</gmd:southBoundLatitude>
<gmd:northBoundLatitude>
<gco:Decimal />
</gmd:northBoundLatitude>
</gmd:EX_GeographicBoundingBox>
</gmd:geographicElement>
These will make interpolated WKT for OutOfMemory:
<gmd:geographicElement>
<gmd:EX_GeographicBoundingBox>
<gmd:westBoundLongitude>
<gco:Decimal>-340282346638529000000000000000000000000</gco:Decimal>
</gmd:westBoundLongitude>
<gmd:eastBoundLongitude>
<gco:Decimal>340282346638529000000000000000000000000</gco:Decimal>
</gmd:eastBoundLongitude>
<gmd:southBoundLatitude>
<gco:Decimal>-340282346638529000000000000000000000000</gco:Decimal>
</gmd:southBoundLatitude>
<gmd:northBoundLatitude>
<gco:Decimal>340282346638529000000000000000000000000</gco:Decimal>
</gmd:northBoundLatitude>
</gmd:EX_GeographicBoundingBox>
</gmd:geographicElement>
*/
private Double getSanitizedValue(String decimal, int max) {
Double value = null;
try {
value = Double.parseDouble(decimal);
} catch (Exception ignored) {}
if (value != null && Math.abs(value) > max) {
value = null;
}
return value;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public void setWestBoundLongitude(String westBoundLongitude) {
this.westBoundLongitude = null;
}
}
public void setWestBoundLongitude(double westBoundLongitude) {
public void setWestBoundLongitude(Double westBoundLongitude) {
this.westBoundLongitude = westBoundLongitude;
}
public Double getSouthBoundLatitude() {
Expand All @@ -325,7 +325,7 @@ public void setSouthBoundLatitude(String southBoundLatitude) {
this.southBoundLatitude = null;
}
}
public void setSouthBoundLatitude(double southBoundLatitude) {
public void setSouthBoundLatitude(Double southBoundLatitude) {
this.southBoundLatitude = southBoundLatitude;
}
public Double getEastBoundLongitude() {
Expand All @@ -342,7 +342,7 @@ public void setEastBoundLongitude(String eastBoundLongitude) {
this.eastBoundLongitude = null;
}
}
public void setEastBoundLongitude(double eastBoundLongitude) {
public void setEastBoundLongitude(Double eastBoundLongitude) {
this.eastBoundLongitude = eastBoundLongitude;
}
public Double getNorthBoundLatitude() {
Expand All @@ -359,7 +359,7 @@ public void setNorthBoundLatitude(String northBoundLatitude) {
this.northBoundLatitude = null;
}
}
public void setNorthBoundLatitude(double northBoundLatitude) {
public void setNorthBoundLatitude(Double northBoundLatitude) {
this.northBoundLatitude = northBoundLatitude;
}

Expand Down

0 comments on commit 2ddfc0a

Please sign in to comment.