kiwi-postgis  Version 0.1.0.0
Segmentize Class Reference
Inheritance diagram for Segmentize:
Collaboration diagram for Segmentize:

Public Member Functions

String getURI ()
 
boolean isSupported (KiWiDialect dialect)
 Return true if this function has available native support for the given dialect. More...
 
String getNative (KiWiDialect dialect, String... args)
 Return a string representing how this GeoSPARQL function is translated into SQL ( Postgis Function ) in the given dialect. More...
 
ValueType getReturnType ()
 Get the return type of the function. More...
 
ValueType getArgumentType (int arg)
 Get the argument type of the function for the arg'th argument (starting to count at 0). More...
 
int getMinArgs ()
 Return the minimum number of arguments this function requires. More...
 
int getMaxArgs ()
 Return the maximum number of arguments this function can take. More...
 
List< LineString > createSegments (Geometry track, double segmentLength) throws NoSuchAuthorityCodeException, FactoryException
 
Value evaluate (ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException
 

Protected Member Functions

Geometry relation (Geometry geom, Double value)
 

Static Package Functions

 [static initializer]
 

Member Function Documentation

◆ [static initializer]()

[static initializer] ( )
staticpackage

◆ createSegments()

List<LineString> createSegments ( Geometry  track,
double  segmentLength 
) throws NoSuchAuthorityCodeException, FactoryException
inherited

Referenced by Segmentize.relation().

38  {
39 
40  //GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING));
41  List<LineString> segments = new ArrayList<>();
42  /*SRSInfo srsInfo = track.getSrsInfo();
43 
44  List<Coordinate> coordinates = new ArrayList<>(track.getCoordinates().length);
45  Collections.addAll(coordinates, track.getCoordinates());
46 
47  double accumulatedLength = 0;
48  List<Coordinate> lastSegment = new ArrayList<>();
49 
50  Iterator<Coordinate> itCoordinates = coordinates.iterator();
51 
52  for (int i = 0; itCoordinates.hasNext() && i < coordinates.size() - 1; i++) {
53  Coordinate c1 = coordinates.get(i);
54  Coordinate c2 = coordinates.get(i + 1);
55 
56  lastSegment.add(c1);
57 
58  double length;
59  //Check for geographic or planar distance.
60  //Does the rest of the calculation take into consideration geographic is non-linear in X/Y relationship?
61  if (srsInfo.isGeographic()) {
62  length = GreatCircleDistance.haversineFormula(c1, c2);
63  } else {
64  length = c1.distance(c2);
65  }
66 
67  if (length + accumulatedLength >= segmentLength) {
68  double offsetLength = segmentLength - accumulatedLength;
69  double ratio = offsetLength / length;
70  double dx = c2.x - c1.x;
71  double dy = c2.y - c1.y;
72 
73  Coordinate segmentationPoint = new Coordinate(c1.x + (dx * ratio), c1.y + (dy * ratio));
74 
75  lastSegment.add(segmentationPoint); // Last point of the segment is the segmentation point
76  segments.add(geometryFactory.createLineString(lastSegment.toArray(new Coordinate[lastSegment.size()])));
77 
78  lastSegment = new ArrayList<>(); // Resets the variable since a new segment will be built
79  accumulatedLength = 0D;
80  coordinates.add(i + 1, segmentationPoint);
81  } else {
82  accumulatedLength += length;
83  }
84  }
85 
86  lastSegment.add(coordinates.get(coordinates.size() - 1)); // Because the last one is never added in the loop above
87  segments.add(geometryFactory.createLineString(lastSegment.toArray(new Coordinate[lastSegment.size()])));
88  */
89  return segments;
90  }

◆ evaluate()

Value evaluate ( ValueFactory  valueFactory,
Value...  args 
) throws ValueExprEvaluationException
inherited

References LiteralRegistry.getLiteral(), and GeometricModifierDoubleFunction.relation().

16  {
17  if (args.length != 2) {
18  throw new ValueExprEvaluationException(getURI() + " requires exactly 2 arguments, got " + args.length);
19  }
20 
21  LiteralType l=LiteralRegistry.getLiteral(((Literal)args[0]).getDatatype().toString());
22  Double value=Double.valueOf(args[1].stringValue());
23  if(l instanceof VectorLiteral) {
24  Geometry geom=((VectorLiteral)l).read(args[0].stringValue());
25  Geometry result = relation(geom,value);
26  return valueFactory.createLiteral(((VectorLiteral) l).unparse(result),((Literal)args[0]).getDatatype());
27  }
28  throw new ValueExprEvaluationException("Argument given is not a geometry literal");
29  }

◆ getArgumentType()

ValueType getArgumentType ( int  arg)

Get the argument type of the function for the arg'th argument (starting to count at 0).

This is needed for SQL type casting inside KiWi.

Parameters
arg
Returns

References ValueType.DOUBLE, and ValueType.GEOMETRY.

87  {
88  switch(arg) {
89  case 0: return ValueType.GEOMETRY;
90  case 1: return ValueType.DOUBLE;
91  }
92  return null;
93  }

◆ getMaxArgs()

int getMaxArgs ( )

Return the maximum number of arguments this function can take.

Returns
111  {
112  return 1;
113  }

◆ getMinArgs()

int getMinArgs ( )

Return the minimum number of arguments this function requires.

Returns
101  {
102  return 1;
103  }

◆ getNative()

String getNative ( KiWiDialect  dialect,
String...  args 
)

Return a string representing how this GeoSPARQL function is translated into SQL ( Postgis Function ) in the given dialect.

Parameters
dialect
args
Returns

References FN_POSTGIS.st_segmentize.

45  {
46  if (dialect instanceof PostgreSQLDialect) {
47  if (args.length == 2) {
48  String geom1 = args[0];
49  Double segmentlength=Double.valueOf(args[1]);
50  String SRID_default = "4326";
51  /*
52  * The following condition is required to read WKT inserted directly into args[0] and create a geometries with SRID
53  * POINT, MULTIPOINT, LINESTRING ... and MULTIPOLYGON conditions:
54  * example: geof:convexHull("POLYGON(( -7 43, -2 43, -2 38, -7 38, -7 43))"^^geo:wktLiteral)
55  * st_AsText condition: It is to use the geometry that is the result of another function geosparql.
56  * example: geof:convexHull(geof:buffer(?geom, 50, units:meter))
57  */
58  if (args[0].contains("POINT") || args[0].contains("MULTIPOINT") || args[0].contains("LINESTRING") || args[0].contains("MULTILINESTRING") || args[0].contains("POLYGON") || args[0].contains("MULTIPOLYGON") || args[0].contains("ST_AsText")) {
59  geom1 = String.format("ST_GeomFromText(%s,%s)", args[0], SRID_default);
60  }
61  return String.format("ST_Segmentize(%s,%s)", geom1,segmentlength);
62  }
63  }
64  throw new UnsupportedOperationException(FN_POSTGIS.st_segmentize.toString()+" function not supported by dialect " + dialect);
65  }

◆ getReturnType()

ValueType getReturnType ( )

Get the return type of the function.

This is needed for SQL type casting inside KiWi.

Returns

References ValueType.GEOMETRY.

74  {
75  return ValueType.GEOMETRY;
76  }

◆ getURI()

String getURI ( )

References FN_POSTGIS.st_segmentize.

20  {
21  return FN_POSTGIS.st_segmentize.stringValue();
22  }

◆ isSupported()

boolean isSupported ( KiWiDialect  dialect)

Return true if this function has available native support for the given dialect.

Parameters
dialect
Returns
32  {
33  return dialect instanceof PostgreSQLDialect;
34  }

◆ relation()

Geometry relation ( Geometry  geom,
Double  value 
)
protectedinherited

References Segmentize.createSegments().

23  {
24  double segmentLength = value;
25 
26  List<LineString> linestrings;
27  try {
28  linestrings = createSegments(geom, segmentLength);
29  GeometryFactory fac=new GeometryFactory();
30  return fac.createMultiLineString(linestrings.toArray(new LineString[0]));
31  } catch (FactoryException e) {
32  // TODO Auto-generated catch block
33  e.printStackTrace();
34  }
35  return null;
36  }
List< LineString > createSegments(Geometry track, double segmentLength)
Definition: openrdf/query/algebra/evaluation/function/postgis/linestring/Segmentize.java:38