added WFSParameter and FeatureParser

This commit is contained in:
Francesco Mangiacrapa 2022-01-28 17:46:36 +01:00
parent a626c4e98c
commit 8e5ef77686
13 changed files with 589 additions and 16 deletions

View File

@ -6,17 +6,18 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">

View File

@ -1,7 +1,12 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error

12
pom.xml
View File

@ -28,6 +28,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<developers>
<developer>
<name>Francesco Mangiacrapa</name>
<email>francesco.mangiacrapa@isti.cnr.it</email>
<organization>CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo"</organization>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
</developer>
</developers>
<dependencies>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='geo-utility'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User' />
<!-- Specify the paths for translatable code -->
<source path='client' />
<source path='shared' />
</module>

View File

@ -0,0 +1,74 @@
package org.gcube.spatial.data.geoutility.shared.wfs;
import java.io.Serializable;
/**
* The Class FeatureGeometry.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Sep 6, 2021
*/
public class FeatureGeometry implements WFSGeometry, Serializable {
/**
*
*/
private static final long serialVersionUID = 8231377594468260590L;
private String type;
private String coordinatesJSON;
private String toJSON;
/**
* Instantiates a new feature geometry.
*/
public FeatureGeometry() {
}
/**
* Instantiates a new feature geometry.
*
* @param type the type
* @param coordinatesJSON the coordinates JSON
*/
public FeatureGeometry(String type, String coordinatesJSON) {
super();
this.type = type;
this.coordinatesJSON = coordinatesJSON;
}
public String getType() {
return type;
}
public String getCoordinatesJSON() {
return coordinatesJSON;
}
public void setType(String type) {
this.type = type;
}
public void setCoordinatesJSON(String coordinatesJSON) {
this.coordinatesJSON = coordinatesJSON;
}
public String getToJSONObject() {
if(toJSON==null)
toJSON = "{\"type\":\""+type+"\",\"coordinates\":"+coordinatesJSON+"}";
return toJSON;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("FeatureGeometry [type=");
builder.append(type);
builder.append(", coordinatesJSON=");
builder.append(coordinatesJSON);
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,89 @@
/**
*
*/
package org.gcube.spatial.data.geoutility.shared.wfs;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* The Class FeatureRow.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Oct 29, 2020
*/
public class FeatureRow implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8741559784800931747L;
private String id;
private Map<String, List<String>> mapProperties;
private FeatureGeometry geometry;
private String crsName;
public FeatureRow() {
}
public FeatureRow(Map<String, List<String>> mapProperties, FeatureGeometry geometry) {
super();
this.mapProperties = mapProperties;
this.setGeometry(geometry);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map<String, List<String>> getMapProperties() {
return mapProperties;
}
public void setMapProperties(Map<String, List<String>> mapProperties) {
this.mapProperties = mapProperties;
}
public String getCrsName() {
return crsName;
}
public void setCrsName(String crsName) {
this.crsName = crsName;
}
public FeatureGeometry getGeometry() {
return geometry;
}
public void setGeometry(FeatureGeometry geometry) {
this.geometry = geometry;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("FeatureRow [id=");
builder.append(id);
builder.append(", mapProperties=");
builder.append(mapProperties);
builder.append(", geometry=");
builder.append(geometry);
builder.append(", crsName=");
builder.append(crsName);
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,27 @@
package org.gcube.spatial.data.geoutility.shared.wfs;
/**
* The Interface WFSGeometry.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Sep 6, 2021
*/
public interface WFSGeometry {
/**
* Gets the type.
*
* @return the type
*/
String getType();
/**
* Gets the coordinates JSON.
*
* @return the coordinates JSON
*/
String getCoordinatesJSON();
}

View File

@ -0,0 +1,60 @@
package org.gcube.spatial.data.geoutility.shared.wfs;
/**
* The Enum WFSParameter with default value (parameter,value)
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jan 28, 2022
*/
public enum WFSParameter {
SERVICE("SERVICE", "WFS"),
VERSION("VERSION", "1.1.0"),
REQUEST("REQUEST", "GetFeature"),
TYPENAME("TYPENAME", ""),
STYLES("STYLES", ""),
BBOX("BBOX", "-180,-90,180,90"),
WIDTH("WIDTH", "676"),
HEIGHT("HEIGHT", "230"),
SRSNAME("srsName", "EPSG:4326"),
CRS("CRS","EPSG:4326"), //WMS 1.3.0 COMPLIANT
OUTPUTFORMAT("OUTPUTFORMAT", "application/json"),
MAXFEATURES("MAXFEATURES", "10"),
PROPERTYNAME("PROPERTYNAME",""),
CQL_FILTER("CQL_FILTER","");
private String parameter;
private String value;
/**
* Instantiates a new wfs parameters.
*
* @param parameter the parameter
* @param value the value
*/
WFSParameter(String parameter, String value) {
this.parameter = parameter;
this.value = value;
}
/**
* Gets the parameter.
*
* @return the parameter
*/
public String getParameter() {
return parameter;
}
/**
* Gets the value.
*
* @return the value
*/
public String getValue() {
return value;
}
}

View File

@ -0,0 +1,151 @@
/**
*
*/
package org.gcube.spatial.data.geoutility.wfs;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.gcube.spatial.data.geoutility.shared.wfs.FeatureGeometry;
import org.gcube.spatial.data.geoutility.shared.wfs.FeatureRow;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class FeatureParser.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jan 28, 2022
*/
public class FeatureParser {
private static Logger LOG = LoggerFactory.getLogger(FeatureParser.class);
/**
* Gets the WFS features.
*
* @param wfsEndPoint the wfs end point
* @param wfsQB the wfs QB
* @return the WFS features
*/
public static List<FeatureRow> getWFSFeatures(String wfsEndPoint, WFSQueryBuilder wfsQB) {
return getWFSFeatureProperties(wfsEndPoint + "?" + wfsQB.getQuery());
}
/**
* Gets the WFS feature properties.
*
* @param wfsURLRequest the wfs URL request
* @return the WFS feature properties
*/
@SuppressWarnings("unchecked")
private static List<FeatureRow> getWFSFeatureProperties(String wfsURLRequest) {
LOG.info("getWFSFeatureProperties for url: " + wfsURLRequest);
InputStream is = null;
List<FeatureRow> listFeaturesRow = new ArrayList<FeatureRow>();
try {
LOG.info("Built WFS URL: " + wfsURLRequest);
is = new URL(wfsURLRequest).openStream();
String jsonTxt = IOUtils.toString(is);
if (jsonTxt == null || jsonTxt.isEmpty()) {
jsonTxt = "{\"type\":\"FeatureCollection\",\"features\":[]}";
}
// get json object
JSONObject json = new JSONObject(jsonTxt);
// iterate features
JSONArray features = json.getJSONArray("features");
if (features.length() == 0) {
LOG.info("No features detected in the response, returning empty list");
return listFeaturesRow;
}
String featureCRSName = "";
try {
JSONObject crs = json.getJSONObject("crs");
JSONObject crsProp = crs.getJSONObject("properties");
featureCRSName = crsProp.getString("name");
LOG.info("Crs name found: " + featureCRSName);
} catch (Exception e) {
LOG.warn("Unable to read the field 'crs'");
}
LOG.info("Features are: " + features.length());
for (int i = 0; i < features.length(); i++) {
final FeatureRow row = new FeatureRow();
row.setCrsName(featureCRSName);
JSONObject theFeature = ((JSONObject) features.get(i));
LOG.debug("Building at index: " + i);
try {
String fetaureId = theFeature.getString("id");
row.setId(fetaureId);
JSONObject geometry = theFeature.getJSONObject("geometry");
String typeValue = geometry.getString("type");
FeatureGeometry fg = new FeatureGeometry();
fg.setType(typeValue);
try {
JSONArray coordinates = geometry.getJSONArray("coordinates");
String coordinateJSONString = coordinates.toString();
LOG.debug("coordinates are: " + coordinateJSONString);
fg.setCoordinatesJSON(coordinates.toString());
} catch (Exception e) {
LOG.warn("Not able to parse the 'coordinates' field: ", e);
}
row.setGeometry(fg);
} catch (Exception e) {
LOG.debug("Unable to parse geometry at index: " + i);
}
// // iterate properties
JSONObject properties = theFeature.getJSONObject("properties");
Map<String, List<String>> mapProperties = new HashMap<String, List<String>>();
@SuppressWarnings("unchecked")
Iterator<String> ii = properties.keys();
while (ii.hasNext()) {
String key = ii.next();
String value = properties.optString(key, "");
List<String> theValues = mapProperties.get(key);
if (theValues == null)
mapProperties.put(key, Arrays.asList(value));
else {
theValues.add(value);
mapProperties.put(key, theValues);
}
}
row.setMapProperties(mapProperties);
listFeaturesRow.add(row);
LOG.info("Added row " + row + " to exported properties");
}
} catch (IOException e) {
LOG.error("Error on requesting properties for url: " + wfsURLRequest, e);
} catch (JSONException e) {
LOG.error("Error on requesting properties for url: " + wfsURLRequest, e);
} finally {
IOUtils.closeQuietly(is);
}
LOG.info("Returning " + listFeaturesRow.size() + " features");
return listFeaturesRow;
}
}

View File

@ -0,0 +1,70 @@
package org.gcube.spatial.data.geoutility.wfs;
import java.util.List;
import org.gcube.spatial.data.geoutility.shared.wfs.WFSParameter;
/**
* The Class WFSQueryBuilder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jan 28, 2022
*/
public class WFSQueryBuilder {
private String query = "";
/**
* Instantiates a new WFS query builder.
*/
public WFSQueryBuilder() {
}
/**
* Adds the parameter.
*
* @param param the param
* @param value the value
*/
public void addParameter(WFSParameter param, String value) {
if (!query.isEmpty())
query += "&";
query += param + "=" + value;
}
/**
* Adds the parameter.
*
* @param param the param
* @param values the values
* @param separtor the separtor
*/
public void addParameter(WFSParameter param, List<String> values, String separtor) {
if (!query.isEmpty())
query += "&";
String value = "";
for (int i = 0; i < values.size() - 1; i++) {
value += values.get(i) + separtor;
}
value += values.get(values.size() - 1);
query += param + "=" + value;
}
/**
* Gets the query.
*
* @return the query
*/
public String getQuery() {
return query;
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='geo-utility'>
<!-- Inherit the core Web Toolkit stuff. -->
<inherits name='com.google.gwt.user.User' />
<!-- Specify the paths for translatable code -->
<source path='client' />
<source path='shared' />
</module>

View File

@ -3,23 +3,28 @@
*/
package org.gcube.spatial.data.geoutility;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.gcube.spatial.data.geoutility.shared.wfs.FeatureRow;
import org.gcube.spatial.data.geoutility.shared.wfs.WFSParameter;
import org.gcube.spatial.data.geoutility.wfs.FeatureParser;
import org.gcube.spatial.data.geoutility.wfs.WFSQueryBuilder;
import org.gcube.spatial.data.geoutility.wms.WmsUrlValidator;
import org.junit.Test;
/**
* The Class GeoJUnitTest.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jan 22, 2016
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jan 22, 2016
*/
public class GeoJUnitTest {
/**
* Test get styles.
*/
@Test
//@Test
public void testGetStyles() {
// String wmsRequest = "http://repoigg.services.iit.cnr.it:8080/geoserver/IGG/ows?service=wms&version=1.1.0&request=GetMap&layers==IGG:area_temp_1000&width=676&height=330&srs=EPSG:4326&crs=EPSG:4326&format=application/openlayers&bbox=-85.5,-180.0,90.0,180.0";
// String wmsRequest = "http://thredds-d-d4s.d4science.org/thredds/wms/public/netcdf/test20.nc?service=wms&version=1.3.0&request=GetMap&layers=analyzed_field&styles=&width=640&height=480&srs=EPSG:4326&CRS=EPSG:4326&format=image/png&COLORSCALERANGE=auto&bbox=-85.0,-180.0,85.0,180.0";
@ -28,16 +33,14 @@ public class GeoJUnitTest {
GeoNcWMSMetadataUtility geo;
try {
geo = new GeoNcWMSMetadataUtility(wmsRequest);
System.out.println("Returned styles: "+geo.loadStyles());
}
catch (Exception e) {
System.out.println("Returned styles: " + geo.loadStyles());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Test get styles.
*/
@ -49,9 +52,8 @@ public class GeoJUnitTest {
GeoNcWMSMetadataUtility geo;
try {
geo = new GeoNcWMSMetadataUtility(wmsRequest);
System.out.println("Returned Z-Axis: "+geo.loadZAxis());
}
catch (Exception e) {
System.out.println("Returned Z-Axis: " + geo.loadZAxis());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@ -68,9 +70,65 @@ public class GeoJUnitTest {
WmsUrlValidator wms;
try {
wms = new WmsUrlValidator(wmsRequest);
System.out.println("Returned wms: "+wms.toString());
System.out.println("Returned wms: " + wms.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
}
//@Test
public void testWFSRequest() {
String wfsEndPoint = "https://geoserver1.dev.d4science.org/geoserver/devvre/wfs";
try {
//REGIONI
String layerName = "devvre:Reg01012021_g_WGS84";
List<String> propertyName = new ArrayList<String>();
propertyName.add("devvre:COD_REG");
propertyName.add("devvre:DEN_REG");
//Querying list of Regioni
WFSQueryBuilder queryBuilder = new WFSQueryBuilder();
queryBuilder.addParameter(WFSParameter.SERVICE, WFSParameter.SERVICE.getValue());
queryBuilder.addParameter(WFSParameter.VERSION, "2.0.0");
queryBuilder.addParameter(WFSParameter.REQUEST, "GetFeature");
queryBuilder.addParameter(WFSParameter.TYPENAME, layerName);
queryBuilder.addParameter(WFSParameter.OUTPUTFORMAT, WFSParameter.OUTPUTFORMAT.getValue());
queryBuilder.addParameter(WFSParameter.PROPERTYNAME, propertyName, ",");
FeatureParser fp = new FeatureParser();
List<FeatureRow> rows = fp.getWFSFeatures(wfsEndPoint, queryBuilder);
System.out.println("Regioni: ");
for (FeatureRow featureRow : rows) {
System.out.println(featureRow);
}
//PROVINCE
layerName = "devvre:ProvCM01012021_g_WGS84";
propertyName = new ArrayList<String>();
propertyName.add("devvre:COD_REG");
propertyName.add("devvre:COD_PROV");
propertyName.add("devvre:DEN_PROV");
propertyName.add("devvre:DEN_CM"); //è pieno per i capoluoghi di provincia
queryBuilder = new WFSQueryBuilder();
queryBuilder.addParameter(WFSParameter.SERVICE, WFSParameter.SERVICE.getValue());
queryBuilder.addParameter(WFSParameter.VERSION, "2.0.0");
queryBuilder.addParameter(WFSParameter.REQUEST, "GetFeature");
queryBuilder.addParameter(WFSParameter.TYPENAME, layerName);
queryBuilder.addParameter(WFSParameter.OUTPUTFORMAT, WFSParameter.OUTPUTFORMAT.getValue());
queryBuilder.addParameter(WFSParameter.PROPERTYNAME, propertyName, ",");
queryBuilder.addParameter(WFSParameter.CQL_FILTER, URLEncoder.encode("devvre:COD_REG=9", "UTF-8"));
fp = new FeatureParser();
rows = fp.getWFSFeatures(wfsEndPoint, queryBuilder);
System.out.println("PROVINCE: ");
for (FeatureRow featureRow : rows) {
System.out.println(featureRow);
}
System.out.println(queryBuilder.getQuery());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

6
src/test/resources/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/devNext.gcubekey
/devsec.gcubekey
/gcube.gcubekey
/log4j.properties
/pred4s.gcubekey
/preprod.gcubekey