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:51 UTC

[incubator-streampipes-extensions] branch feature/geodesicCalc created (now c70ad4d)

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

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


      at c70ad4d  improved static distance calculation suing new utility class SpDistanceCalculatior

This branch includes the following new commits:

     new e304d80  introducing measurement dependencies
     new 0754ef5  creating utility for distance calculation
     new 2f01d18  improved distance calculation suing new utility class SpDistanceCalculatior
     new c70ad4d  improved static distance calculation suing new utility class SpDistanceCalculatior

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-streampipes-extensions] 01/04: introducing measurement dependencies

Posted by mi...@apache.org.
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 e304d80f77e34671dc2a1e810c4e78c11f6babdc
Author: micklich <fl...@disy.net>
AuthorDate: Sat May 16 12:40:03 2020 +0200

    introducing measurement dependencies
---
 streampipes-processors-geo-jvm/pom.xml | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/streampipes-processors-geo-jvm/pom.xml b/streampipes-processors-geo-jvm/pom.xml
index e1f57f7..11db712 100644
--- a/streampipes-processors-geo-jvm/pom.xml
+++ b/streampipes-processors-geo-jvm/pom.xml
@@ -87,6 +87,29 @@
             <version>1.16.1</version>
         </dependency>
 
+
+
+        <dependency>
+            <groupId>javax.measure</groupId>
+            <artifactId>unit-api</artifactId>
+            <version>1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>tec.units</groupId>
+            <artifactId>unit-ri</artifactId>
+            <version>1.0.3</version>
+        </dependency>
+
+        <dependency>
+            <groupId>si.uom</groupId>
+            <artifactId>si-units</artifactId>
+            <version>1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.streampipes</groupId>
+            <artifactId>streampipes-measurement-units</artifactId>
+        </dependency>
+
     </dependencies>
 
     <build>


[incubator-streampipes-extensions] 02/04: creating utility for distance calculation

Posted by mi...@apache.org.
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 0754ef5ab96ae94c5fc11e2bd9ac57739a4c4271
Author: micklich <fl...@disy.net>
AuthorDate: Sat May 16 12:40:49 2020 +0200

    creating utility for distance calculation
---
 .../geo/jvm/processor/util/SpLengthCalculator.java | 198 +++++++++++++++++++++
 1 file changed, 198 insertions(+)

diff --git a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/util/SpLengthCalculator.java b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/util/SpLengthCalculator.java
new file mode 100644
index 0000000..61552d1
--- /dev/null
+++ b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/util/SpLengthCalculator.java
@@ -0,0 +1,198 @@
+package org.apache.streampipes.processors.geo.jvm.processor.util;
+
+import si.uom.SI;
+import tec.units.ri.quantity.Quantities;
+import tec.units.ri.unit.MetricPrefix;
+
+
+//import com.github.jqudt.Quantity;
+//import com.github.jqudt.Unit;
+
+import javax.measure.Quantity;
+import javax.measure.Unit;
+import javax.measure.quantity.Length;
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+
+public class SpLengthCalculator {
+
+  private final double EARTHRADIUS = 6378137; //meters
+
+  private final Unit<Length> M = SI.METRE;
+  private final Unit<Length> KM = MetricPrefix.KILO(SI.METRE);
+  private final Unit<Length> MILE = SI.METRE.multiply(1609344).divide(1000);
+  private final Unit<Length> FOOT = SI.METRE.multiply(3048).divide(10000);
+
+  public enum ValidLengthUnits {
+    METER(1), KM(2), MILE(3), FOOT(4);
+
+    private final int number;
+
+    ValidLengthUnits(int number) {
+      this.number = number;
+    }
+
+    public int getNumber() {
+      return number;
+    }
+  }
+
+
+  private Integer decimalPosition;
+  private Quantity<Length> length;
+
+  // ========================= constructor
+  public SpLengthCalculator(int decimalPosition) {
+    this.length = Quantities.getQuantity(-9999, M);
+    this.decimalPosition = decimalPosition;
+  }
+
+
+  // ========================== getter
+
+  public int getDecimalPosition() {
+    return decimalPosition;
+  }
+
+  public Double getLengthValue() {
+    return (length.getValue()).doubleValue();
+  }
+
+  public String getLengthUnit() {
+    return length.getUnit().toString();
+  }
+
+  public Unit<Length> getUnit() {
+    return length.getUnit();
+  }
+
+  public String getLengthAsString() {
+    String result = doubleToString(getLengthValue(), getDecimalPosition());
+    return result;
+  }
+
+
+  // ========================== setter
+
+  private void setLength(Quantity<Length> length) {
+    this.length = length;
+  }
+
+  private void setLength(double value, Unit<Length> unit) {
+    this.length = Quantities.getQuantity(value, unit);
+  }
+
+
+  // unit Transformation
+  /**
+   * convert  Quantity<Length>  value to Unit<Length>
+   *
+   * @param value choose target Unit<Length> from enum @see ValidLengthUnits
+   */
+  public void convertUnit(ValidLengthUnits value) {
+    switch (value.getNumber()) {
+      case 1:
+        setLength(this.length.to(M));
+        break;
+      case 2:
+        setLength(this.length.to(KM));
+        break;
+      case 3:
+        setLength(this.length.to(MILE));
+        break;
+      case 4:
+        setLength(this.length.to(FOOT));
+        break;
+    }
+  }
+
+  /**
+   * convert  Quantity<Length>  value to Unit<Length>
+   *
+   * @param value choose int value corresponding to ValidLengthUnits
+   */
+  public void convertUnit(int value) {
+    switch (value) {
+      case 1:
+        setLength(this.length.to(M));
+        break;
+      case 2:
+        setLength(this.length.to(KM));
+        break;
+      case 3:
+        setLength(this.length.to(MILE));
+        break;
+      case 4:
+        setLength(this.length.to(FOOT));
+        break;
+    }
+  }
+
+
+  /**
+   * transform the double value to a String depending on the {@link DecimalFormat} pattern.
+   * Max 10 decimal positions are defined. negative values are interpreted as 4 digits.
+   *
+   * @param value
+   * @param decimalPositions
+   * @return
+   */
+  public static String doubleToString(Double value, int decimalPositions) {
+
+    //handle negative values but should not be possible but if 3 decimal Position will be used
+    if (decimalPositions < 0) {
+      decimalPositions = 3;
+    }
+    // setup the decimal format to prevent scientific format style
+    DecimalFormat df = new DecimalFormat();
+
+    // transform the decimal
+    DecimalFormatSymbols dfs = new DecimalFormatSymbols();
+    // uses always the . instead of , for decimal separator
+    dfs.setDecimalSeparator('.');
+    df.setDecimalFormatSymbols(dfs);
+    df.setGroupingUsed(false);
+
+    // if decimal position higher than 0 is chosen, decimal Pos will be set. if negative value all
+    // calc decimal position will be used
+    if (decimalPositions >= 0) {
+      df.setMaximumFractionDigits(decimalPositions);
+    }
+
+    //writes the result into a String to parse this into the stream. Cannot be parsed as a Double Otherwise scientific style comes back
+    String result = df.format(value);
+
+    return result;
+  }
+
+
+  public void calcGeodesicDistance(double lat1, double lng1, double lat2, double lng2) {
+     // using haversine formula
+    double dLat = Math.toRadians(lat2-lat1);
+    double dLng = Math.toRadians(lng2-lng1);
+    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
+        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
+            Math.sin(dLng/2) * Math.sin(dLng/2);
+    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
+    float dist = (float) (EARTHRADIUS * c);
+
+    setLength(dist, M);
+  }
+
+
+
+  public void calcGeodesicDistanceNew(double lat1, double lng1, double lat2, double lng2) {
+    // using haversine formula
+    double dLat = Math.toRadians(lat2-lat1);
+    double dLng = Math.toRadians(lng2-lng1);
+    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
+        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
+            Math.sin(dLng/2) * Math.sin(dLng/2);
+    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
+    float dist = (float) (EARTHRADIUS * c);
+
+    setLength(dist, M);
+  }
+
+
+}


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

Posted by mi...@apache.org.
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 2f01d188888315c031778ed6e7b09fad896e432b
Author: micklich <fl...@disy.net>
AuthorDate: Sat May 16 12:42:09 2020 +0200

    improved distance calculation suing new utility class SpDistanceCalculatior
---
 .../distancecalculator/DistanceCalculator.java     | 45 +++++++++---
 .../DistanceCalculatorController.java              | 84 +++++++++++++++++-----
 .../DistanceCalculatorParameters.java              | 10 ++-
 3 files changed, 109 insertions(+), 30 deletions(-)

diff --git a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculator.java b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculator.java
index d310f1b..481a72c 100644
--- a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculator.java
+++ b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculator.java
@@ -18,33 +18,58 @@
 
 package org.apache.streampipes.processors.geo.jvm.processor.distancecalculator;
 
+import org.apache.streampipes.logging.api.Logger;
 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;
 
 public class DistanceCalculator implements EventProcessor<DistanceCalculatorParameters> {
 
-  private DistanceCalculatorParameters params;
+  private static Logger LOG;
+  private SpLengthCalculator length;
+
+  private String latitute1;
+  private String longitude1;
+  private String latitude2;
+  private String longitude2;
+  private Integer unit;
+
+
 
   @Override
-  public void onInvocation(DistanceCalculatorParameters numericalFilterParameters, SpOutputCollector spOutputCollector, EventProcessorRuntimeContext
+  public void onInvocation(DistanceCalculatorParameters params, SpOutputCollector spOutputCollector, EventProcessorRuntimeContext
           runtimeContext) {
-    this.params = numericalFilterParameters;
+
+    LOG = params.getGraph().getLogger(DistanceCalculatorParameters.class);
+    this.latitute1 = params.getLat1PropertyName();
+    this.longitude1 = params.getLong1PropertyName();
+    this.latitude2 = params.getLat2PropertyName();
+    this.longitude2 = params.getLong2PropertyName();
+    this.unit = params.getUnit();
+
+    // init class with constructor
+    length = new SpLengthCalculator(params.getDecimalPosition());
+
   }
 
   @Override
   public void onEvent(Event event, SpOutputCollector out) {
 
-    float lat1 = event.getFieldBySelector(this.params.getLat1PropertyName()).getAsPrimitive().getAsFloat();
-    float long1 = event.getFieldBySelector(this.params.getLong1PropertyName()).getAsPrimitive().getAsFloat();
-    float lat2 = event.getFieldBySelector(this.params.getLat2PropertyName()).getAsPrimitive().getAsFloat();
-    float long2 = event.getFieldBySelector(this.params.getLong2PropertyName()).getAsPrimitive().getAsFloat();
+    double lat1 = event.getFieldBySelector(latitute1).getAsPrimitive().getAsDouble();
+    double lng1 = event.getFieldBySelector(longitude1).getAsPrimitive().getAsDouble();
+    double lat2 = event.getFieldBySelector(latitude2).getAsPrimitive().getAsDouble();
+    double lng2 = event.getFieldBySelector(longitude2).getAsPrimitive().getAsDouble();
+
+    length.calcGeodesicDistance(lat1, lng1, lat2, lng2);
 
-    double resultDist = DistanceUtil.dist(lat1, long1, lat2, long2);
+    if (unit != 1) {
+      length.convertUnit(unit);
+    }
 
-    event.addField("distance", resultDist);
+    event.addField(DistanceCalculatorController.LENGTH_RUNTIME, length.getLengthAsString());
+    event.addField(DistanceCalculatorController.UNIT_RUNTIME, length.getLengthUnit());
 
     out.collect(event);
   }
diff --git a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorController.java b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorController.java
index a6e1497..7bbf3a9 100644
--- a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorController.java
+++ b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorController.java
@@ -22,59 +22,105 @@ 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.*;
 import org.apache.streampipes.sdk.utils.Assets;
+import org.apache.streampipes.sdk.utils.Datatypes;
 import org.apache.streampipes.vocabulary.Geo;
 import org.apache.streampipes.vocabulary.SO;
 import org.apache.streampipes.wrapper.standalone.ConfiguredEventProcessor;
 import org.apache.streampipes.wrapper.standalone.declarer.StandaloneEventProcessingDeclarer;
 
+import java.net.URI;
+
 public class DistanceCalculatorController extends StandaloneEventProcessingDeclarer<DistanceCalculatorParameters> {
 
   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 DECIMAL_POSITION_KEY = "decimalPosition";
+  private static final String UNIT_KEY = "unit";
+
   private static final String CALCULATED_DISTANCE_KEY = "calculatedDistance";
 
+  protected final static String LENGTH_RUNTIME = "geodesicDistance";
+  protected final static String UNIT_RUNTIME = "geodesicDistanceUnit";
 
   @Override
   public DataProcessorDescription declareModel() {
     return ProcessingElementBuilder.create("org.apache.streampipes.processors.geo.jvm.processor.distancecalculator")
-            .category(DataProcessorType.FILTER)
-            .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))
+        .category(DataProcessorType.FILTER)
+        .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())
+        .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<DistanceCalculatorParameters> onInvocation
-          (DataProcessorInvocation sepa, ProcessingElementParameterExtractor extractor) {
+      (DataProcessorInvocation sepa, ProcessingElementParameterExtractor extractor) {
 
     String lat1PropertyName = extractor.mappingPropertyValue(LAT_1_KEY);
     String long11PropertyName = extractor.mappingPropertyValue(LONG_1_KEY);
     String lat2PropertyName = extractor.mappingPropertyValue(LAT_2_KEY);
     String long2PropertyName = extractor.mappingPropertyValue(LONG_2_KEY);
 
-    DistanceCalculatorParameters staticParam = new DistanceCalculatorParameters(sepa, lat1PropertyName, long11PropertyName, lat2PropertyName, long2PropertyName);
+    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();
+    }
+
+    DistanceCalculatorParameters staticParam = new DistanceCalculatorParameters(sepa, lat1PropertyName, long11PropertyName, lat2PropertyName, long2PropertyName, decimalPosition, unit);
 
     return new ConfiguredEventProcessor<>(staticParam, DistanceCalculator::new);
   }
diff --git a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorParameters.java b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorParameters.java
index a4ecdd4..187419d 100644
--- a/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorParameters.java
+++ b/streampipes-processors-geo-jvm/src/main/java/org/apache/streampipes/processors/geo/jvm/processor/distancecalculator/DistanceCalculatorParameters.java
@@ -27,17 +27,21 @@ public class DistanceCalculatorParameters extends EventProcessorBindingParams {
   public String long1PropertyName;
   public String lat2PropertyName;
   public String long2PropertyName;
+  protected Integer decimalPosition;
+  protected Integer unit;
 
   public DistanceCalculatorParameters(DataProcessorInvocation graph) {
     super(graph);
   }
 
-  public DistanceCalculatorParameters(DataProcessorInvocation graph, String lat1PropertyName, String long1PropertyName, String lat2PropertyName, String long2PropertyName) {
+  public DistanceCalculatorParameters(DataProcessorInvocation graph, String lat1PropertyName, String long1PropertyName, String lat2PropertyName, String long2PropertyName, Integer decimalPosition, Integer unit) {
     super(graph);
     this.lat1PropertyName = lat1PropertyName;
     this.long1PropertyName = long1PropertyName;
     this.lat2PropertyName = lat2PropertyName;
     this.long2PropertyName = long2PropertyName;
+    this.decimalPosition = decimalPosition;
+    this.unit = unit;
   }
 
 
@@ -56,4 +60,8 @@ public class DistanceCalculatorParameters extends EventProcessorBindingParams {
   public String getLong2PropertyName() {
     return long2PropertyName;
   }
+
+  public Integer getDecimalPosition() { return decimalPosition; }
+
+  public Integer getUnit() { return unit; }
 }


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

Posted by mi...@apache.org.
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; }
 }