You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by mi...@apache.org on 2022/11/30 14:59:55 UTC

[streampipes] 04/11: [STREAMPIPES-642] 1 class for distance calculation. adjust ressources

This is an automated email from the ASF dual-hosted git repository.

micklich pushed a commit to branch STREAMPIPES-642
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit bcf5f4a2bc186c51cb964f2595a608a1c68265bf
Author: micklich <mi...@apache.org>
AuthorDate: Tue Nov 29 16:17:06 2022 +0100

    [STREAMPIPES-642]  1 class for distance calculation. adjust ressources
---
 .../streampipes/processors/geo/jvm/GeoJvmInit.java |   4 +-
 .../DistanceCalculatorProcessor.java               | 104 +++++++++++++++++++++
 .../documentation.md                               |   8 +-
 .../icon.png                                       | Bin 0 -> 14322 bytes
 .../strings.en                                     |  17 ++--
 5 files changed, 119 insertions(+), 14 deletions(-)

diff --git a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/GeoJvmInit.java b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/GeoJvmInit.java
index 815ada965..55f990827 100644
--- a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/GeoJvmInit.java
+++ b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/GeoJvmInit.java
@@ -32,7 +32,7 @@ import org.apache.streampipes.processors.geo.jvm.config.ConfigKeys;
 import org.apache.streampipes.processors.geo.jvm.jts.processor.epsg.EpsgProcessor;
 import org.apache.streampipes.processors.geo.jvm.jts.processor.latlngtojtspoint.LatLngToJtsPointProcessor;
 import org.apache.streampipes.processors.geo.jvm.jts.processor.trajectory.TrajectoryFromPointsProcessor;
-import org.apache.streampipes.processors.geo.jvm.processor.distancecalculator.DistanceCalculatorController;
+import org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator.DistanceCalculatorProcessor;
 import org.apache.streampipes.processors.geo.jvm.processor.geocoder.GoogleMapsGeocodingController;
 import org.apache.streampipes.processors.geo.jvm.processor.revgeocoder.ReverseGeocodingController;
 import org.apache.streampipes.processors.geo.jvm.processor.speed.SpeedCalculatorController;
@@ -48,7 +48,7 @@ public class GeoJvmInit extends StandaloneModelSubmitter {
             "",
             8090)
             .registerPipelineElements(
-                    new DistanceCalculatorController(),
+                    new DistanceCalculatorProcessor(),
                     new GoogleMapsGeocodingController(),
                     new StaticGoogleMapsGeocodingController(),
                     new ReverseGeocodingController(),
diff --git a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/latlong/processor/distancecalculator/DistanceCalculatorProcessor.java b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/latlong/processor/distancecalculator/DistanceCalculatorProcessor.java
new file mode 100644
index 000000000..0b5e8e73b
--- /dev/null
+++ b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/latlong/processor/distancecalculator/DistanceCalculatorProcessor.java
@@ -0,0 +1,104 @@
+/*
+ * 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.latlong.processor.distancecalculator;
+
+import org.apache.streampipes.commons.exceptions.SpRuntimeException;
+import org.apache.streampipes.model.DataProcessorType;
+import org.apache.streampipes.model.graph.DataProcessorDescription;
+import org.apache.streampipes.model.runtime.Event;
+import org.apache.streampipes.model.schema.PropertyScope;
+import org.apache.streampipes.processors.geo.jvm.latlong.helper.HaversineDistanceUtil;
+import org.apache.streampipes.sdk.builder.ProcessingElementBuilder;
+import org.apache.streampipes.sdk.builder.StreamRequirementsBuilder;
+import org.apache.streampipes.sdk.helpers.EpProperties;
+import org.apache.streampipes.sdk.helpers.EpRequirements;
+import org.apache.streampipes.sdk.helpers.Labels;
+import org.apache.streampipes.sdk.helpers.Locales;
+import org.apache.streampipes.sdk.helpers.OutputStrategies;
+import org.apache.streampipes.sdk.utils.Assets;
+import org.apache.streampipes.vocabulary.Geo;
+import org.apache.streampipes.vocabulary.SO;
+import org.apache.streampipes.wrapper.context.EventProcessorRuntimeContext;
+import org.apache.streampipes.wrapper.routing.SpOutputCollector;
+import org.apache.streampipes.wrapper.standalone.ProcessorParams;
+import org.apache.streampipes.wrapper.standalone.StreamPipesDataProcessor;
+
+public class DistanceCalculatorProcessor extends StreamPipesDataProcessor {
+    private static final String LAT_1_KEY = "lat1";
+    private static final String LONG_1_KEY = "long1";
+    private static final String LAT_2_KEY = "lat2";
+    private static final String LONG_2_KEY = "long2";
+    private static final String CALCULATED_DISTANCE_KEY = "calculatedDistance";
+    String lat1FieldMapper;
+    String long1FieldMapper;
+    String lat2FieldMapper;
+    String long2FieldMapper;
+
+    @Override
+    public DataProcessorDescription declareModel() {
+        return ProcessingElementBuilder.create(
+                        "org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator")
+                .category(DataProcessorType.GEO)
+                .withAssets(Assets.DOCUMENTATION)
+                .withLocales(Locales.EN)
+                .requiredStream(StreamRequirementsBuilder
+                        .create()
+                        .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lat),
+                                Labels.withId(LAT_1_KEY), PropertyScope.MEASUREMENT_PROPERTY)
+                        .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lng),
+                                Labels.withId(LONG_1_KEY), PropertyScope.MEASUREMENT_PROPERTY)
+                        .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lat),
+                                Labels.withId(LAT_2_KEY), PropertyScope.MEASUREMENT_PROPERTY)
+                        .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lng),
+                                Labels.withId(LONG_2_KEY), PropertyScope.MEASUREMENT_PROPERTY).build()).outputStrategy(
+                        OutputStrategies.append(
+                                EpProperties.numberEp(Labels.withId(CALCULATED_DISTANCE_KEY), "distance", SO.Number)))
+                .build();
+    }
+
+    @Override
+    public void onInvocation(ProcessorParams parameters, SpOutputCollector spOutputCollector,
+                             EventProcessorRuntimeContext runtimeContext) throws SpRuntimeException {
+
+        this.lat1FieldMapper = parameters.extractor().mappingPropertyValue(LAT_1_KEY);
+        this.long1FieldMapper = parameters.extractor().mappingPropertyValue(LONG_1_KEY);
+        this.lat2FieldMapper = parameters.extractor().mappingPropertyValue(LAT_2_KEY);
+        this.long2FieldMapper = parameters.extractor().mappingPropertyValue(LONG_2_KEY);
+
+    }
+
+    @Override
+    public void onEvent(Event event, SpOutputCollector collector) throws SpRuntimeException {
+
+        float lat1 = event.getFieldBySelector(lat1FieldMapper).getAsPrimitive().getAsFloat();
+        float long1 = event.getFieldBySelector(long1FieldMapper).getAsPrimitive().getAsFloat();
+        float lat2 = event.getFieldBySelector(lat2FieldMapper).getAsPrimitive().getAsFloat();
+        float long2 = event.getFieldBySelector(long2FieldMapper).getAsPrimitive().getAsFloat();
+
+        double resultDist = HaversineDistanceUtil.dist(lat1, long1, lat2, long2);
+
+        event.addField("distance", resultDist);
+
+        collector.collect(event);
+    }
+
+    @Override
+    public void onDetach() throws SpRuntimeException {
+
+    }
+}
diff --git a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.processor.distancecalculator/documentation.md b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/documentation.md
similarity index 91%
rename from streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.processor.distancecalculator/documentation.md
rename to streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/documentation.md
index 5821bbfb4..ae7382554 100644
--- a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.processor.distancecalculator/documentation.md
+++ b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/documentation.md
@@ -16,7 +16,7 @@
   ~
   -->
 
-## Distance Calculator
+## Haversine Distance Calculator
 
 <p align="center"> 
     <img src="icon.png" width="150px;" class="pe-image-documentation"/>
@@ -25,7 +25,11 @@
 ***
 
 ## Description
-Calculates the distance between two latitude/longitude pairs in a single event.
+Calculates the distance between two latitude/longitude pairs in a single event with the
+<a href="https://en.wikipedia.org/wiki/Haversine_formula" target="_blank">Haversine formula</a>.
+ 
+
+
 
 ***
 
diff --git a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/icon.png b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/icon.png
new file mode 100644
index 000000000..3876342d8
Binary files /dev/null and b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/icon.png differ
diff --git a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.processor.distancecalculator/strings.en b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/strings.en
similarity index 65%
rename from streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.processor.distancecalculator/strings.en
rename to streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/strings.en
index c39ad9687..a94f6f895 100644
--- a/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.processor.distancecalculator/strings.en
+++ b/streampipes-extensions/streampipes-processors-geo-jvm/src/main/resources/org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator/strings.en
@@ -16,20 +16,17 @@
 #
 
 
-org.apache.streampipes.processors.geo.jvm.processor.distancecalculator.title=Distance Calculator
-org.apache.streampipes.processors.geo.jvm.processor.distancecalculator.description=Calculates the distance between two geo points
+org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator.title=Haversine Distance Calculator
+org.apache.streampipes.processors.geo.jvm.latlong.processor.distancecalculator.description=Calculates the distance between two geo points with the Haversine formula
 
-lat1.title=First Latitude
+lat1.title=First Latitude Position 1
 lat1.description=
 
-long1.title=First Longitude
+long1.title=First Longitude Position 1
 long1.description=
 
-lat2.title=Second Latitude
+lat2.title=Second Latitude Position 2
 lat2.description=
 
-long2.title=Second Longitude
-long2.description=
-
-calculatedDistance.title=Distance Field
-startingCell.description=
\ No newline at end of file
+long2.title=Second Longitude Position 2
+long2.description=
\ No newline at end of file