Exemplo n.º 1
0
  protected Geometry transformPolygon(Polygon geom, Geometry parent) {
    boolean isAllValidLinearRings = true;
    Geometry shell = transformLinearRing((LinearRing) geom.getExteriorRing(), geom);

    if (shell == null || !(shell instanceof LinearRing) || shell.isEmpty())
      isAllValidLinearRings = false;
    // return factory.createPolygon(null, null);

    ArrayList holes = new ArrayList();
    for (int i = 0; i < geom.getNumInteriorRing(); i++) {
      Geometry hole = transformLinearRing((LinearRing) geom.getInteriorRingN(i), geom);
      if (hole == null || hole.isEmpty()) {
        continue;
      }
      if (!(hole instanceof LinearRing)) isAllValidLinearRings = false;

      holes.add(hole);
    }

    if (isAllValidLinearRings)
      return factory.createPolygon(
          (LinearRing) shell, (LinearRing[]) holes.toArray(new LinearRing[] {}));
    else {
      List components = new ArrayList();
      if (shell != null) components.add(shell);
      components.addAll(holes);
      return factory.buildGeometry(components);
    }
  }
Exemplo n.º 2
0
 private static Geometry convertSegStrings(Iterator it) {
   GeometryFactory fact = new GeometryFactory();
   List lines = new ArrayList();
   while (it.hasNext()) {
     SegmentString ss = (SegmentString) it.next();
     LineString line = fact.createLineString(ss.getCoordinates());
     lines.add(line);
   }
   return fact.buildGeometry(lines);
 }
Exemplo n.º 3
0
 protected Geometry transformMultiPolygon(MultiPolygon geom, Geometry parent) {
   List transGeomList = new ArrayList();
   for (int i = 0; i < geom.getNumGeometries(); i++) {
     Geometry transformGeom = transformPolygon((Polygon) geom.getGeometryN(i), geom);
     if (transformGeom == null) continue;
     if (transformGeom.isEmpty()) continue;
     transGeomList.add(transformGeom);
   }
   return factory.buildGeometry(transGeomList);
 }
Exemplo n.º 4
0
  private Geobuf.Data.Feature makeFeature(GeobufFeature feature, List<String> keys) {
    Geobuf.Data.Feature.Builder feat =
        Geobuf.Data.Feature.newBuilder().setGeometry(geomToGeobuf(feature.geometry));

    for (Map.Entry<String, Object> e : feature.properties.entrySet()) {
      // TODO store keys separately from features
      Geobuf.Data.Value.Builder val = Geobuf.Data.Value.newBuilder();

      Object featVal = e.getValue();

      if (featVal instanceof String) val.setStringValue((String) featVal);
      else if (featVal instanceof Boolean) val.setBoolValue((Boolean) featVal);
      else if (featVal instanceof Integer) {
        int keyInt = (Integer) featVal;
        if (keyInt >= 0) val.setPosIntValue(keyInt);
        else val.setNegIntValue(keyInt);
      } else if (featVal instanceof Long) {
        long keyLong = (Long) featVal;
        if (keyLong >= 0) val.setPosIntValue(keyLong);
        else val.setNegIntValue(keyLong);
      } else if (featVal instanceof Double || featVal instanceof Float)
        val.setDoubleValue(((Number) featVal).doubleValue());
      else {
        // TODO serialize to JSON
        LOG.warn(
            "Unable to save object of type {} to geobuf, falling back on toString. Deserialization will not work as expected.",
            featVal.getClass());
        val.setStringValue(featVal.toString());
      }

      int keyIdx = keys.indexOf(e.getKey());
      if (keyIdx == -1) {
        synchronized (keys) {
          keyIdx = keys.size();
          keys.add(e.getKey());
        }
      }

      // properties is a jagged array of [key index, value index, . . .]
      feat.addProperties(keyIdx);
      feat.addProperties(feat.getValuesCount());
      feat.addValues(val);
    }

    if (feature.id != null) {
      feat.setId(feature.id);
      feat.clearIntId();
    } else {
      feat.setIntId(feature.numericId);
      feat.clearId();
    }

    return feat.build();
  }
Exemplo n.º 5
0
 protected Geometry transformGeometryCollection(GeometryCollection geom, Geometry parent) {
   List transGeomList = new ArrayList();
   for (int i = 0; i < geom.getNumGeometries(); i++) {
     Geometry transformGeom = transform(geom.getGeometryN(i));
     if (transformGeom == null) continue;
     if (pruneEmptyGeometry && transformGeom.isEmpty()) continue;
     transGeomList.add(transformGeom);
   }
   if (preserveGeometryCollectionType)
     return factory.createGeometryCollection(GeometryFactory.toGeometryArray(transGeomList));
   return factory.buildGeometry(transGeomList);
 }
Exemplo n.º 6
0
 /**
  * Computes the list of envelopes (from 2 to 4, possibily 0 if env covers this) resulting from the
  * extrusion of env from this. Only in 2D for the moment. Does not return null envelopes.
  */
 public List<Envelope> extrusion(final Envelope env) {
   List<Envelope> list = new ArrayList();
   double x1 = getMinX();
   double x2 = getMaxX();
   double y1 = getMinY();
   double y2 = getMaxY();
   double xx1 = env.getMinX();
   double xx2 = env.getMaxX();
   double yy1 = env.getMinY();
   double yy2 = env.getMaxY();
   if (x2 >= x1 && yy1 >= y1) {
     list.add(new Envelope(x1, x2, y1, yy1));
   }
   if (xx1 >= x1 && y2 >= yy1) {
     list.add(new Envelope(x1, xx1, yy1, y2));
   }
   if (x2 >= xx1 && y2 >= yy2) {
     list.add(new Envelope(xx1, x2, yy2, y2));
   }
   if (x2 >= xx2 && yy2 >= yy1) {
     list.add(new Envelope(xx2, x2, yy1, yy2));
   }
   return list;
 }
Exemplo n.º 7
0
 private List createSubgraphs(PlanarGraph graph) {
   List subgraphList = new ArrayList();
   for (Iterator i = graph.getNodes().iterator(); i.hasNext(); ) {
     Node node = (Node) i.next();
     if (!node.isVisited()) {
       BufferSubgraph subgraph = new BufferSubgraph();
       subgraph.create(node);
       subgraphList.add(subgraph);
     }
   }
   /**
    * Sort the subgraphs in descending order of their rightmost coordinate. This ensures that when
    * the Polygons for the subgraphs are built, subgraphs for shells will have been built before
    * the subgraphs for any holes they contain.
    */
   Collections.sort(subgraphList, Collections.reverseOrder());
   return subgraphList;
 }
Exemplo n.º 8
0
 /**
  * Completes the building of the input subgraphs by depth-labelling them, and adds them to the
  * PolygonBuilder. The subgraph list must be sorted in rightmost-coordinate order.
  *
  * @param subgraphList the subgraphs to build
  * @param polyBuilder the PolygonBuilder which will build the final polygons
  */
 private void buildSubgraphs(List subgraphList, PolygonBuilder polyBuilder) {
   List processedGraphs = new ArrayList();
   for (Iterator i = subgraphList.iterator(); i.hasNext(); ) {
     BufferSubgraph subgraph = (BufferSubgraph) i.next();
     Coordinate p = subgraph.getRightmostCoordinate();
     //      int outsideDepth = 0;
     //      if (polyBuilder.containsPoint(p))
     //        outsideDepth = 1;
     SubgraphDepthLocater locater = new SubgraphDepthLocater(processedGraphs);
     int outsideDepth = locater.getDepth(p);
     //      try {
     subgraph.computeDepth(outsideDepth);
     //      }
     //      catch (RuntimeException ex) {
     //        // debugging only
     //        //subgraph.saveDirEdges();
     //        throw ex;
     //      }
     subgraph.findResultEdges();
     processedGraphs.add(subgraph);
     polyBuilder.add(subgraph.getDirectedEdges(), subgraph.getNodes());
   }
 }
Exemplo n.º 9
0
  /**
   * This method is called by clients of the {@link SegmentIntersector} class to process
   * intersections for two segments of the {@link SegmentString}s being intersected. Note that some
   * clients (such as {@link MonotoneChain}s) may optimize away this call for segment pairs which
   * they have determined do not intersect (e.g. by an disjoint envelope test).
   */
  public void processIntersections(
      SegmentString e0, int segIndex0, SegmentString e1, int segIndex1) {
    // don't bother intersecting a segment with itself
    if (e0 == e1 && segIndex0 == segIndex1) return;

    Coordinate p00 = e0.getCoordinates()[segIndex0];
    Coordinate p01 = e0.getCoordinates()[segIndex0 + 1];
    Coordinate p10 = e1.getCoordinates()[segIndex1];
    Coordinate p11 = e1.getCoordinates()[segIndex1 + 1];

    li.computeIntersection(p00, p01, p10, p11);
    // if (li.hasIntersection() && li.isProper()) Debug.println(li);

    if (li.hasIntersection()) {
      if (li.isInteriorIntersection()) {
        for (int intIndex = 0; intIndex < li.getIntersectionNum(); intIndex++) {
          interiorIntersections.add(li.getIntersection(intIndex));
        }
        ((NodedSegmentString) e0).addIntersections(li, segIndex0, 0);
        ((NodedSegmentString) e1).addIntersections(li, segIndex1, 1);
      }
    }
  }
Exemplo n.º 10
0
 public void addToResult(LineSegment seg) {
   resultSegs.add(seg);
 }
Exemplo n.º 11
0
 /**
  * Adds a hole to the polygon formed by this ring.
  *
  * @param hole the {@link LinearRing} forming the hole.
  */
 public void addHole(LinearRing hole) {
   if (holes == null) holes = new ArrayList();
   holes.add(hole);
 }
Exemplo n.º 12
0
 /**
  * Adds a {@link DirectedEdge} which is known to form part of this ring.
  *
  * @param de the {@link DirectedEdge} to add.
  */
 public void add(DirectedEdge de) {
   deList.add(de);
 }