Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata extents API / Make configurable to display the metadata bboxes using geodesic extents for local projections. #7560

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2021 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -128,6 +128,7 @@ public class Settings {
public static final String REGION_GETMAP_MAPPROJ = "region/getmap/mapproj";
public static final String REGION_GETMAP_WIDTH = "region/getmap/width";
public static final String REGION_GETMAP_SUMMARY_WIDTH = "region/getmap/summaryWidth";
public static final String REGION_GETMAP_GEODESIC_EXTENTS = "region/getmap/useGeodesicExtents";
public static final String METADATA_WORKFLOW_ENABLE = "metadata/workflow/enable";
public static final String METADATA_WORKFLOW_DRAFT_WHEN_IN_GROUP = "metadata/workflow/draftWhenInGroup";
public static final String METADATA_WORKFLOW_ALLOW_SUBMIT_APPROVE_INVALID_MD = "metadata/workflow/allowSubmitApproveInvalidMd";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2022 Food and Agriculture Organization of the
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand All @@ -24,7 +24,6 @@
package org.fao.geonet.api.records.extent;

import jeeves.server.context.ServiceContext;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.fao.geonet.api.regions.GeomFormat;
import org.fao.geonet.exceptions.BadParameterEx;
Expand All @@ -33,31 +32,22 @@
import org.fao.geonet.kernel.region.RegionsDAO;
import org.fao.geonet.kernel.setting.SettingManager;
import org.fao.geonet.kernel.setting.Settings;
import org.fao.geonet.lib.Lib;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.locationtech.jts.awt.ShapeWriter;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.geotools.api.metadata.extent.Extent;
import org.geotools.api.metadata.extent.GeographicBoundingBox;
import org.geotools.api.metadata.extent.GeographicExtent;
import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
import org.geotools.api.referencing.operation.MathTransform;
import org.springframework.context.ApplicationContext;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Map;
import java.util.SortedSet;
Expand All @@ -84,35 +74,48 @@ public static AffineTransform worldToScreenTransform(Envelope mapExtent, Dimensi
return new AffineTransform(scaleX, 0.0d, 0.0d, -scaleY, tx, ty);
}

/**
* Returns a bounding box geometry.
*
* @param geom Bounding box geometry.
* @param srs Bounding box geometry srs.
* @param useGeodesicExtents false: returns the bounding box geometry as a rectangle (using min / max bounds).
* true: returns the bounding box geometry.
* @return bounding box geometry.
*/
public static Geometry getGeometryExtent(Geometry geom, String srs, boolean useGeodesicExtents) {
boolean isGlobalSrs = srs.equals("EPSG:4326") || srs.equals("EPSG:3857");

return (!isGlobalSrs && !useGeodesicExtents ? geom.getEnvelope() : geom);
}

/**
* Check if a geometry is in the domain of validity of a projection and if not return the
* intersection of the geometry with the coordinate system domain of validity.
*/
public static Geometry computeGeomInDomainOfValidity(Geometry geom, CoordinateReferenceSystem mapCRS) {
final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
final Extent domainOfValidity = mapCRS.getDomainOfValidity();
Geometry adjustedGeom = geom;
if (domainOfValidity != null) {
for (final GeographicExtent extent :
domainOfValidity.getGeographicElements()) {
if (Boolean.FALSE.equals(extent.getInclusion())) {
if ((extent == null) || (Boolean.FALSE.equals(extent.getInclusion()))) {
continue;
}

if (extent instanceof GeographicBoundingBox) {
if (extent != null) {
GeographicBoundingBox box = (GeographicBoundingBox) extent;

Envelope env = new Envelope(
box.getWestBoundLongitude(),
box.getEastBoundLongitude(),
box.getSouthBoundLatitude(),
box.getNorthBoundLatitude());
if (env.contains(geom.getEnvelopeInternal())) {
return geom;
} else {
Geometry extentPolygon = JTS.toGeometry(env);
adjustedGeom = geom.intersection(extentPolygon);
}
GeographicBoundingBox box = (GeographicBoundingBox) extent;

Envelope env = new Envelope(
box.getWestBoundLongitude(),
box.getEastBoundLongitude(),
box.getSouthBoundLatitude(),
box.getNorthBoundLatitude());
if (env.contains(geom.getEnvelopeInternal())) {
return geom;
} else {
Geometry extentPolygon = JTS.toGeometry(env);
adjustedGeom = geom.intersection(extentPolygon);
}
}
}
Expand All @@ -123,12 +126,15 @@ public static Geometry computeGeomInDomainOfValidity(Geometry geom, CoordinateRe
}

public BufferedImage render(String id, String srs, Integer width, Integer height,
String background, String geomParam, String geomType, String geomSrs, String fillColor, String strokeColor) throws Exception {
String background, String geomParam, String geomType,
String geomSrs, String fillColor, String strokeColor) throws Exception {
ApplicationContext appContext = context.getApplicationContext();
Map<String, String> regionGetMapBackgroundLayers = appContext.getBean("regionGetMapBackgroundLayers", Map.class);
SortedSet<ExpandFactor> regionGetMapExpandFactors = appContext.getBean("regionGetMapExpandFactors", SortedSet.class);
SettingManager settingManager = appContext.getBean(SettingManager.class);

boolean useGeodesicExtents = settingManager.getValueAsBool(Settings.REGION_GETMAP_GEODESIC_EXTENTS, false);

Geometry geom = null;
if (id != null) {
Collection<RegionsDAO> daos = context.getApplicationContext().getBeansOfType(RegionsDAO.class).values();
Expand Down Expand Up @@ -192,7 +198,6 @@ public BufferedImage render(String id, String srs, Integer width, Integer height
;

BufferedImage baseMapImage = baseMapRenderer.render();
// ImageIO.write(baseMapImage, "png", new File("delme.png"));

image = baseMapImage;
} else {
Expand All @@ -209,8 +214,9 @@ public BufferedImage render(String id, String srs, Integer width, Integer height
Color geomStrokeColor = getColor(strokeColor, new Color(0, 0, 0, 255));
AffineTransform worldToScreenTransform = worldToScreenTransform(bboxOfImage, imageDimensions);
for (int i = 0; i < geom.getNumGeometries(); i++) {
Geometry geomExtent = MapRenderer.getGeometryExtent(geom.getGeometryN(i), srs, useGeodesicExtents);
// draw each included geometry separately to ensure they are filled correctly
Shape shape = worldToScreenTransform.createTransformedShape(shapeWriter.toShape(geom.getGeometryN(i)));
Shape shape = worldToScreenTransform.createTransformedShape(shapeWriter.toShape(geomExtent));
graphics.setColor(geomFillColor);
graphics.fill(shape);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
/*
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
* Rome - Italy. email: geonetwork@osgeo.org
*/

package org.fao.geonet.api.records.extent;

import org.fao.geonet.kernel.region.Region;
Expand Down Expand Up @@ -48,4 +71,55 @@ public void domainIntersectsBoundingPolygon() throws Exception {
String result = wktWriter.write(MapRenderer.computeGeomInDomainOfValidity(bbox, Region.decodeCRS("EPSG:3857")));
assertEquals("POLYGON ((159.70909090909092 -85.06, 135 -76, 161 -76, 153 -81, 161.12 -85.06, 159.70909090909092 -85.06))", result);
}

@Test
public void extentGeodesicLocalProjection() throws Exception {
String test = "POLYGON ((646.3563610491983 308975.2885578188, 10545.528150276805 637111.0281460616, 276050.8102636032 636456.3084312443, 284347.2430806639 308289.5622343737, 646.3563610491983 308975.2885578188))";
String localSrs = "EPSG:28992";
Geometry geometry = wktReader.read(test);
geometry.setSRID(28992);

Geometry geometryExtent = MapRenderer.getGeometryExtent(geometry, localSrs, true);
assertEquals(geometry, geometryExtent);
}

@Test
public void extentNonGeodesicLocalProjection() throws Exception {
String test = "POLYGON ((646.3563610491983 308975.2885578188, 10545.528150276805 637111.0281460616, 276050.8102636032 636456.3084312443, 284347.2430806639 308289.5622343737, 646.3563610491983 308975.2885578188))";
String localSrs = "EPSG:28992";
Geometry geometry = wktReader.read(test);
geometry.setSRID(28992);

Geometry geometryExtent = MapRenderer.getGeometryExtent(geometry, localSrs, false);
assertEquals(geometry.getEnvelope(), geometryExtent);
}

@Test
public void extentGlobal3857Projection() throws Exception {
String test = "POLYGON ((159.70909090909092 -85.06, 135 -76, 161 -76, 153 -81, 161.12 -85.06, 159.70909090909092 -85.06))";
String globalSrs = "EPSG:3857";
Geometry geometry = wktReader.read(test);
geometry.setSRID(3857);

// For global projections
Geometry geometryExtent = MapRenderer.getGeometryExtent(geometry, globalSrs, true);
assertEquals(geometry, geometryExtent);

geometryExtent = MapRenderer.getGeometryExtent(geometry, globalSrs, false);
assertEquals(geometry, geometryExtent);
}

@Test
public void extentGlobal4326Projection() throws Exception {
String test = "POLYGON ((165 -87, 135 -76, 161 -76, 153 -81, 165 -87))";
String globalSrs = "EPSG:4326";
Geometry geometry = wktReader.read(test);
geometry.setSRID(4326);

Geometry geometryExtent = MapRenderer.getGeometryExtent(geometry, globalSrs, true);
assertEquals(geometry, geometryExtent);

geometryExtent = MapRenderer.getGeometryExtent(geometry, globalSrs, false);
assertEquals(geometry, geometryExtent);
}
}
2 changes: 2 additions & 0 deletions web-ui/src/main/resources/catalog/locales/en-admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,8 @@
"region/getmap/background": "Background map, URL or Named Layer ID",
"region/getmap/mapproj": "Map projection",
"region/getmap/summaryWidth": "Summary width",
"region/getmap/useGeodesicExtents": "Display geodesic extents",
"region/getmap/useGeodesicExtents-help": "By default, the displayed metadata extents are planar (i.e. rectangular). If you enable this option, the metadata extents will become geodesic. If the map uses a projected coordinate system, this may lead to non-rectangular extents (e.g. trapezoid).",
"region/getmap/width": "Width",
"metadata/editor": "Metadata editor configuration",
"metadata/editor/schemaConfig": "Standard configuration",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('region
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('region/getmap/width', '500', 0, 9590, 'n');
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('region/getmap/summaryWidth', '500', 0, 9590, 'n');
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('region/getmap/mapproj', 'EPSG:3857', 0, 9590, 'n');
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('region/getmap/useGeodesicExtents', 'false', 2, 9591, 'n');


INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/url/sitemapLinkUrl', NULL, 0, 9165, 'y');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
UPDATE Settings SET value='4.4.2' WHERE name='system/platform/version';
UPDATE Settings SET value='SNAPSHOT' WHERE name='system/platform/subVersion';

INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('region/getmap/useGeodesicExtents', 'false', 2, 9591, 'n');

ALTER TABLE public.spg_page ADD icon varchar NULL;
Loading