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

Public Member Functions

String getURI ()
 
Coordinate getGeoMedian (Coordinate start, Coordinate[] points)
 
Value evaluate (ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException
 

Protected Member Functions

Geometry operation (Geometry geom)
 

Static Private Member Functions

static double distance (double x1, double y1, double x2, double y2)
 

Member Function Documentation

◆ distance()

static double distance ( double  x1,
double  y1,
double  x2,
double  y2 
)
staticprivate

Referenced by GeometricMedian.getGeoMedian().

51  {
52  x1 -= x2;
53  y1 -= y2;
54  return Math.sqrt(x1 * x1 + y1 * y1);
55 }

◆ evaluate()

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

References LiteralRegistry.getLiteral(), and GeometricUnaryFunction.operation().

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  if(l instanceof VectorLiteral) {
23  Geometry geom=((VectorLiteral)l).read(args[0].stringValue());
24  Geometry result = operation(geom);
25  return valueFactory.createLiteral(((VectorLiteral) l).unparse(result),((Literal)args[0]).getDatatype());
26  }
27  throw new ValueExprEvaluationException("Argument given is not a geometry literal");
28  }

◆ getGeoMedian()

Coordinate getGeoMedian ( Coordinate  start,
Coordinate []  points 
)

References GeometricMedian.distance(), Coordinate.x, and Coordinate.y.

Referenced by GeometricMedian.operation().

27  {
28  double cx = 0;
29  double cy = 0;
30 
31  double centroidx = start.x;
32  double centroidy = start.y;
33  do {
34  double totalWeight = 0;
35  for (int i = 0; i < points.length; i++) {
36  Coordinate pt = points[i];
37  double weight = 1 / distance(pt.x, pt.y, centroidx, centroidy);
38  cx += pt.x * weight;
39  cy += pt.y * weight;
40  totalWeight += weight;
41  }
42  cx /= totalWeight;
43  cy /= totalWeight;
44  } while (Math.abs(cx - centroidx) > 0.5
45  || Math.abs(cy - centroidy) > 0.5);// Probably this condition
46  // needs to change
47 
48  return new Coordinate(cx, cy);
49 }
static double distance(double x1, double y1, double x2, double y2)
Definition: openrdf/query/algebra/evaluation/function/postgis/point/GeometricMedian.java:51

◆ getURI()

String getURI ( )

References POSTGIS.st_geometricMedian.

13  {
14  return POSTGIS.st_geometricMedian.stringValue();
15  }

◆ operation()

Geometry operation ( Geometry  geom)
protected

References GeometricMedian.getGeoMedian().

18  {
19  Coordinate median=getGeoMedian(geom.getCentroid().getCoordinate(), geom.getCoordinates());
20  GeometryFactory fac=new GeometryFactory();
21  return fac.createPoint(median);
22  }
Coordinate getGeoMedian(Coordinate start, Coordinate[] points)
Definition: openrdf/query/algebra/evaluation/function/postgis/point/GeometricMedian.java:27