Exemplo n.º 1
0
 protected Geometry transformMultiLineString(MultiLineString geom, Geometry parent) {
   List transGeomList = new ArrayList();
   for (int i = 0; i < geom.getNumGeometries(); i++) {
     Geometry transformGeom = transformLineString((LineString) geom.getGeometryN(i), geom);
     if (transformGeom == null) continue;
     if (transformGeom.isEmpty()) continue;
     transGeomList.add(transformGeom);
   }
   return factory.buildGeometry(transGeomList);
 }
 protected static Geometry transformMultiLineString(
     CoordinateTransform ct, MultiLineString multiLineString) {
   LineString[] lineString = new LineString[multiLineString.getNumGeometries()];
   for (int i = 0; i < lineString.length; ++i) {
     lineString[i] =
         multiLineString
             .getFactory()
             .createLineString(
                 transformCoordinates(ct, multiLineString.getGeometryN(i).getCoordinates()));
   }
   return multiLineString.getFactory().createMultiLineString(lineString);
 }
Exemplo n.º 3
0
  private String transformMultiLineStringToJsonLineStringArray(MultiLineString multilinestring) {

    LOGGER.info("Transforming multilinestring to JSON linestring array");

    /*
     * For a multi line, we make several simple lines and put them into an array
     */

    String lineArray = "[";
    boolean firstElement = true;
    for (int i = 0; i < multilinestring.getNumGeometries(); i++) {
      LineString linestring = (LineString) multilinestring.getGeometryN(i);
      String aLine = transformLineStringToJsonLineString(linestring);
      if (firstElement) {
        lineArray = lineArray.concat(aLine);
        firstElement = false;
      } else {
        lineArray = lineArray.concat(", " + aLine);
      }
    }

    return lineArray;
  }
Exemplo n.º 4
0
  // Multilinestring
  private String transformMultiLineStringToJsonLineString(MultiLineString multilinestring) {

    LOGGER.info("Transforming multilinestring to JSON linestring with several paths in it.");

    /* Lines are like this: {"paths":[[[-122.68,45.53], [-122.58,45.55],[-122.57,45.58],[-122.53,45.6]]],
    "spatialReference":{"wkid":4326}}
    * For a multi line, we put several paths.
    * We will assemble it in several parts:
    * 		(a)  {"paths":[
    * 		(b)  several paths, comma separated, each has square brackets:
    * 		(b)  		[  [coords],[coords],[coords]  ]   <== this is one path, with square brackets!
    * 		(b)  		[  [coords],[coords],[coords]  ],  <== this is one path, with square brackets!
    * 		(b)  		[  [coords],[coords],[coords]  ],  <== this is one path, with square brackets!
    *       (c)  ],
    *       (d)  "spatialReference":{"wkid":4326}
    *       (e)  }
    */

    // (1) make paths, and concatenate them, comma separated
    String allPaths = "";
    boolean firstElement = true;
    for (int i = 0; i < multilinestring.getNumGeometries(); i++) {
      LineString linestring = (LineString) multilinestring.getGeometryN(i);
      String aPath = linestringToJsonPath(linestring);
      if (firstElement) {
        allPaths = allPaths.concat(aPath);
        firstElement = false;
      } else {
        allPaths = allPaths.concat(", " + aPath);
      }
    }

    // (2) assemble to line
    String jsonString = assembleLine(allPaths, multilinestring);
    return jsonString;
  }
Exemplo n.º 5
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;
  }
Exemplo n.º 6
0
  public static IGlobeFeatureCollection<
          IVector2, ? extends IBoundedGeometry2D<? extends IFinite2DBounds<?>>>
      readFeatures(final DataStore dataStore, final String layerName, final GProjection projection)
          throws Exception {

    final SimpleFeatureSource featureSource = dataStore.getFeatureSource(layerName);

    final SimpleFeatureCollection featuresCollection = featureSource.getFeatures();

    final GIntHolder validCounter = new GIntHolder(0);
    // final GIntHolder polygonsWithHolesCounter = new GIntHolder(0);
    final GIntHolder invalidCounter = new GIntHolder(0);
    //      final GIntHolder validVerticesCounter = new GIntHolder(0);
    final GIntHolder polygonsCounter = new GIntHolder(0);
    final GIntHolder linesCounter = new GIntHolder(0);
    final GIntHolder pointsCounter = new GIntHolder(0);

    final int featuresCount = featuresCollection.size();
    final ArrayList<IGlobeFeature<IVector2, IBoundedGeometry2D<? extends IFinite2DBounds<?>>>>
        euclidFeatures =
            new ArrayList<
                IGlobeFeature<IVector2, IBoundedGeometry2D<? extends IFinite2DBounds<?>>>>(
                featuresCount);

    final GProgress progress =
        new GProgress(featuresCount) {
          @Override
          public void informProgress(
              final double percent, final long elapsed, final long estimatedMsToFinish) {
            //            System.out.println("Loading \"" + fileName.buildPath() + "\" "
            //                               + progressString(percent, elapsed,
            // estimatedMsToFinish));
            System.out.println(
                "Loading data from data storage: "
                    + layerName
                    + " "
                    + progressString(percent, elapsed, estimatedMsToFinish));
          }
        };

    final FeatureIterator<SimpleFeature> iterator = featuresCollection.features();

    while (iterator.hasNext()) {
      final SimpleFeature feature = iterator.next();

      final GeometryAttribute geometryAttribute = feature.getDefaultGeometryProperty();

      final GeometryType type = geometryAttribute.getType();

      if (type.getBinding() == com.vividsolutions.jts.geom.MultiPolygon.class) {

        polygonsCounter.increment();

        final com.vividsolutions.jts.geom.MultiPolygon multipolygon =
            (com.vividsolutions.jts.geom.MultiPolygon) geometryAttribute.getValue();
        final int geometriesCount = multipolygon.getNumGeometries();

        final List<IPolygon2D> polygons = new ArrayList<IPolygon2D>(geometriesCount);
        for (int i = 0; i < geometriesCount; i++) {
          final com.vividsolutions.jts.geom.Polygon jtsPolygon =
              (com.vividsolutions.jts.geom.Polygon) multipolygon.getGeometryN(i);

          try {
            final IPolygon2D euclidPolygon = createEuclidPolygon(projection, jtsPolygon);

            if (euclidPolygon != null) {
              //                     euclidFeatures.add(createFeature(euclidPolygon, feature));
              polygons.add(euclidPolygon);
              validCounter.increment();
            }
          } catch (final IllegalArgumentException e) {
            //                     System.err.println(e.getMessage());
          }
        }

        if (!polygons.isEmpty()) {
          if (polygons.size() == 1) {
            euclidFeatures.add(createFeature(polygons.get(0), feature));
          } else {
            euclidFeatures.add(createFeature(new GMultiGeometry2D<IPolygon2D>(polygons), feature));
          }
        }

      } else if (type.getBinding() == com.vividsolutions.jts.geom.MultiLineString.class) {

        linesCounter.increment();

        final com.vividsolutions.jts.geom.MultiLineString multiline =
            (com.vividsolutions.jts.geom.MultiLineString) geometryAttribute.getValue();
        final int geometriesCount = multiline.getNumGeometries();

        final List<IPolygonalChain2D> lines = new ArrayList<IPolygonalChain2D>(geometriesCount);
        for (int i = 0; i < geometriesCount; i++) {
          final com.vividsolutions.jts.geom.LineString jtsLine =
              (com.vividsolutions.jts.geom.LineString) multiline.getGeometryN(i);

          try {
            final IPolygonalChain2D euclidLine = createLine(jtsLine.getCoordinates(), projection);

            // euclidFeatures.add(createFeature(euclidLines, feature));
            lines.add(euclidLine);
          } catch (final IllegalArgumentException e) {
            //                     System.err.println(e.getMessage());
          }
        }

        if (!lines.isEmpty()) {
          if (lines.size() == 1) {
            euclidFeatures.add(createFeature(lines.get(0), feature));
          } else {
            euclidFeatures.add(
                createFeature(new GMultiGeometry2D<IPolygonalChain2D>(lines), feature));
          }
        }

        validCounter.increment();
      } else if (type.getBinding() == com.vividsolutions.jts.geom.Point.class) {

        pointsCounter.increment();

        final IVector2 euclidPoint =
            createPoint(
                ((com.vividsolutions.jts.geom.Point) geometryAttribute.getValue()).getCoordinate(),
                projection);
        euclidFeatures.add(createFeature(euclidPoint, feature));

        validCounter.increment();
      } else if (type.getBinding() == com.vividsolutions.jts.geom.MultiPoint.class) {
        final IBoundedGeometry2D<? extends IFinite2DBounds<?>> euclidMultipoint =
            createEuclidMultiPoint(geometryAttribute, projection);
        euclidFeatures.add(createFeature(euclidMultipoint, feature));

        validCounter.increment();
      } else {
        invalidCounter.increment();
        System.out.println("invalid type: " + type);
      }

      progress.stepDone();
    }

    dataStore.dispose();

    euclidFeatures.trimToSize();

    System.out.println();
    System.out.println("Features: " + featuresCount);

    System.out.println();
    System.out.println("Read " + validCounter.get() + " valid geometries");

    System.out.println("  => " + polygonsCounter.get() + " valid polygons");
    System.out.println("  => " + linesCounter.get() + " valid lines");
    System.out.println("  => " + pointsCounter.get() + " valid points");
    System.out.println();

    if (invalidCounter.get() > 0) {
      System.out.println("Ignored " + invalidCounter.get() + " invalid geometries");
    }

    System.out.println();

    final SimpleFeatureType schema = featureSource.getSchema();
    final int fieldsCount = schema.getAttributeCount();
    final List<GField> fields = new ArrayList<GField>(fieldsCount);
    System.out.println("Fields count: " + fieldsCount);
    for (int i = 0; i < fieldsCount; i++) {
      final String fieldName = schema.getType(i).getName().getLocalPart();
      System.out.println("Fieldname: " + fieldName);
      final Class<?> fieldType = schema.getType(i).getBinding();

      fields.add(new GField(fieldName, fieldType));
    }

    return new GListFeatureCollection<IVector2, IBoundedGeometry2D<? extends IFinite2DBounds<?>>>(
        GProjection.EPSG_4326, fields, euclidFeatures, "uniqueId_000");
  }
Exemplo n.º 7
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;
  }
Exemplo n.º 8
0
  public void write(WriteBufferManager buffer, Object geometry) throws IOException {
    MultiLineString multi = (MultiLineString) geometry;

    Envelope box = multi.getEnvelopeInternal();
    buffer.putDouble(box.getMinX());
    buffer.putDouble(box.getMinY());
    buffer.putDouble(box.getMaxX());
    buffer.putDouble(box.getMaxY());

    int numParts = multi.getNumGeometries();

    buffer.putInt(numParts);
    int npoints = multi.getNumPoints();
    buffer.putInt(npoints);

    LineString[] lines = new LineString[numParts];
    int idx = 0;

    for (int i = 0; i < numParts; i++) {
      lines[i] = (LineString) multi.getGeometryN(i);
      buffer.putInt(idx);
      idx = idx + lines[i].getNumPoints();
    }

    Coordinate[] coords = multi.getCoordinates();

    for (int t = 0; t < npoints; t++) {
      buffer.putDouble(coords[t].x);
      buffer.putDouble(coords[t].y);
    }

    if (shapeType == ShapeType.ARCZ) {
      double[] zExtreame = JTSUtilities.zMinMax(coords);

      if (Double.isNaN(zExtreame[0])) {
        buffer.putDouble(0.0);
        buffer.putDouble(0.0);
      } else {
        buffer.putDouble(zExtreame[0]);
        buffer.putDouble(zExtreame[1]);
      }

      for (int t = 0; t < npoints; t++) {
        double z = coords[t].z;

        if (Double.isNaN(z)) {
          buffer.putDouble(0.0);
        } else {
          buffer.putDouble(z);
        }
      }
    }

    if (shapeType == ShapeType.ARCZ) {
      buffer.putDouble(-10E40);
      buffer.putDouble(-10E40);

      for (int t = 0; t < npoints; t++) {
        buffer.putDouble(-10E40);
      }
    }
  }