Java Code Examples for com.vividsolutions.jts.geom.GeometryFactory#createGeometryCollection()

The following examples show how to use com.vividsolutions.jts.geom.GeometryFactory#createGeometryCollection() . 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: PolygonOverlayFunctions.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static Geometry overlaySnapRounded(Geometry g1, Geometry g2, double precisionTol)
{
  PrecisionModel pm = new PrecisionModel(precisionTol);
  GeometryFactory geomFact = g1.getFactory();
  
  List lines = LinearComponentExtracter.getLines(g1);
  // add second input's linework, if any
  if (g2 != null)
    LinearComponentExtracter.getLines(g2, lines);
  List nodedLinework = new GeometryNoder(pm).node(lines);
  // union the noded linework to remove duplicates
  Geometry nodedDedupedLinework = geomFact.buildGeometry(nodedLinework).union();
  
  // polygonize the result
  Polygonizer polygonizer = new Polygonizer();
  polygonizer.add(nodedDedupedLinework);
  Collection polys = polygonizer.getPolygons();
  
  // convert to collection for return
  Polygon[] polyArray = GeometryFactory.toPolygonArray(polys);
  return geomFact.createGeometryCollection(polyArray);
}
 
Example 2
Source File: GeometryCollectionImpl.java    From mrgeo with Apache License 2.0 6 votes vote down vote up
@Override
public com.vividsolutions.jts.geom.GeometryCollection toJTS()
{
  GeometryFactory factory = new GeometryFactory();

  com.vividsolutions.jts.geom.Geometry[] jtsGeometries = new com.vividsolutions.jts.geom.Geometry[geometries.size()];
//    LinkedList<com.vividsolutions.jts.geom.Geometry> jtsGeometries = new LinkedList<com.vividsolutions.jts.geom.Geometry>();
  int i = 0;
  for (Geometry geometry : geometries)
  {
//      jtsGeometries.add(geometry.toJTS());
    jtsGeometries[i++] = geometry.toJTS();
  }

//    return factory.createGeometryCollection(
//      (com.vividsolutions.jts.geom.Geometry[]) jtsGeometries.toArray());
  return factory.createGeometryCollection(jtsGeometries);
}
 
Example 3
Source File: OraReaderCreateTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testRawGeometryCollectionWithPoint() throws Exception {
  final GeometryFactory geometryFactory = new GeometryFactory();
  final OraReader oraReader = new OraReader(geometryFactory);

  // Geometry type is a 'collection'.
  final int gType = 2004;
  // A collection of a 'line' and a 'point'/
  // The 'line' is starting at ordinate offset 1.
  // The 'point' is starting at ordinate offset 5.
  final int[] elemInfo = new int[] {1, 2, 1, 5, 1, 1};
  // 6 ordinates.
  // 'line' (1, 1, 2, 2).
  // 'point' (3, 3).
  final double[] ordinates = new double[] {1, 1, 2, 2, 3, 3};
  // Made 'create' method package private to enable test.
  final Geometry actual = oraReader.read(new OraGeom(gType, 0, elemInfo, ordinates));

  // Preparing expected result.
  final LineString lineString =
      geometryFactory.createLineString(new Coordinate[] {new Coordinate(1, 1), new Coordinate(2, 2)});
  final Point point = geometryFactory.createPoint(new Coordinate(3, 3));

  final List<Geometry> geometries = new ArrayList<Geometry>();
  geometries.add(lineString);
  geometries.add(point);

  final GeometryCollection expected =
      geometryFactory.createGeometryCollection(GeometryFactory.toGeometryArray(geometries));

  assertEquals(expected, actual);
}
 
Example 4
Source File: IOUtil.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static Geometry readShapefile(String filename, GeometryFactory geomFact)
throws Exception 
{
  Shapefile shpfile = new Shapefile(new FileInputStream(filename));
  shpfile.readStream(geomFact);
  List geomList = new ArrayList();
  do {
    Geometry geom = shpfile.next();
    if (geom == null)
      break;
    geomList.add(geom);
  } while (true);
  
  return geomFact.createGeometryCollection(GeometryFactory.toGeometryArray(geomList));
}
 
Example 5
Source File: IOUtil.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Reads one or more WKT geometries from a string.
 * 
 * @param wkt
 * @param geomFact
 * @return the geometry read
 * @throws ParseException
 * @throws IOException
 */
public static Geometry readWKTString(String wkt, GeometryFactory geomFact)
throws ParseException, IOException 
{
  WKTReader reader = new WKTReader(geomFact);
  WKTFileReader fileReader = new WKTFileReader(new StringReader(wkt), reader);
  List geomList = fileReader.read();
  
  if (geomList.size() == 1)
    return (Geometry) geomList.get(0);
  
  return geomFact.createGeometryCollection(GeometryFactory.toGeometryArray(geomList));
}
 
Example 6
Source File: IOUtil.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry readWKBHexString(String wkb, GeometryFactory geomFact)
throws ParseException, IOException 
{
  WKBReader reader = new WKBReader(geomFact);
  WKBHexFileReader fileReader = new WKBHexFileReader(new StringReader(wkb), reader);
  List geomList = fileReader.read();
  
  if (geomList.size() == 1)
    return (Geometry) geomList.get(0);
  
  return geomFact.createGeometryCollection(GeometryFactory.toGeometryArray(geomList));
}
 
Example 7
Source File: StyleConverterServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private Geometry toGeometry(GeometryFactory factory, AbstractGeometryInfo geom) throws LayerException {
	Geometry geometry = null;
	if (geom instanceof AbstractGeometryCollectionInfo) {
		AbstractGeometryCollectionInfo geomCollection = (AbstractGeometryCollectionInfo) geom;
		List<GeometryMemberInfo> members = geomCollection.getGeometryMemberList();
		if (geom instanceof MultiPointInfo) {
			Point[] points = new Point[members.size()];
			for (int i = 0; i < members.size(); i++) {
				points[i] = (Point) toSimpleGeometry(factory, members.get(i).getGeometry());
			}
			geometry = factory.createMultiPoint(points);
		} else if (geom instanceof MultiLineStringInfo) {
			LineString[] lines = new LineString[members.size()];
			for (int i = 0; i < members.size(); i++) {
				lines[i] = (LineString) toSimpleGeometry(factory, members.get(i).getGeometry());
			}
			geometry = factory.createMultiLineString(lines);
		} else if (geom instanceof MultiPolygonInfo) {
			Polygon[] polygons = new Polygon[members.size()];
			for (int i = 0; i < members.size(); i++) {
				polygons[i] = (Polygon) toSimpleGeometry(factory, members.get(i).getGeometry());
			}
			geometry = factory.createMultiPolygon(polygons);
		} else if (geom instanceof MultiGeometryInfo) {
			Geometry[] geometries = new Geometry[members.size()];
			for (int i = 0; i < members.size(); i++) {
				geometries[i] = toGeometry(factory, members.get(i).getGeometry());
			}
			geometry = factory.createGeometryCollection(geometries);
		}
	} else {
		geometry = toSimpleGeometry(factory, geom);
	}
	return geometry;
}
 
Example 8
Source File: GeoJsonReader.java    From jts with GNU Lesser General Public License v2.1 3 votes vote down vote up
private Geometry createGeometryCollection(Map<String, Object> geometryMap,
    GeometryFactory geometryFactory) throws ParseException {

  Geometry result = null;

  try {

    @SuppressWarnings("unchecked")
    List<Map<String, Object>> geometriesList = (List<Map<String, Object>>) geometryMap
        .get(GeoJsonConstants.NAME_GEOMETRIES);

    Geometry[] geometries = new Geometry[geometriesList.size()];

    int i = 0;
    for (Map<String, Object> map : geometriesList) {

      geometries[i] = this.create(map, geometryFactory);

      ++i;
    }

    result = geometryFactory.createGeometryCollection(geometries);

  } catch (RuntimeException e) {
    throw new ParseException(
        "Could not parse GeometryCollection from GeoJson string.", e);
  }

  return result;
}
 
Example 9
Source File: SimpleDemo.java    From jts with GNU Lesser General Public License v2.1 2 votes vote down vote up
private Geometry demonstrateGeometryAndWktWriter() {
	GeometryFactory gf = new GeometryFactory();

	WKTWriter wkt2 = new WKTWriter();

	WKTWriter wkt3 = new WKTWriter(3);

	Coordinate c1 = new Coordinate(1234234.233442, 2234234.234234,
			3323424.2342342);
	Point p = gf.createPoint(c1);

	sLogger.info("Point WKT: " + wkt2.write(p));
	sLogger.info("Point WKT3: " + wkt3.write(p));

	Coordinate c2 = new Coordinate(4234234.233442, 5234223.234234,
			5323424.2342342);

	Coordinate[] c3 = new Coordinate[] { c1, c2 };

	MultiPoint mp = gf.createMultiPoint(c3);

	sLogger.info("Point WKT: " + wkt2.write(mp));
	sLogger.info("Point WKT3: " + wkt3.write(mp));

	LineString ls = gf.createLineString(c3);

	sLogger.info("Point WKT: " + wkt2.write(ls));
	sLogger.info("Point WKT3: " + wkt3.write(ls));

	MultiLineString mls = gf.createMultiLineString(new LineString[] { ls,
			ls });

	sLogger.info("Point WKT: " + wkt2.write(mls));
	sLogger.info("Point WKT3: " + wkt3.write(mls));

	Coordinate c4 = new Coordinate(6234234.233442, 8234234.234234,
			9323424.2342342);
	Coordinate[] c5 = new Coordinate[] { c1, c2, c4, c1 };

	Polygon poly = gf.createPolygon(c5);

	sLogger.info("Point WKT: " + wkt2.write(poly));
	sLogger.info("Point WKT3: " + wkt3.write(poly));

	MultiPolygon mpoly = gf
			.createMultiPolygon(new Polygon[] { poly, poly });

	sLogger.info("Point WKT: " + wkt2.write(mpoly));
	sLogger.info("Point WKT3: " + wkt3.write(mpoly));

	GeometryCollection gc = gf.createGeometryCollection(new Geometry[] { p,
			mp, ls, mls, poly, mpoly });

	sLogger.info("Point WKT: " + wkt2.write(gc));
	sLogger.info("Point WKT3: " + wkt3.write(gc));

	return gc;
}