Skip to content

Commit

Permalink
Merge pull request #1026 from DenverCoder544/mydata_db_direct_refactored
Browse files Browse the repository at this point in the history
Myplaces/userlayerdata db direct refactored
  • Loading branch information
ZakarFin authored Nov 15, 2023
2 parents 2ffd08f + 565ec35 commit 26da508
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.json.JSONObject;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
Expand All @@ -29,7 +28,6 @@
import org.opengis.filter.expression.Expression;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.oskari.geojson.GeoJSONFeatureCollection;
import org.oskari.geojson.GeoJSONReader;
import org.oskari.permissions.PermissionService;
import org.oskari.permissions.model.PermissionType;
import org.oskari.permissions.model.ResourceType;
Expand Down Expand Up @@ -177,10 +175,11 @@ private boolean isVisibleProperty(String name) {
@Override
public SimpleFeatureCollection getFeatures(String layerId, OskariLayer layer, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException {
try {
JSONObject featureCollectionJSON = analysisDbService.getFeatures(parseId(layerId), bbox, crs);
SimpleFeatureCollection featureCollection = featureCollectionJSON == null ?
new EmptyFeatureCollection(null) :
GeoJSONReader.toFeatureCollection(featureCollectionJSON);
SimpleFeatureCollection featureCollection = analysisDbService.getFeatures(parseId(layerId), bbox, crs);
if (featureCollection == null) {
return new EmptyFeatureCollection(null);
}

return postProcess(featureCollection);
} catch(Exception e) {
throw new ServiceException("Failed to get features. ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,8 @@ private static MyPlace parseMyPlace(JSONObject feature, boolean shouldSetId, int

@Override
public SimpleFeatureCollection getFeatures(String layerId, OskariLayer layer, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException{
try {
int categoryId = parseId(layerId);
JSONObject featureCollectionJSON = featureService.getFeatures(categoryId, bbox, crs);

if (featureCollectionJSON == null) {
return new EmptyFeatureCollection(null);
}
SimpleFeatureCollection featureCollection = GeoJSONReader.toFeatureCollection(featureCollectionJSON);
return featureCollection != null ? featureCollection : new EmptyFeatureCollection(null);
} catch (JSONException e) {
throw new ServiceException("GetFeatures failed.", e);
}
int categoryId = parseId(layerId);
SimpleFeatureCollection featureCollection = featureService.getFeatures(categoryId, bbox, crs);
return featureCollection != null ? featureCollection : new EmptyFeatureCollection(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import fi.nls.oskari.domain.map.analysis.Analysis;
import fi.nls.oskari.service.OskariComponent;
import fi.nls.oskari.service.ServiceException;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.json.JSONObject;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.util.HashMap;
Expand All @@ -24,5 +24,5 @@ public abstract class AnalysisDbService extends OskariComponent {
public abstract void mergeAnalysis(final Analysis analysis, final List<Long> ids) throws ServiceException;
public abstract long updatePublisherName(final long id, final String uuid, final String name);

public abstract JSONObject getFeatures(int layerId, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException;
public abstract SimpleFeatureCollection getFeatures(int layerId, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.store.EmptyFeatureCollection;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.oskari.geojson.GeoJSON;
import org.oskari.geojson.GeoJSONWriter;

import javax.sql.DataSource;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static fi.nls.oskari.map.geometry.ProjectionHelper.getSRID;
import static fi.nls.oskari.map.geometry.WKTHelper.GEOM_ATTRIBUTE;
import static fi.nls.oskari.map.geometry.WKTHelper.parseWKT;

@Oskari
Expand Down Expand Up @@ -319,97 +323,108 @@ public long updatePublisherName(final long id, final String uuid, final String n
}

@Override
public JSONObject getFeatures(int layerId, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException {
public SimpleFeatureCollection getFeatures(int layerId, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException {
try (SqlSession session = factory.openSession()) {
log.debug("getFeatures by bbox: ", bbox);

final AnalysisMapper mapper = session.getMapper(AnalysisMapper.class);
String nativeSrsName = PropertyUtil.get("oskari.native.srs", "EPSG:3857");
String targetSrsName = crs.getIdentifiers()
.stream()
.filter(identifier -> identifier.getCodeSpace().equals("EPSG"))
.map(identifier -> identifier.getCodeSpace() + ":" + identifier.getCode())
.findFirst()
.orElse(null);

int nativeSrid = getSRID(nativeSrsName);
List<AnalysisData> features = mapper.findAllByBBOX(layerId, bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY(), nativeSrid);

JSONObject featureCollection = this.toGeoJSONFeatureCollection(features, targetSrsName != null ? targetSrsName : nativeSrsName);
return featureCollection;
return this.toSimpleFeatureCollection(features);
} catch (Exception e) {
log.warn(e, "Exception when trying to get features by bounding box ", bbox.getMinX(), bbox.getMinY(), bbox.getMaxX(), bbox.getMaxY());
throw new ServiceException(e.getMessage());
}
}

private JSONObject toGeoJSONFeatureCollection(List<AnalysisData> features, String targetSRSName) throws ServiceException {
if (features == null || features.isEmpty()) {
return null;
}
JSONObject json = new JSONObject();
private SimpleFeatureCollection toSimpleFeatureCollection(List<AnalysisData> features) throws ServiceException {
try {
json.put(GeoJSON.TYPE, GeoJSON.FEATURE_COLLECTION);
json.put("crs", geojsonWriter.writeCRSObject(targetSRSName));
if (features == null || features.isEmpty()) {
return new EmptyFeatureCollection(null);
}

JSONArray jsonFeatures = new JSONArray(features.stream().map(feature -> this.toGeoJSONFeature(feature, targetSRSName)).collect(Collectors.toList()));
json.put(GeoJSON.FEATURES, jsonFeatures);
DefaultFeatureCollection collection = new DefaultFeatureCollection();
for (AnalysisData feature: features) {
if (feature.getWkt() != null) {
collection.add(toSimpleFeature(feature));
}
}

} catch(JSONException ex) {
log.warn("Failed to create GeoJSON FeatureCollection");
throw new ServiceException("Failed to create GeoJSON FeatureCollection");
return collection;
} catch (Exception e) {
throw new ServiceException("Failed to create SimpleFeatureCollection");
}
return json;
}

private JSONObject toGeoJSONFeature(AnalysisData feature, String targetSRSName) {
JSONObject jsonFeature = new JSONObject();
JSONObject properties = new JSONObject();
try {
jsonFeature.put("id", feature.getId());
jsonFeature.put("geometry_name", GeoJSON.GEOMETRY);
jsonFeature.put(GeoJSON.TYPE, GeoJSON.FEATURE);

String sourceSRSName = "EPSG:" + feature.getDatabaseSRID();
Geometry transformed = WKTHelper.transform(parseWKT(feature.getWkt()), sourceSRSName, targetSRSName);
JSONObject geoJsonGeometry = geojsonWriter.writeGeometry(transformed);
jsonFeature.put(GeoJSON.GEOMETRY, geoJsonGeometry);

properties.put("id", feature.getId());
properties.put("analysis_id", feature.getAnalysisId());
properties.put("uuid", feature.getUuid());
properties.put("t1", feature.getT1());
properties.put("t2", feature.getT2());
properties.put("t3", feature.getT3());
properties.put("t4", feature.getT4());
properties.put("t5", feature.getT5());
properties.put("t6", feature.getT6());
properties.put("t7", feature.getT7());
properties.put("t8", feature.getT8());

properties.put("n1", feature.getN1());
properties.put("n2", feature.getN2());
properties.put("n3", feature.getN3());
properties.put("n4", feature.getN4());
properties.put("n5", feature.getN5());
properties.put("n6", feature.getN6());
properties.put("n7", feature.getN7());
properties.put("n8", feature.getN8());

properties.put("d1", feature.getD1());
properties.put("d2", feature.getD2());
properties.put("d3", feature.getD3());
properties.put("d4", feature.getD4());

properties.put("created", feature.getCreated());
properties.put("updated", feature.getUpdated());
jsonFeature.put("properties", properties);

} catch(JSONException ex) {
log.warn("Failed to convert UserLayerData to GeoJSONFeature");
}

return jsonFeature;
}
private SimpleFeature toSimpleFeature(AnalysisData feature) {
Geometry geom = parseWKT(feature.getWkt());
SimpleFeatureTypeBuilder featureTypeBuilder = getFeatureTypeBuilder(geom);
SimpleFeatureType simpleFeatureType = featureTypeBuilder.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(simpleFeatureType);
featureBuilder.set(GEOM_ATTRIBUTE, geom);

featureBuilder.set("id", feature.getId());
featureBuilder.set("analysis_id", feature.getAnalysisId());
featureBuilder.set("uuid", feature.getUuid());
featureBuilder.set("t1", feature.getT1());
featureBuilder.set("t2", feature.getT2());
featureBuilder.set("t3", feature.getT3());
featureBuilder.set("t4", feature.getT4());
featureBuilder.set("t5", feature.getT5());
featureBuilder.set("t6", feature.getT6());
featureBuilder.set("t7", feature.getT7());
featureBuilder.set("t8", feature.getT8());

featureBuilder.set("n1", feature.getN1());
featureBuilder.set("n2", feature.getN2());
featureBuilder.set("n3", feature.getN3());
featureBuilder.set("n4", feature.getN4());
featureBuilder.set("n5", feature.getN5());
featureBuilder.set("n6", feature.getN6());
featureBuilder.set("n7", feature.getN7());
featureBuilder.set("n8", feature.getN8());

featureBuilder.set("d1", feature.getD1());
featureBuilder.set("d2", feature.getD2());
featureBuilder.set("d3", feature.getD3());
featureBuilder.set("d4", feature.getD4());

featureBuilder.set("created", feature.getCreated());
featureBuilder.set("updated", feature.getUpdated());
return featureBuilder.buildFeature(Long.valueOf(feature.getId()).toString());
}

private SimpleFeatureTypeBuilder getFeatureTypeBuilder(Geometry geometry) {
SimpleFeatureTypeBuilder featureTypeBuilder = WKTHelper.getFeatureTypeBuilder(geometry);
featureTypeBuilder.add("id", Long.class);
featureTypeBuilder.add("analysis_id", Long.class);
featureTypeBuilder.add("uuid", String.class);
featureTypeBuilder.add("t1", String.class);
featureTypeBuilder.add("t2", String.class);
featureTypeBuilder.add("t3", String.class);
featureTypeBuilder.add("t4", String.class);
featureTypeBuilder.add("t5", String.class);
featureTypeBuilder.add("t6", String.class);
featureTypeBuilder.add("t7", String.class);
featureTypeBuilder.add("t8", String.class);

featureTypeBuilder.add("n1", Long.class);
featureTypeBuilder.add("n2", Long.class);
featureTypeBuilder.add("n3", Long.class);
featureTypeBuilder.add("n4", Long.class);
featureTypeBuilder.add("n5", Long.class);
featureTypeBuilder.add("n6", Long.class);
featureTypeBuilder.add("n7", Long.class);
featureTypeBuilder.add("n8", Long.class);

featureTypeBuilder.add("d1", OffsetDateTime.class);
featureTypeBuilder.add("d2", OffsetDateTime.class);
featureTypeBuilder.add("d3", OffsetDateTime.class);
featureTypeBuilder.add("d4", OffsetDateTime.class);

featureTypeBuilder.add("created", OffsetDateTime.class);
featureTypeBuilder.add("updated", OffsetDateTime.class);
return featureTypeBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPoint;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.oskari.geojson.GeoJSON;


/**
Expand All @@ -24,6 +31,8 @@ public class WKTHelper {
private static final Logger log = LogFactory.getLogger(WKTHelper.class);
public final static CoordinateReferenceSystem CRS_EPSG_4326 = getCRS(PROJ_EPSG_4326);

public final static String GEOM_ATTRIBUTE = "geometry";

private static final double INTERPOLATE_THRESHOLD = 1.0;
private static final double WGS84_LON_MIN = -180.0;
private static final double WGS84_LON_MAX= 180.0;
Expand Down Expand Up @@ -181,4 +190,47 @@ public static String getBBOX(final double westBoundLongitude, final double south
log.debug("BBOX: " + bbox);
return bbox;
}

public static SimpleFeatureTypeBuilder getFeatureTypeBuilder(Geometry geometry) {
SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
featureTypeBuilder.setName("userdata");
featureTypeBuilder.setNamespaceURI("http://oskari.org");
featureTypeBuilder.setDefaultGeometry(GEOM_ATTRIBUTE);

if (geometry != null) {
setFeatureTypeBuilderGeometry(featureTypeBuilder, geometry);
}

return featureTypeBuilder;
}

private static void setFeatureTypeBuilderGeometry(SimpleFeatureTypeBuilder featureTypeBuilder, Geometry geometry) {
if (geometry != null) {
featureTypeBuilder.setDefaultGeometry(GEOM_ATTRIBUTE);
switch (geometry.getGeometryType()) {
case GeoJSON.POINT:
featureTypeBuilder.add(GEOM_ATTRIBUTE, Point.class);
break;
case GeoJSON.LINESTRING:
featureTypeBuilder.add(GEOM_ATTRIBUTE, LineString.class);
break;
case GeoJSON.POLYGON:
featureTypeBuilder.add(GEOM_ATTRIBUTE, Polygon.class);
break;
case GeoJSON.MULTI_POINT:
featureTypeBuilder.add(GEOM_ATTRIBUTE, MultiPoint.class);
break;
case GeoJSON.MULTI_LINESTRING:
featureTypeBuilder.add(GEOM_ATTRIBUTE, MultiLineString.class);
break;
case GeoJSON.MULTI_POLYGON:
featureTypeBuilder.add(GEOM_ATTRIBUTE, MultiPolygon.class);
break;
case GeoJSON.GEOMETRY_COLLECTION:
featureTypeBuilder.add(GEOM_ATTRIBUTE, GeometryCollection.class);
break;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fi.nls.oskari.domain.map.MyPlace;
import fi.nls.oskari.service.ServiceException;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.json.JSONObject;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
Expand All @@ -18,7 +19,7 @@ public interface MyPlacesFeaturesService {
public JSONObject getFeaturesByUserId(String uuid, String crs) throws ServiceException;
public JSONObject getFeaturesByMyPlaceId(long[] ids, String crs) throws ServiceException;

public JSONObject getFeatures(int categoryId, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException;
public SimpleFeatureCollection getFeatures(int categoryId, ReferencedEnvelope bbox, CoordinateReferenceSystem crs) throws ServiceException;

/**
* Returns ids of inserted features
Expand Down
Loading

0 comments on commit 26da508

Please sign in to comment.