Java Code Examples for com.vividsolutions.jts.geom.Geometry#getNumGeometries()

The following examples show how to use com.vividsolutions.jts.geom.Geometry#getNumGeometries() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GamaGisFile.java    From gama with GNU General Public License v3.0 8 votes vote down vote up
protected Geometry multiPolygonManagement(final Geometry geom) {
	if (geom instanceof MultiPolygon) {
		final Polygon gs[] = new Polygon[geom.getNumGeometries()];
		for (int i = 0; i < geom.getNumGeometries(); i++) {
			final Polygon p = (Polygon) geom.getGeometryN(i);
			final ICoordinates coords = GeometryUtils.getContourCoordinates(p);
			final LinearRing lr = GEOMETRY_FACTORY.createLinearRing(coords.toCoordinateArray());
			try (final Collector.AsList<LinearRing> holes = Collector.getList()) {
				for (int j = 0; j < p.getNumInteriorRing(); j++) {
					final LinearRing h = (LinearRing) p.getInteriorRingN(j);
					if (!hasNullElements(h.getCoordinates())) {
						holes.add(h);
					}
				}
				LinearRing[] stockArr = new LinearRing[holes.size()];
				stockArr = holes.items().toArray(stockArr);
				gs[i] = GEOMETRY_FACTORY.createPolygon(lr, stockArr);
			}
		}
		return GEOMETRY_FACTORY.createMultiPolygon(gs);
	}
	return geom;
}
 
Example 2
Source File: NominatimGeocoder.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
private static Geometry convexHullOneLevel(final Geometry geometry)
{
   if (geometry.getNumGeometries() > 1)
   {
      Geometry [] convex_hulls = new Geometry [geometry.getNumGeometries()];
      for (int igeom=0; igeom<geometry.getNumGeometries(); igeom++)
      {
         convex_hulls[igeom] = geometry.getGeometryN(igeom).convexHull();
      }
      return
         geometry.getFactory().createGeometryCollection(convex_hulls);
   }
   else
   {
      return geometry.convexHull();
   }
}
 
Example 3
Source File: GamaShape.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setInnerGeometry(final Geometry geom) {
	if (geom == null) {
		geometry = null;
		return;
	}
	if (geom.isEmpty()) {
		// See Issue 725
		return;
	}
	if (geom instanceof GeometryCollection && geom.getNumGeometries() == 1) {
		geometry = geom.getGeometryN(0);
	} else {
		geometry = geom;
	}
}
 
Example 4
Source File: Projection.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Geometry transform(final Geometry g) {
	// Remove uselessly complicated multigeometries
	if (g instanceof GeometryCollection && g.getNumGeometries() == 1) { return transform(g.getGeometryN(0)); }
	Geometry geom = GeometryUtils.GEOMETRY_FACTORY.createGeometry(g);
	if (transformer != null) {
		try {
			geom = transformer.transform(geom);
		} catch (final TransformException e) {
			e.printStackTrace();
		}
	}
	translate(geom);
	convertUnit(geom);
	return geom;
}
 
Example 5
Source File: GamaSpatialMatrix.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method getGeometries()
 *
 * @see msi.gama.metamodel.shape.IShape#getGeometries()
 */
@Override
public IList<? extends IShape> getGeometries() {

	final IList<IShape> result = GamaListFactory.create(Types.GEOMETRY);
	if (isMultiple()) {
		final Geometry g = getInnerGeometry();
		for (int i = 0, n = g.getNumGeometries(); i < n; i++) {
			result.add(new GamaShape(g.getGeometryN(i)));
		}
	} else {
		result.add(this);
	}
	return result;

}
 
Example 6
Source File: Distance3DOp.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void computeMinDistanceMultiMulti(Geometry g0, Geometry g1, boolean flip) {
	if (g0 instanceof GeometryCollection) {
		int n = g0.getNumGeometries();
		for (int i = 0; i < n; i++) {
			Geometry g = g0.getGeometryN(i);
			computeMinDistanceMultiMulti(g, g1, flip);
			if (isDone)	return;
		}
	}
	else {
		// handle case of multigeom component being empty
		if (g0.isEmpty())
			return;
		
		// compute planar polygon only once for efficiency
		if (g0 instanceof Polygon) {
			computeMinDistanceOneMulti(polyPlane(g0), g1, flip);
		}
		else 
			computeMinDistanceOneMulti(g0, g1, flip);
	}
}
 
Example 7
Source File: Distance3DOp.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void computeMinDistanceOneMulti(PlanarPolygon3D poly, Geometry geom, boolean flip) {
	if (geom instanceof GeometryCollection) {
		int n = geom.getNumGeometries();
		for (int i = 0; i < n; i++) {
			Geometry g = geom.getGeometryN(i);
			computeMinDistanceOneMulti(poly, g, flip);
			if (isDone)	return;
		}
	}
	else {
		if (geom instanceof Point) {
			computeMinDistancePolygonPoint(poly, (Point) geom, flip);
			return;
		}
		if (geom instanceof LineString) {
			computeMinDistancePolygonLine(poly, (LineString) geom, flip);
			return;
		}
		if (geom instanceof Polygon) {
			computeMinDistancePolygonPolygon(poly, (Polygon) geom, flip);
			return;
		}
	}
}
 
Example 8
Source File: GMLTraversabilityValidator.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Find the index of the subgeometry the given edge is part of. Return -1 if
 * the edge is not contained in the geometry at all.
 * @param edge
 * @param geom
 * @return
 */
private static int findPolygonPartOfEdge(GMLDirectedEdge edge, Geometry geom) {
    for (int i = 0; i < geom.getNumGeometries(); i++) {
        if (edgePartOfPolygon(edge, geom.getGeometryN(i))) {
            return i;
        }
    }
    return -1;
}
 
Example 9
Source File: SortingFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static List components(Geometry g)
{
  List comp = new ArrayList();
  for (int i = 0; i < g.getNumGeometries(); i++) {
    comp.add(g.getGeometryN(i));
  }
  return comp;
}
 
Example 10
Source File: ValidationFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Validates all geometries in a collection independently.
 * Errors are returned as points at the invalid location
 * 
 * @param g
 * @return the invalid locations, if any
 */
public static Geometry invalidLocations(Geometry g)
{
  List invalidLoc = new ArrayList();
  for (int i = 0; i < g.getNumGeometries(); i++) {
    Geometry geom = g.getGeometryN(i);
    IsValidOp ivop = new IsValidOp(geom);
    TopologyValidationError err = ivop.getValidationError();
    if (err != null) {
      invalidLoc.add(g.getFactory().createPoint(err.getCoordinate()));
    }
  }
  return g.getFactory().buildGeometry(invalidLoc);
}
 
Example 11
Source File: GeometryDataUtil.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void setComponentDataToIndex(Geometry geom)
{
	for (int i = 0; i < geom.getNumGeometries(); i++) {
		Geometry comp = geom.getGeometryN(i);
		comp.setUserData("Component # " + i); 
	}
}
 
Example 12
Source File: Distance3DOp.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void computeMinDistanceOneMulti(Geometry g0, Geometry g1, boolean flip) {
	if (g1 instanceof GeometryCollection) {
		int n = g1.getNumGeometries();
		for (int i = 0; i < n; i++) {
			Geometry g = g1.getGeometryN(i);
			computeMinDistanceOneMulti(g0, g, flip);
			if (isDone)	return;
		}
	}
	else {
		computeMinDistance(g0, g1, flip);
	}
}
 
Example 13
Source File: SvgFeatureWriter.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
public void writeObject(Object object, GraphicsDocument document, boolean asChild) throws RenderException {
	try {
		InternalFeatureImpl feature = (InternalFeatureImpl) object;
		Geometry geom = feature.getGeometry();
		if (feature.isClipped()) {
			geom = feature.getClippedGeometry();
		}
		geom = transformer.transform(geom);

		if (geom instanceof Point || geom instanceof MultiPoint) {
			// write the enclosing group
			document.writeElement("g", asChild);
			document.writeAttribute("id", feature.getId());

			// write the points
			for (int i = 0; i < geom.getNumGeometries(); i++) {
				document.writeObject(geom.getGeometryN(i), true);
				document.writeAttribute("id", feature.getId());
				document.writeAttribute("xlink:href", "#" + feature.getStyleInfo().getStyleId());
				document.closeElement();
			}
		} else {
			document.writeObject(geom, asChild);
			document.writeAttribute("id", feature.getId());
		}
	} catch (TransformException e) {
		log.warn("could not render feature");
	}
}
 
Example 14
Source File: MergePolygonCommand.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void execute(MergePolygonRequest request, MergePolygonResponse response) throws Exception {
	Polygon[] polygons = new Polygon[request.getPolygons().length];
	for (int i = 0; i < request.getPolygons().length; i++) {
		try {
			polygons[i] = (Polygon) converter.toInternal(request.getPolygons()[i]);
		} catch (Exception e) {
			throw new GeomajasException(e, ExceptionCode.MERGE_NO_POLYGON);
		}
	}
	int precision = polygons[0].getPrecisionModel().getMaximumSignificantDigits() - 1;
	PrecisionModel precisionModel = new PrecisionModel(Math.pow(10.0, precision));
	GeometryFactory factory = new GeometryFactory(precisionModel, polygons[0].getSRID());

	Geometry temp = factory.createGeometry(polygons[0]);
	for (int i = 1; i < polygons.length; i++) {
		Geometry polygon = factory.createGeometry(polygons[i]);
		temp = temp.union(polygon.buffer(Math.pow(10.0, -(precision - 1))));
	}
	if (temp instanceof Polygon) {
		MultiPolygon mp = factory.createMultiPolygon(new Polygon[] { (Polygon) temp });
		response.setGeometry(converter.toDto(mp));
	} else if (temp instanceof MultiPolygon && temp.getNumGeometries() != 0
			&& (request.isAllowMultiPolygon() || temp.getNumGeometries() == 1)) {
		response.setGeometry(converter.toDto(temp));
	} else {
		throw new GeomajasException(ExceptionCode.MERGE_NO_POLYGON);
	}
}
 
Example 15
Source File: NominatimGeocoder.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
private static Geometry simplifyGeometry(final Geometry input_geometry,
      final int max_output_points, final double cluster_factor)
{
   // Return null if the input geometry is null
   if (input_geometry == null)
   {
      return null;
   }

   // Return the input geometry if the number of points is already lower
   // or equal to the maximum allowed
   if (input_geometry.getNumPoints() <= max_output_points)
   {
      return input_geometry;
   }

   // Assign local geometry to refine
   Geometry geometry = input_geometry;

   if (geometry.getNumGeometries() > 1)
   {
      geometry = convexHullOneLevel(geometry).union();
      geometry = clusterizeGeometry(geometry, cluster_factor);
   }

   int current_point_number = geometry.getNumPoints();
   int previous_point_number = -1;

   int iteration_count = 0;

   double tolerance = 0.005;

   while ((current_point_number > max_output_points) &&
          (iteration_count < 10))
   {
      previous_point_number = current_point_number;
      current_point_number = geometry.getNumPoints();
      if (current_point_number == previous_point_number)
      {
         iteration_count += 1;
      }
      else
      {
         iteration_count = 0;
      }

      geometry =
         TopologyPreservingSimplifier.simplify(geometry,
               tolerance);

      tolerance += 0.005;
   }

   return geometry;
}
 
Example 16
Source File: NominatimGeocoder.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
private static Geometry clusterizeGeometry(final Geometry geometry,
      final double distance_ratio)
{
   if (geometry == null)
   {
      return null;
   }

   int number_geometries = geometry.getNumGeometries();

   if (number_geometries > 1)
   {
      Geometry [] clustered_geometries =
         new Geometry [number_geometries];

      for (int igeom=0; igeom<number_geometries-1; igeom++)
      {
         Geometry current_geometry = geometry.getGeometryN(igeom);
         Point current_centroid = current_geometry.getCentroid();

         if ((current_geometry == null) ||
             (current_centroid == null))
         {
            // TODO Warning
            continue;
         }

         ArrayList<Geometry> current_cluster = new ArrayList<Geometry>();

         current_cluster.add(current_geometry);

         for (int jgeom=igeom+1; jgeom<number_geometries; jgeom++)
         {
            Geometry next_geometry = geometry.getGeometryN(jgeom);
            Point next_centroid = next_geometry.getCentroid();

            if ((next_geometry == null) ||
                (next_centroid == null))
            {
               // TODO Warning
               continue;
            }

            double distance = current_geometry.distance(next_geometry);
            double centroids_distance =
               current_centroid.distance(next_centroid);

            if (distance < (centroids_distance * distance_ratio))
            {
               current_cluster.add(next_geometry);
            }
         }

         Geometry [] current_cluster_array =
               new Geometry [current_cluster.size()];

         clustered_geometries[igeom] =
            geometry.getFactory().createGeometryCollection(
               current_cluster.toArray(current_cluster_array));
      }

      clustered_geometries[number_geometries-1] =
         geometry.getGeometryN(number_geometries-1);

      return convexHullOneLevel(
         geometry.getFactory().createGeometryCollection(
            clustered_geometries)).union();
   }
   else
   {
      return geometry;
   }
}