flomickl commented on code in PR #146: URL: https://github.com/apache/streampipes/pull/146#discussion_r1027325623
########## streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/jts/processor/reprojection/ProjTransformation.java: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.streampipes.processors.geo.jvm.jts.processor.reprojection; + +import org.apache.streampipes.commons.exceptions.SpRuntimeException; +import org.apache.streampipes.logging.api.Logger; +import org.apache.streampipes.model.runtime.Event; +import org.apache.streampipes.processors.geo.jvm.jts.exceptions.SpNotSupportedGeometryException; +import org.apache.streampipes.processors.geo.jvm.jts.helper.SpGeometryBuilder; +import org.apache.streampipes.processors.geo.jvm.jts.helper.SpReprojectionBuilder; +import org.apache.streampipes.processors.geo.jvm.jts.processor.latLngToGeo.LatLngToGeoParameter; +import org.apache.streampipes.wrapper.context.EventProcessorRuntimeContext; +import org.apache.streampipes.wrapper.routing.SpOutputCollector; +import org.apache.streampipes.wrapper.runtime.EventProcessor; + +import org.apache.sis.setup.Configuration; +import org.locationtech.jts.geom.Geometry; +import org.opengis.util.FactoryException; +import org.postgresql.ds.PGSimpleDataSource; + +import javax.sql.DataSource; + +public class ProjTransformation implements EventProcessor<ProjTransformationParameter> { + + private static Logger logger; + private ProjTransformationParameter params; + private Integer targetEPSG; + + @Override + public void onInvocation(ProjTransformationParameter params, SpOutputCollector spOutputCollector, + EventProcessorRuntimeContext runtimeContext) { + logger = params.getGraph().getLogger(LatLngToGeoParameter.class); + this.params = params; + targetEPSG = params.getTargetEpsg(); + + //TODO: this has to move to a central place in the streampipes backend + // otherwise Connection to SpatialMetadata database is already initialized occur + + try { + Configuration.current().setDatabase(ProjTransformation::createDataSource); + } catch (IllegalStateException e) { + logger.info("Setup was already established"); + // catch the exceptions due connection is already initialized. + } + + + // check if SIS DB is set up with imported data or is null + try { + if (SpReprojectionBuilder.isSisConfigurationValid()){ + logger.info("SIS DB Settings successful checked "); + } else { + logger.warn("The required EPSG database is not imported"); + //TODO implement fallback option with proj4j + throw new SpRuntimeException("Database not set and ready for fallback "); + } + } catch (FactoryException e) { + throw new SpRuntimeException("Something unexpected happened " + e); + } + + // check if SIS DB has the supported 9.9.1 Version. + try { + if (!SpReprojectionBuilder.isSisDbCorrectVersion()) { + logger.warn("Not supported EPSG DB used."); + throw new SpRuntimeException("Your current EPSG DB version " + SpReprojectionBuilder.getSisDbVersion() + + " is not the supported 9.9.1 version. "); + } + } catch (FactoryException e) { + throw new SpRuntimeException("Something unexpected happened " + e); + } + + // checks if Input EPSG in valid and exists in EPSG DB + if (!SpReprojectionBuilder.isSisEpsgValid(targetEPSG)) { + throw new SpRuntimeException("Your chosen EPSG Code " + targetEPSG + " is not valid or supported. " + + "Check EPSG on https://spatialreference.org"); + } + } + + @Override + public void onEvent(Event in, SpOutputCollector out) { + + String wkt = in.getFieldBySelector(params.getWktString()).getAsPrimitive().getAsString(); + Integer epsgCode = in.getFieldBySelector(params.getEpsgCode()).getAsPrimitive().getAsInt(); + Geometry geometry = SpGeometryBuilder.createSPGeom(wkt, epsgCode); + + Geometry transformed = null; + try { + transformed = SpReprojectionBuilder.reprojectSpGeometry(geometry, targetEPSG); + } catch (SpNotSupportedGeometryException e) { + transformed = SpGeometryBuilder.createEmptyGeometry(geometry); + } + + if (!transformed.isEmpty()) { + in.updateFieldBySelector("s0::" + ProjTransformationController.EPSG_RUNTIME, params.getTargetEpsg()); + in.updateFieldBySelector("s0::" + ProjTransformationController.WKT_RUNTIME, transformed.toText()); + + out.collect(in); + } else { + logger.warn("An empty point geometry is created in " + ProjTransformationController.EPA_NAME + " " + + "due invalid input values. Check used epsg Code:" + epsgCode); + } + } + + @Override + public void onDetach() { + } + + // https://sis.apache.org/apidocs/org/apache/sis/setup/Configuration.html#setDatabase(java.util.function.Supplier) + // TODO: Best would be ConfigKeys VARIABLE. ATM hardcoded and adjustments for development required like IP of client + // TODO: has to move together with the Configuration.current() method + protected static DataSource createDataSource() { + PGSimpleDataSource ds = new PGSimpleDataSource(); + // HAS TO BE ADJUSTED OR INCLUDED IN THE AUTO_DISCOVERY + String[] serverAddresses = {"192.168.178.100"}; Review Comment: Can you show me an example with the consul variables please? BTW is it save to put this and the cretaConenction in the GeoJvmInit ? This method just has to be called ones during "start" So it can be used by all processors. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
