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 2020/05/16 10:46:55 UTC

[incubator-streampipes-extensions] 04/04: improved static distance calculation suing new utility class SpDistanceCalculatior

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

micklich pushed a commit to branch feature/geodesicCalc
in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git

commit c70ad4d5a13b5fa2c59047914d1a5b3ba5e57b9f
Author: micklich <fl...@disy.net>
AuthorDate: Sat May 16 12:45:54 2020 +0200

    improved static distance calculation suing new utility class SpDistanceCalculatior
---
 .../StaticDistanceCalculator.java                  | 28 +++++--
 .../StaticDistanceCalculatorController.java        | 96 +++++++++++++++-------
 .../StaticDistanceCalculatorParameters.java        | 27 ++++--
 3 files changed, 106 insertions(+), 45 deletions(-)

diff --git a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculator.java b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculator.java
index 2889388..1ee19c4 100644
--- a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculator.java
+++ b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculator.java
@@ -18,7 +18,7 @@ package org.apache.streampipes.processors.geo.jvm.processor.staticdistancecalcul
 
 import org.apache.streampipes.commons.exceptions.SpRuntimeException;
 import org.apache.streampipes.model.runtime.Event;
-import org.apache.streampipes.processors.geo.jvm.processor.util.DistanceUtil;
+import org.apache.streampipes.processors.geo.jvm.processor.util.SpLengthCalculator;
 import org.apache.streampipes.wrapper.context.EventProcessorRuntimeContext;
 import org.apache.streampipes.wrapper.routing.SpOutputCollector;
 import org.apache.streampipes.wrapper.runtime.EventProcessor;
@@ -28,8 +28,12 @@ public class StaticDistanceCalculator implements EventProcessor<StaticDistanceCa
   private String latitudeFieldName;
   private String longitudeFieldName;
 
-  private Float selectedLocationLatitude;
-  private Float selectedLocationLongitude;
+  private Double selectedLocationLatitude;
+  private Double selectedLocationLongitude;
+
+  private Integer unit;
+
+  SpLengthCalculator staticLength;
 
   @Override
   public void onInvocation(StaticDistanceCalculatorParameters parameters, SpOutputCollector spOutputCollector, EventProcessorRuntimeContext runtimeContext) throws SpRuntimeException {
@@ -38,17 +42,25 @@ public class StaticDistanceCalculator implements EventProcessor<StaticDistanceCa
 
     this.selectedLocationLatitude = parameters.getSelectedLatitude();
     this.selectedLocationLongitude = parameters.getSelectedLongitude();
+
+    this.unit = parameters.getUnit();
+
+    staticLength = new SpLengthCalculator(parameters.getDecimalPosition());
   }
 
   @Override
   public void onEvent(Event event, SpOutputCollector collector) throws SpRuntimeException {
-    Float latitude = event.getFieldBySelector(latitudeFieldName).getAsPrimitive().getAsFloat();
-    Float longitude = event.getFieldBySelector(longitudeFieldName).getAsPrimitive().getAsFloat();
+    Double latitude = event.getFieldBySelector(latitudeFieldName).getAsPrimitive().getAsDouble();
+    Double longitude = event.getFieldBySelector(longitudeFieldName).getAsPrimitive().getAsDouble();
+
+    staticLength.calcGeodesicDistance(latitude, longitude, selectedLocationLatitude, selectedLocationLongitude);
 
-    Float distance = DistanceUtil.dist(latitude, longitude, selectedLocationLatitude,
-            selectedLocationLongitude);
+    if (unit != 1) {
+      staticLength.convertUnit(unit);
+    }
 
-    event.addField("distance", distance);
+    event.addField(StaticDistanceCalculatorController.LENGTH_RUNTIME, staticLength.getLengthAsString());
+    event.addField(StaticDistanceCalculatorController.UNIT_RUNTIME, staticLength.getLengthUnit());
 
     collector.collect(event);
   }
diff --git a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorController.java b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorController.java
index 2192e7d..e6cca22 100644
--- a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorController.java
+++ b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorController.java
@@ -20,14 +20,12 @@ import org.apache.streampipes.model.DataProcessorType;
 import org.apache.streampipes.model.graph.DataProcessorDescription;
 import org.apache.streampipes.model.graph.DataProcessorInvocation;
 import org.apache.streampipes.model.schema.PropertyScope;
+import org.apache.streampipes.processors.geo.jvm.processor.util.SpLengthCalculator;
 import org.apache.streampipes.sdk.builder.PrimitivePropertyBuilder;
 import org.apache.streampipes.sdk.builder.ProcessingElementBuilder;
 import org.apache.streampipes.sdk.builder.StreamRequirementsBuilder;
 import org.apache.streampipes.sdk.extractor.ProcessingElementParameterExtractor;
-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.helpers.*;
 import org.apache.streampipes.sdk.utils.Assets;
 import org.apache.streampipes.sdk.utils.Datatypes;
 import org.apache.streampipes.vocabulary.Geo;
@@ -40,47 +38,87 @@ import java.net.URI;
 public class StaticDistanceCalculatorController extends StandaloneEventProcessingDeclarer<StaticDistanceCalculatorParameters> {
 
   private static final String LATITUDE_KEY = "latitude-key";
-  private static final String LONGITUDE_KEY = "longitude-key" ;
+  private static final String LONGITUDE_KEY = "longitude-key";
   private static final String SELECTED_LATITUDE_KEY = "selected-latitude-key";
   private static final String SELECTED_LONGITUDE_KEY = "selected-longitude-key";
 
+  private static final String DECIMAL_POSITION_KEY = "decimalPosition";
+  private static final String UNIT_KEY = "unit";
+
+  protected final static String LENGTH_RUNTIME = "geodesicStaticDistance";
+  protected final static String UNIT_RUNTIME = "geodesicStaticDistanceUnit";
+
   @Override
   public DataProcessorDescription declareModel() {
     return ProcessingElementBuilder.create("org.apache.streampipes.processors.geo.jvm.processor" +
-            ".staticdistancecalculator")
-            .category(DataProcessorType.FILTER)
-            .withAssets(Assets.DOCUMENTATION)
-            .withLocales(Locales.EN)
-            .requiredStream(StreamRequirementsBuilder
-                    .create()
-                    .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lat)
-                            , Labels.withId(LATITUDE_KEY), PropertyScope.MEASUREMENT_PROPERTY)
-                    .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lng)
-                            , Labels.withId(LONGITUDE_KEY), PropertyScope.MEASUREMENT_PROPERTY)
-                    .build())
-            .requiredFloatParameter(Labels.withId(SELECTED_LATITUDE_KEY))
-            .requiredFloatParameter(Labels.withId(SELECTED_LONGITUDE_KEY))
-            .outputStrategy(
-                    OutputStrategies.append(PrimitivePropertyBuilder
-                            .create(Datatypes.Float,"distance")
-                            .domainProperty(SO.Number)
-                            .measurementUnit(URI.create("http://qudt.org/vocab/unit#Kilometer"))
-                            .build())
+        ".staticdistancecalculator")
+        .category(DataProcessorType.FILTER)
+        .withAssets(Assets.DOCUMENTATION)
+        .withLocales(Locales.EN)
+        .requiredStream(StreamRequirementsBuilder
+            .create()
+            .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lat)
+                , Labels.withId(LATITUDE_KEY), PropertyScope.MEASUREMENT_PROPERTY)
+            .requiredPropertyWithUnaryMapping(EpRequirements.domainPropertyReq(Geo.lng)
+                , Labels.withId(LONGITUDE_KEY), PropertyScope.MEASUREMENT_PROPERTY)
+            .build())
+        .requiredFloatParameter(Labels.withId(SELECTED_LATITUDE_KEY), (float) 49.0068901)
+        .requiredFloatParameter(Labels.withId(SELECTED_LONGITUDE_KEY), (float) 8.4036527)
+        .requiredIntegerParameter(
+            Labels.withId(DECIMAL_POSITION_KEY), 0, 10, 1)
+        .requiredSingleValueSelection(
+            Labels.withId(UNIT_KEY),
+            Options.from(
+                SpLengthCalculator.ValidLengthUnits.METER.name(),
+                SpLengthCalculator.ValidLengthUnits.KM.name(),
+                SpLengthCalculator.ValidLengthUnits.MILE.name(),
+                SpLengthCalculator.ValidLengthUnits.FOOT.name()
             )
-            .build();
+        )
+
+        .outputStrategy(OutputStrategies.append(
+            PrimitivePropertyBuilder
+                .create(Datatypes.Double,LENGTH_RUNTIME)
+                .domainProperty(SO.Number)
+                //todo dynamic
+                //.measurementUnit(URI.create("http://qudt.org/vocab/unit#Meter"))
+                .build(),
+            PrimitivePropertyBuilder
+                .create(Datatypes.Double,UNIT_RUNTIME)
+                .domainProperty(SO.Text)
+                // todo unit type?
+                .measurementUnit(URI.create("http://qudt.org/vocab/quantitykind/Length"))
+                .build())
+        )
+        .build();
 
   }
 
   @Override
   public ConfiguredEventProcessor<StaticDistanceCalculatorParameters> onInvocation(DataProcessorInvocation graph,
-                                                  ProcessingElementParameterExtractor extractor) {
+                                                                                   ProcessingElementParameterExtractor extractor) {
     String latitudeFieldName = extractor.mappingPropertyValue(LATITUDE_KEY);
     String longitudeFieldName = extractor.mappingPropertyValue(LONGITUDE_KEY);
-    Float selectedLatitude = extractor.singleValueParameter(SELECTED_LATITUDE_KEY, Float.class);
-    Float selectedLongitude = extractor.singleValueParameter(SELECTED_LONGITUDE_KEY, Float.class);
+    Double selectedLatitude = extractor.singleValueParameter(SELECTED_LATITUDE_KEY, Double.class);
+    Double selectedLongitude = extractor.singleValueParameter(SELECTED_LONGITUDE_KEY, Double.class);
+
+
+    Integer decimalPosition = extractor.singleValueParameter(DECIMAL_POSITION_KEY, Integer.class);
+
+    String chosenUnit = extractor.selectedSingleValue(UNIT_KEY, String.class);
+
+    // convert enum to integer values default meter
+    int unit = 1;
+    if (chosenUnit.equals(SpLengthCalculator.ValidLengthUnits.KM.name())){
+      unit = SpLengthCalculator.ValidLengthUnits.KM.getNumber();
+    } else if (chosenUnit.equals(SpLengthCalculator.ValidLengthUnits.MILE.name())){
+      unit = SpLengthCalculator.ValidLengthUnits.MILE.getNumber();
+    } else if (chosenUnit.equals(SpLengthCalculator.ValidLengthUnits.FOOT.name())){
+      unit = SpLengthCalculator.ValidLengthUnits.FOOT.getNumber();
+    }
 
     StaticDistanceCalculatorParameters staticParam = new StaticDistanceCalculatorParameters(graph,
-            latitudeFieldName, longitudeFieldName, selectedLatitude, selectedLongitude);
+        latitudeFieldName, longitudeFieldName, selectedLatitude, selectedLongitude, decimalPosition, unit);
 
     return new ConfiguredEventProcessor<>(staticParam, StaticDistanceCalculator::new);
   }
diff --git a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorParameters.java b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorParameters.java
index 0d7e347..4be7b31 100644
--- a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorParameters.java
+++ b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/staticdistancecalculator/StaticDistanceCalculatorParameters.java
@@ -24,19 +24,26 @@ public class StaticDistanceCalculatorParameters extends EventProcessorBindingPar
   private String latitudeFieldName;
   private String longitudeFieldName;
 
-  private Float selectedLatitude;
-  private Float selectedLongitude;
+  private Double selectedLatitude;
+  private Double selectedLongitude;
+
+  private Integer decimalPosition;
+  private Integer unit;
 
   public StaticDistanceCalculatorParameters(DataProcessorInvocation graph,
                                             String latitudeFieldName,
                                             String longitudeFieldName,
-                                            Float selectedLatitude,
-                                            Float selectedLongitude) {
+                                            Double selectedLatitude,
+                                            Double selectedLongitude,
+                                            Integer decimalPosition,
+                                            Integer unit) {
     super(graph);
     this.latitudeFieldName = latitudeFieldName;
     this.longitudeFieldName = longitudeFieldName;
     this.selectedLatitude = selectedLatitude;
     this.selectedLongitude = selectedLongitude;
+    this.decimalPosition = decimalPosition;
+    this.unit = unit;
   }
 
   public String getLatitudeFieldName() {
@@ -47,19 +54,23 @@ public class StaticDistanceCalculatorParameters extends EventProcessorBindingPar
     return longitudeFieldName;
   }
 
-  public Float getSelectedLatitude() {
+  public Double getSelectedLatitude() {
     return selectedLatitude;
   }
 
-  public void setSelectedLatitude(Float selectedLatitude) {
+  public void setSelectedLatitude(Double selectedLatitude) {
     this.selectedLatitude = selectedLatitude;
   }
 
-  public Float getSelectedLongitude() {
+  public Double getSelectedLongitude() {
     return selectedLongitude;
   }
 
-  public void setSelectedLongitude(Float selectedLongitude) {
+  public void setSelectedLongitude(Double selectedLongitude) {
     this.selectedLongitude = selectedLongitude;
   }
+
+  public Integer getDecimalPosition() { return decimalPosition; }
+
+  public Integer getUnit() { return unit; }
 }