Skip to content

Commit

Permalink
update PolyLine
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroVega committed Aug 1, 2024
1 parent 45717a2 commit 7e31f6d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public static Feature createPolyLineFeature(String paths) {
try {
return new Feature(new PolyLine(paths));
} catch (Exception e) {
LOGGER.error(e.getClass().getSimpleName() + " " + e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
package com.telefonica.iot.cygnus.backends.arcgis.model;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;

import com.telefonica.iot.cygnus.backends.arcgis.exceptions.ArcgisException;
import com.telefonica.iot.cygnus.log.CygnusLogger;

Expand All @@ -36,27 +42,7 @@ public class PolyLine implements Geometry {
private static final String WKID_TAG = "wkid";
private static final String PATHS_TAG = "paths";

class Paths {
private List<List<Point>> paths;

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{\n \"paths\": [\n");
for (List<Point> path : this.paths) {
sb.append(" [\n");
for (Point point : path) {
sb.append(" ").append(point.toString()).append(",\n");
}
sb.setLength(sb.length() - 2);
sb.append("\n ],\n");
}
sb.setLength(sb.length() - 2);
sb.append("\n ]\n}");
return sb.toString();
}

}
private List<List<Point>> paths;
public List<List<double[]>> paths;

private SpatialReference spatialReference;
private int type = Geometry.TYPE_SHAPE; // TBD
Expand All @@ -67,7 +53,7 @@ public String toString() {
* @param paths
* @param spatialReference
*/
public PolyLine(List<List<Point>> paths, SpatialReference spatialReference) {
public PolyLine(List<List<double[]>> paths, SpatialReference spatialReference) {
this.paths = paths;
this.spatialReference = spatialReference;
}
Expand All @@ -78,7 +64,7 @@ public PolyLine(List<List<Point>> paths, SpatialReference spatialReference) {
* @param lat
* @param lng
*/
public PolyLine(List<List<Point>> paths) {
public PolyLine(List<List<double[]>> paths) {
this(paths, SpatialReference.WGS84);
}

Expand All @@ -92,7 +78,6 @@ public void setValue(Geometry g) throws ArcgisException {
} else {
throw new ArcgisException("Invalid Geometry Type, Point expected.");
}

}

/**
Expand All @@ -103,13 +88,15 @@ public void setValue(Geometry g) throws ArcgisException {
*/
public PolyLine(String strPolyline) throws ArcgisException {
try {
JsonObject jsonObject = JsonParser.parseString(strPolyline).getAsJsonObject();
String thePathsStr = jsonObject.get("paths").toString();
Gson gson = new Gson();
Paths wrapper = gson.fromJson(strPolyline, Paths.class);
this.paths = wrapper.paths;
Type listType = new TypeToken<List<List<double[]>>>() {}.getType();
this.paths = gson.fromJson(thePathsStr, listType);
this.spatialReference = SpatialReference.WGS84;

} catch (NumberFormatException e) {
throw new ArcgisException("Unexpected string format for type Point.");
LOGGER.error(e.getClass().getSimpleName() + " " + e.getMessage());
throw new ArcgisException("Unexpected string format for type PolyLine.");
}
}

Expand All @@ -126,7 +113,8 @@ public void setGeometryFromJSON(String json) {
*/
public JsonObject toJSON() {
JsonObject result = new JsonObject();
result.addProperty(PATHS_TAG, this.paths.toString());
LOGGER.debug("toJSON ");
result.addProperty(PATHS_TAG, this.toString());

JsonObject spatialRef = new JsonObject();
spatialRef.addProperty(WKID_TAG, spatialReference.getWkid());
Expand Down Expand Up @@ -156,7 +144,25 @@ public static Geometry createInstanceFromJson(JsonObject json) throws ArcgisExce
* @return String
*/
public String toString() {
return getPaths().toString();
StringBuilder sb = new StringBuilder();
sb.append("{ \"paths\": [");
for (int i = 0; i < this.paths.size(); i++) {
List<double[]> innerList = this.paths.get(i);
for (int j = 0; j < innerList.size(); j++) {
sb.append(" [");
sb.append("[");
double[] array = innerList.get(j);
for (double value : array) {
sb.append(" ").append(value).append(",");
}
sb.append(" ]");
sb.setLength(sb.length() - 2);
sb.append(" ],");
}
}
sb.setLength(sb.length() - 2);
sb.append(" ]}");
return sb.toString();
}

/**
Expand Down Expand Up @@ -184,8 +190,8 @@ public SpatialReference getSpatialReference() {
*
* @return
*/
public List<List<Point>> getPaths() {
return paths;
public List<List<double[]>> getPaths() {
return this.paths;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
import com.google.gson.JsonParser;
import com.telefonica.iot.cygnus.backends.arcgis.exceptions.ArcgisException;
import com.telefonica.iot.cygnus.backends.arcgis.model.Feature;
import com.telefonica.iot.cygnus.backends.arcgis.model.PolyLine;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import com.google.gson.JsonObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


/**
*
Expand Down Expand Up @@ -161,6 +171,15 @@ public void createInstanceFromJson2() {
@Test
public void getPolyFeatureTest() {
System.out.println("---------------- getNewPolyLineFeature");
try {
String paths = "{ \"paths\": [ [ [-97.06138, 32.837], [-97.06133, 33.836], [-98.2, 34.834], [-97, 40] ] ] }";
PolyLine poly = new PolyLine(paths);
System.out.println("POLY: " + poly.toString());

} catch (Exception e) {
System.out.println("Exception");
System.out.println(e.getClass().getSimpleName() + " " + e.getMessage());
}
Feature poly = FeatureTestFactory.getNewPolyLineFeature("Mi PolyLine", 33);
System.out.println("feature poly - " + poly.toJson());
assertTrue("ok.", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public static Feature getNewPolyLineFeature(String description, Integer external
attributes.put("FFIN", new GregorianCalendar());
attributes.put("CANTIDADOCUPADA", null);
String jsonString = "{ \"paths\": [ [ [-97.06138, 32.837], [-97.06133, 33.836], [-98.2, 34.834], [-97, 40] ] ] }";

Feature feature = Feature.createPolyLineFeature(jsonString);
feature.setAttributes(attributes);
return feature;
Expand Down

0 comments on commit 7e31f6d

Please sign in to comment.