/**
   * Returns a completed multi type.
   *
   * @param geometryFactory The factory this method should use to create the multi type.
   * @return Appropriate multi geometry type.
   */
  public Geometry create(GeometryFactory geometryFactory) {
    if (internalType.equals("Point")) {
      Point[] pointArray = geometryFactory.toPointArray(geometries);
      MultiPoint multiPoint = geometryFactory.createMultiPoint(pointArray);
      multiPoint.setUserData(getSRS());
      multiPoint.setSRID(getSRID());
      LOGGER.fine("created " + multiPoint);

      return multiPoint;
    } else if (internalType.equals("LineString")) {
      LineString[] lineStringArray = geometryFactory.toLineStringArray(geometries);
      MultiLineString multiLineString = geometryFactory.createMultiLineString(lineStringArray);
      multiLineString.setUserData(getSRS());
      multiLineString.setSRID(getSRID());
      LOGGER.fine("created " + multiLineString);

      return multiLineString;
    } else if (internalType.equals("Polygon")) {
      Polygon[] polygonArray = geometryFactory.toPolygonArray(geometries);
      MultiPolygon multiPolygon = geometryFactory.createMultiPolygon(polygonArray);
      multiPolygon.setUserData(getSRS());
      multiPolygon.setSRID(getSRID());
      LOGGER.fine("created " + multiPolygon);

      return multiPolygon;
    } else {
      return null;
    }
  }
예제 #2
0
 protected Geometry transformMultiPoint(MultiPoint geom, Geometry parent) {
   List transGeomList = new ArrayList();
   for (int i = 0; i < geom.getNumGeometries(); i++) {
     Geometry transformGeom = transformPoint((Point) geom.getGeometryN(i), geom);
     if (transformGeom == null) continue;
     if (transformGeom.isEmpty()) continue;
     transGeomList.add(transformGeom);
   }
   return factory.buildGeometry(transGeomList);
 }
 /** Estimate an array of "fat" Points */
 private int estimateMultiPoint(MultiPoint geom) {
   // int size
   int result = 4;
   if (geom.getNumGeometries() > 0) {
     // We can shortcut here, compared to estimateGeometryArray, as all
     // subgeoms have the same fixed size
     result += geom.getNumGeometries() * estimateBytes(geom.getGeometryN(0));
   }
   return result;
 }
  public static void main(String[] args) {

    // Handle invalid arguments..
    if (args.length < 2) {
      System.out.println("Usage: ConvexHull arg1 arg2");
      System.out.println("arg1: input dataset A file path [points]");
      System.out.println("arg2: output file name and path");
      System.exit(1);
    }

    // Creating and setting sparkconf
    SparkConf sparkConf = new SparkConf().setAppName("Group3-edu.asu.cse512.ConvexHull");
    JavaSparkContext sc = new JavaSparkContext(sparkConf);

    // Adding external jars
    // sc.addJar("lib/jts-1.13.jar");

    JavaRDD<String> lines = sc.textFile(args[0]);
    // Using mapPartitions function to find convex hull points in distributed environment
    JavaRDD<Coordinate> hullPointsRDD = lines.mapPartitions(new ConvexH());
    List<Coordinate> hullPointsList = hullPointsRDD.collect();
    Coordinate[] inputArray = new Coordinate[hullPointsList.size()];
    int j = 0;
    for (Coordinate c : hullPointsList) {
      inputArray[j] = c;
      j++;
    }
    // Finding convex hull points on the final subset of points retrieved from distributed
    // environment
    GeometryFactory geoFactory1 = new GeometryFactory();
    MultiPoint mPoint1 = geoFactory1.createMultiPoint(inputArray);
    Geometry geo1 = mPoint1.convexHull();
    Coordinate[] convexHullResult = geo1.getCoordinates();
    int length = convexHullResult.length;
    Coordinate[] convexHullFinalResult = Arrays.copyOf(convexHullResult, length - 1);
    Arrays.sort(convexHullFinalResult);

    // Converting the list of coordinates into Coordinate RDD
    JavaRDD<Coordinate> convexHullResultRDD =
        sc.parallelize(Arrays.asList(convexHullFinalResult), 1);
    JavaRDD<String> convexHullResultString =
        convexHullResultRDD
            .repartition(1)
            .map(
                new Function<Coordinate, String>() {
                  public String call(Coordinate hullPoint) throws Exception {

                    return hullPoint.x + "," + hullPoint.y;
                  }
                });
    // Save the String RDD into text file. Using repartition(1) to preserve the order of coordinates
    convexHullResultString.repartition(1).saveAsTextFile(args[1]);
  }
예제 #5
0
  private static IBoundedGeometry2D<? extends IFinite2DBounds<?>> createEuclidMultiPoint(
      final GeometryAttribute geometryAttribute, final GProjection projection) {
    final com.vividsolutions.jts.geom.MultiPoint multipoint =
        (com.vividsolutions.jts.geom.MultiPoint) geometryAttribute.getValue();

    if (multipoint.getNumGeometries() == 1) {
      final com.vividsolutions.jts.geom.Point jtsPoint =
          (com.vividsolutions.jts.geom.Point) multipoint.getGeometryN(0);
      return createPoint(jtsPoint.getCoordinate(), projection);
    }

    final IVector2[] euclidPoints = new IVector2[multipoint.getNumGeometries()];

    for (int i = 0; i < euclidPoints.length; i++) {
      final com.vividsolutions.jts.geom.Point jtsPoint =
          (com.vividsolutions.jts.geom.Point) multipoint.getGeometryN(i);
      euclidPoints[i] = createPoint(jtsPoint.getCoordinate(), projection);
    }

    return new GMultiGeometry2D<IVector2>(euclidPoints);
  }
  public Iterable<Coordinate> call(Iterator<String> coordinatesIterator) throws Exception {
    // TODO Auto-generated method stub
    List<Coordinate> coorList = new ArrayList<Coordinate>();

    // Retrieving points from JavaRDD<String> and storing it in a list
    while (coordinatesIterator.hasNext()) {
      String[] temp = coordinatesIterator.next().split(",");
      coorList.add(new Coordinate(Double.parseDouble(temp[0]), Double.parseDouble(temp[1])));
    }
    Coordinate[] coorArray = new Coordinate[coorList.size()];
    int i = 0;
    for (Coordinate c : coorList) {
      coorArray[i] = c;
      i++;
    }
    // Using Geometry class of JTS library to find convex hull points
    GeometryFactory geoFactory = new GeometryFactory();
    MultiPoint mPoint = geoFactory.createMultiPoint(coorArray);
    Geometry geo = mPoint.convexHull();
    Coordinate[] convexResult = geo.getCoordinates();
    return Arrays.asList(convexResult);
  }
  @Test
  public void testModify() throws Exception {
    final Query queryAll = new Query(RENAMED);

    SimpleFeatureStore store;
    store = (SimpleFeatureStore) rts.getFeatureSource(RENAMED);
    SimpleFeature original = store.getFeatures(fidFilter).features().next();

    // test a non mapped attribute
    String newDescription = ((String) original.getAttribute("description")) + " xxx";
    store.modifyFeatures(
        original.getFeatureType().getDescriptor("description"), newDescription, fidFilter);
    SimpleFeature modified = store.getFeatures(fidFilter).features().next();
    assertEquals(newDescription, modified.getAttribute("description"));

    // test a mapped attribute
    MultiPoint mpo = (MultiPoint) original.getAttribute("pointProperty");
    MultiPoint mpm = mpo.getFactory().createMultiPoint(new Coordinate[] {new Coordinate(10, 12)});
    store.modifyFeatures(original.getFeatureType().getDescriptor("pointProperty"), mpm, fidFilter);
    modified = store.getFeatures(fidFilter).features().next();
    assertTrue(mpm.equalsExact((Geometry) modified.getAttribute("pointProperty")));
  }
예제 #8
0
  // Multipoint
  private String transformMultiPointToJsonMultiPoint(MultiPoint multipoint) {

    LOGGER.info("Transforming multipoint to json multipoint");

    /* Multipoints are like this:
     * {"points":[[-122.63,45.51],[-122.56,45.51],[-122.56,45.55]],"spatialReference":({" wkid":4326 })}
     * 		(1)   {"points":[
     * 		(2)   [coords],[coords],[coords],
     * 		(4)   ]
     * 		(5)   "spatialReference":{" wkid":4326 }
     * 		(6)   }
     * */

    // Get CRS in this form: "spatialReference":{" wkid":4326 }
    String wkid;
    try {
      wkid = crsToJsonSpatialReference(multipoint);
    } catch (IOException e) {
      wkid = "\"spatialReference\":{\"wkid\":0000}";
    }
    LOGGER.debug("Point's CRS: " + wkid);

    // Step (1)
    String jsonString = "{\"points\":[";

    // Step (2)
    boolean firstElement = true;
    for (int i = 0; i < multipoint.getNumGeometries(); i++) {

      /* Treat one point of the multipoint: */
      Point point = (Point) multipoint.getGeometryN(i);
      String onePointString = "[";
      // Get coordinates
      Coordinate coordinatePair = point.getCoordinate();
      double x = coordinatePair.x;
      double y = coordinatePair.y;
      LOGGER.info("Point's coordinates: " + x + ", " + y);
      // Define which one is east-west and which one is north-soutn
      // TODO There should not be this difference!! I think the error is that
      // x and y were wrongly given to GML input!
      double eastwest;
      double northsouth;
      if (isShapefile) {
        // In GTVectorBindings made of shapefiles,
        // the x seems to stand for north-south
        // and the y seems to stand for east-west
        eastwest = x;
        northsouth = y;
      } else {
        // In GML content (without given CRS)
        // the y seems to stand for north-south
        // and the x seems to stand for east-west
        northsouth = x;
        eastwest = y;
      }
      /* In JSONGEOMETRY, x stands for EAST/WEST coordinate, y for NORTH/SOUTH.
       * Lines are like this [eastwest, northsouth] */
      onePointString = onePointString.concat(eastwest + "," + northsouth + "]");

      if (firstElement) {
        jsonString = jsonString.concat(onePointString);
        firstElement = false;
      } else {
        jsonString = jsonString.concat("," + onePointString);
      }
    }
    // Step (3), (4), (5)
    jsonString = jsonString.concat("], " + wkid + "}");

    return jsonString;
  }
예제 #9
0
  /**
   * Transform a feature to a JSON string
   *
   * <p>The feature that is passed to this method is transformed to a JSON string according to this
   * site (http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi_start.htm).
   *
   * @param geom
   * @return
   * @throws IOException
   */
  private String transformOneFeature(Geometry geom) throws IOException {

    // Depending on geometry's type, transform to json geometry
    String jsonString = "";

    // simple geometries:
    if (geom instanceof Point) {
      jsonString = transformPointToJsonPoint((Point) geom);
    } else if (geom instanceof LineString) {
      jsonString = transformLineStringToJsonLineString((LineString) geom);
    } else if (geom instanceof Polygon) {
      jsonString = transformPolygonToJsonPolygon((Polygon) geom);
    }

    // multipoint
    else if (geom instanceof MultiPoint) {
      MultiPoint multipoint = (MultiPoint) geom;

      // if only one point inside, make normal point out of it
      if (multipoint.getNumGeometries() == 1) {
        Point point = (Point) multipoint.getGeometryN(0);
        jsonString = transformPointToJsonPoint(point);
      } else {
        jsonString = transformMultiPointToJsonMultiPoint(multipoint);
      }

      // multilinestring
    } else if (geom instanceof MultiLineString) {
      MultiLineString multiline = (MultiLineString) geom;

      // if only one linestring inside, make normal linestring out of it
      if (multiline.getNumGeometries() == 1) {
        LineString line = (LineString) multiline.getGeometryN(0);
        jsonString = transformLineStringToJsonLineString(line);
      }
      // real multilinestring
      else {
        if (multiGeometriesToArray) {
          jsonString = transformMultiLineStringToJsonLineStringArray(multiline);
        } else {
          jsonString = transformMultiLineStringToJsonLineString(multiline);
        }
      }

      // multipolygon
    } else if (geom instanceof MultiPolygon) {
      MultiPolygon multipoly = (MultiPolygon) geom;

      // if only one polygon inside, make normal polygon out of it
      if (multipoly.getNumGeometries() == 1) {
        Polygon poly = (Polygon) multipoly.getGeometryN(0);
        jsonString = transformPolygonToJsonPolygon(poly);
      }

      // real multi polygon
      else {
        if (multiGeometriesToArray) {
          jsonString = transformMultiPolygonToJsonPolygonArray(multipoly);
        } else {
          jsonString = transformMultiPolygonToJsonPolygon(multipoly);
        }
      }
    } else {
      LOGGER.error("Feature has no recognized geometry type");
    }

    return jsonString;
  }
 private void writeMultiPoint(MultiPoint geom, ValueSetter dest) {
   dest.setInt(geom.getNumPoints());
   for (int i = 0; i < geom.getNumPoints(); i++) {
     writeGeometry(geom.getGeometryN(i), dest);
   }
 }
예제 #11
0
  /**
   * Return an image of the world with a shape drawn on it. Flags control the rendering mode.
   * Default is a hollow shape in red outline, touching the borders of the image.
   *
   * @param shape
   * @param width
   * @param height
   * @param flags
   * @return
   * @throws ThinklabException
   */
  public BufferedImage getImagery(
      Envelope envelope, ShapeValue shape, int width, int height, int flags)
      throws ThinklabException {

    BufferedImage ret = getImagery(envelope, width, height);
    GeometryFactory geoFactory = new GeometryFactory();

    double edgeBuffer = 0.0;

    if (ret == null) {
      ret =
          getSatelliteImage(
              envelope, width, height, null, null, HAlignment.MIDDLE, VAlignment.MIDDLE);
    }

    /*
     * draw shape boundaries.
     */
    Geometry geometry = shape.getGeometry();
    double x = envelope.getMinX() - edgeBuffer;
    double y = envelope.getMinY() - edgeBuffer;
    double w = envelope.getWidth() + edgeBuffer * 2;
    double h = envelope.getHeight() + edgeBuffer * 2;

    java.awt.geom.Rectangle2D.Double bounds = new java.awt.geom.Rectangle2D.Double(x, y, w, h);

    Graphics graphics = ret.getGraphics();

    if ((flags & GREEN_SHAPES) != 0) {
      graphics.setColor(Color.green);
    } else {
      graphics.setColor(Color.red);
    }

    graphics.setPaintMode();

    if (geometry.getClass().equals(MultiPolygon.class)
        || geometry.getClass().equals(Polygon.class)) {

      for (int i = 0; i < geometry.getNumGeometries(); i++) {
        com.vividsolutions.jts.geom.Polygon poly =
            (com.vividsolutions.jts.geom.Polygon) geometry.getGeometryN(i);
        LinearRing lr = geoFactory.createLinearRing(poly.getExteriorRing().getCoordinates());
        com.vividsolutions.jts.geom.Polygon part = geoFactory.createPolygon(lr, null);
        drawGeometry(part, bounds, graphics, width, height, flags);
        for (int j = 0; j < poly.getNumInteriorRing(); j++) {
          lr = geoFactory.createLinearRing(poly.getInteriorRingN(j).getCoordinates());
          part = geoFactory.createPolygon(lr, null);
          drawGeometry(part, bounds, graphics, width, height, flags);
        }
      }
    } else if (geometry.getClass().equals(MultiLineString.class)) {
      MultiLineString mp = (MultiLineString) geometry;
      for (int n = 0; n < mp.getNumGeometries(); n++) {
        drawGeometry(mp.getGeometryN(n), bounds, graphics, width, height, flags);
      }
    } else if (geometry.getClass().equals(MultiPoint.class)) {
      MultiPoint mp = (MultiPoint) geometry;
      for (int n = 0; n < mp.getNumGeometries(); n++) {
        drawGeometry(mp.getGeometryN(n), bounds, graphics, width, height, flags);
      }
    } else {
      drawGeometry(geometry, bounds, graphics, width, height, flags);
    }

    return ret;
  }
 protected static Geometry transformMultiPoint(CoordinateTransform ct, MultiPoint multiPoint) {
   return multiPoint
       .getFactory()
       .createMultiPoint(transformCoordinates(ct, multiPoint.getCoordinates()));
 }