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

The following examples show how to use com.vividsolutions.jts.geom.GeometryFactory#createLineString() . 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: OraReaderCreateTest.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testRawLineStringM2() throws Exception {
  final GeometryFactory geometryFactory = new GeometryFactory();
  final OraReader oraReader = new OraReader(geometryFactory);

  // Geometry type is a 3-dimensional measured line.
  final int gType = 3302;
  // The 'line' is starting at ordinate offset 1.
  final int[] elemInfo = new int[] {1, 2, 1};
  final double[] ordinates = new double[] {1, 1, 20, 2, 2, 30};
  // Made 'create' method package private to enable test.
  final Geometry actual = oraReader.read(new OraGeom(gType, 0, elemInfo, ordinates));

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

  assertEquals(expected, actual);
}
 
Example 2
Source File: TestFactory.java    From TomboloDigitalConnector with MIT License 5 votes vote down vote up
/**
 * Make LineString given the coordinates
 * @param coordinates
 * @return
 */
public static Geometry makeLineStringGeometry(Coordinate[] coordinates) {
    GeometryFactory geometryFactory = new GeometryFactory();
    Geometry lineString =  geometryFactory.createLineString(coordinates);
    lineString.setSRID(Subject.SRID);
    return lineString;
}
 
Example 3
Source File: SearchByLocationCommandTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void intersectCountriesOnEquatorWithLayerFilter() throws Exception {
	// prepare command
	SearchByLocationRequest request = new SearchByLocationRequest();
	request.setCrs("EPSG:4326");
	request.setQueryType(SearchByLocationRequest.QUERY_INTERSECTS);
	request.setSearchType(SearchByLocationRequest.SEARCH_ALL_LAYERS);
	request.setLayerIds(new String[] {LAYER_ID});
	request.setFilter(LAYER_ID, "region='Region 1'");

	GeometryFactory factory = new GeometryFactory();
	LineString equator = factory.createLineString(new Coordinate[] {new Coordinate(0, 0),
			new Coordinate(-180, 180)});
	request.setLocation(converter.toDto(equator));

	// execute
	SearchByLocationResponse response = (SearchByLocationResponse) dispatcher.execute(
			SearchByLocationRequest.COMMAND, request, null, "en");

	// test
	Assert.assertFalse(response.isError());
	List<Feature> features = response.getFeatureMap().get(LAYER_ID);
	Assert.assertNotNull(features);
	Assert.assertEquals(2, features.size());
	List<String> actual = new ArrayList<String>();
	for (Feature feature : features) {
		actual.add(feature.getLabel());
	}
	Assert.assertTrue(actual.contains("Country 2"));
	Assert.assertTrue(actual.contains("Country 1"));
}
 
Example 4
Source File: SearchByLocationCommandTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void intersectCountriesOnEquatorWithFilter() throws Exception {
	// prepare command
	SearchByLocationRequest request = new SearchByLocationRequest();
	request.setCrs("EPSG:4326");
	request.setQueryType(SearchByLocationRequest.QUERY_INTERSECTS);
	request.setSearchType(SearchByLocationRequest.SEARCH_ALL_LAYERS);
	request.setLayerIds(new String[] {LAYER_ID});
	//note that setting a global filter on the SearchByLocationRequest will only work if the filter is applicable
	//to all layers! In this test case there is only one layer.
	request.setFilter("region='Region 1'");

	GeometryFactory factory = new GeometryFactory();
	LineString equator = factory.createLineString(new Coordinate[] {new Coordinate(0, 0),
			new Coordinate(-180, 180)});
	request.setLocation(converter.toDto(equator));

	// execute
	SearchByLocationResponse response = (SearchByLocationResponse) dispatcher.execute(
			SearchByLocationRequest.COMMAND, request, null, "en");

	// test
	Assert.assertFalse(response.isError());
	List<Feature> features = response.getFeatureMap().get(LAYER_ID);
	Assert.assertNotNull(features);
	Assert.assertEquals(2, features.size());
	List<String> actual = new ArrayList<String>();
	for (Feature feature : features) {
		actual.add(feature.getLabel());
	}
	Assert.assertTrue(actual.contains("Country 2"));
	Assert.assertTrue(actual.contains("Country 1"));
}
 
Example 5
Source File: SearchByLocationCommandTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void intersectCountriesOnEquator() throws Exception {
	// prepare command
	SearchByLocationRequest request = new SearchByLocationRequest();
	request.setCrs("EPSG:4326");
	request.setQueryType(SearchByLocationRequest.QUERY_INTERSECTS);
	request.setSearchType(SearchByLocationRequest.SEARCH_ALL_LAYERS);
	request.setLayerIds(new String[] {LAYER_ID});

	GeometryFactory factory = new GeometryFactory();
	LineString equator = factory.createLineString(new Coordinate[] {new Coordinate(0, 0),
			new Coordinate(-180, 180)});
	request.setLocation(converter.toDto(equator));

	// execute
	SearchByLocationResponse response = (SearchByLocationResponse) dispatcher.execute(
			SearchByLocationRequest.COMMAND, request, null, "en");

	// test
	Assert.assertFalse(response.isError());
	List<Feature> features = response.getFeatureMap().get(LAYER_ID);
	Assert.assertNotNull(features);
	Assert.assertEquals(4, features.size());
	List<String> actual = new ArrayList<String>();
	for (Feature feature : features) {
		actual.add(feature.getLabel());
	}
	Assert.assertTrue(actual.contains("Country 4"));
	Assert.assertTrue(actual.contains("Country 3"));
	Assert.assertTrue(actual.contains("Country 2"));
	Assert.assertTrue(actual.contains("Country 1"));
}
 
Example 6
Source File: VectorLayerServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testGetBoundsIntersectsFiltered() throws Exception {
	GeometryFactory factory = new GeometryFactory();
	LineString equator = factory.createLineString(new Coordinate[] { new Coordinate(0, 0),
			new Coordinate(-180, 180) });
	Filter filter = filterService.createIntersectsFilter(equator,beanLayer.getLayerInfo().getFeatureInfo().getGeometryType().getName());
	Envelope bounds = layerService.getBounds(LAYER_ID,
			geoService.getCrs2(beanLayer.getLayerInfo().getCrs()), filter);
	Assert.assertEquals(0, bounds.getMinX(), ALLOWANCE);
	Assert.assertEquals(0, bounds.getMinY(), ALLOWANCE);
	Assert.assertEquals(1, bounds.getMaxX(), ALLOWANCE);
	Assert.assertEquals(1, bounds.getMaxY(), ALLOWANCE);
}
 
Example 7
Source File: GeoServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testCalcDefaultLabelPosition() throws Exception {
	Geometry geometry;
	GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
	Coordinate coordinate;
	InternalFeature feature = new InternalFeatureImpl();
	feature.setId("x");
	feature.setLabel("Label x");
	coordinate = geoService.calcDefaultLabelPosition(feature);
	Assert.assertNull(coordinate);

	feature.setGeometry(factory.createMultiPolygon(new Polygon[] {}));
	coordinate = geoService.calcDefaultLabelPosition(feature);
	Assert.assertNull(coordinate);

	feature.setGeometry(JTS.toGeometry(new Envelope(10, 20, 30, 40)));
	coordinate = geoService.calcDefaultLabelPosition(feature);
	// this tests current behaviour, without claims that this is the "best" (or even "good") position
	Assert.assertEquals(15.0, coordinate.x, DELTA);
	Assert.assertEquals(35.0, coordinate.y, DELTA);

	geometry = factory.createLineString(new Coordinate[] { new Coordinate(5,4), new Coordinate(30,10) });
	feature.setGeometry(geometry);
	coordinate = geoService.calcDefaultLabelPosition(feature);
	// this tests current behaviour, without claims that this is the "best" (or even "good") position
	Assert.assertEquals(5.0, coordinate.x, DELTA);
	Assert.assertEquals(4.0, coordinate.y, DELTA);
}
 
Example 8
Source File: GeoServiceTransformableAreaTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void transformOutsideAreaTest() throws Exception {
	GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
	Geometry geometry = factory.createLineString(new Coordinate[] { new Coordinate(110, 50),
			new Coordinate(120, 60) });
	geometry = geoService.transform(geometry, LONLAT, LAMBERT72);
	Assert.assertTrue(geometry.isEmpty());
}
 
Example 9
Source File: StyleConverterServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private Geometry toSimpleGeometry(GeometryFactory factory, AbstractGeometryInfo geom) throws LayerException {
	Geometry geometry = null;
	if (geom instanceof PointTypeInfo) {
		PointTypeInfo point = (PointTypeInfo) geom;
		if (point.ifCoord()) {
			geometry = factory.createPoint(getCoordinates(Collections.singletonList(point.getCoord()))[0]);
		} else if (point.ifCoordinates()) {
			geometry = factory.createPoint(getCoordinates(point.getCoordinates())[0]);
		}
	} else if (geom instanceof LineStringTypeInfo) {
		LineStringTypeInfo linestring = (LineStringTypeInfo) geom;
		if (linestring.ifCoordList()) {
			geometry = factory.createLineString(getCoordinates(linestring.getCoordList()));
		} else if (linestring.ifCoordinates()) {
			geometry = factory.createLineString(getCoordinates(linestring.getCoordinates()));
		}
	} else if (geom instanceof PolygonTypeInfo) {
		PolygonTypeInfo polygon = (PolygonTypeInfo) geom;
		OuterBoundaryIsInfo outer = polygon.getOuterBoundaryIs();
		LinearRing shell = toLinearRing(factory, outer.getLinearRing());
		if (polygon.getInnerBoundaryIList() == null) {
			geometry = factory.createPolygon(shell, null);
		} else {
			LinearRing[] holes = new LinearRing[polygon.getInnerBoundaryIList().size()];
			int i = 0;
			for (InnerBoundaryIsInfo inner : polygon.getInnerBoundaryIList()) {
				holes[i++] = toLinearRing(factory, inner.getLinearRing());
			}
			geometry = factory.createPolygon(shell, holes);
		}
	}
	return geometry;
}
 
Example 10
Source File: WKTWriterTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testWrite3D_withNaN() {
  GeometryFactory geometryFactory = new GeometryFactory();
  Coordinate[] coordinates = { new Coordinate(1, 1),
                               new Coordinate(2, 2, 2) };
  LineString line = geometryFactory.createLineString(coordinates);
  String wkt = writer3D.write(line);
  assertEquals("LINESTRING (1 1, 2 2 2)", wkt);
}
 
Example 11
Source File: SegmentStringUtil.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Converts a collection of {@link SegmentString}s into a {@link Geometry}.
 * The geometry will be either a {@link LineString} or a {@link MultiLineString} (possibly empty).
 *
 * @param segStrings a collection of SegmentStrings
 * @return a LineString or MultiLineString
 */
public static Geometry toGeometry(Collection segStrings, GeometryFactory geomFact)
{
  LineString[] lines = new LineString[segStrings.size()];
  int index = 0;
  for (Iterator i = segStrings.iterator(); i.hasNext(); ) {
    SegmentString ss = (SegmentString) i.next();
    LineString line = geomFact.createLineString(ss.getCoordinates());
    lines[index++] = line;
  }
  if (lines.length == 1) return lines[0];
  return geomFact.createMultiLineString(lines);
}
 
Example 12
Source File: CreateRandomShapeFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry randomLineString(Geometry g, int nPts) {
  Envelope env = FunctionsUtil.getEnvelopeOrDefault(g);
  GeometryFactory geomFact = FunctionsUtil.getFactoryOrDefault(g);
  double width = env.getWidth();
  double hgt = env.getHeight();

  Coordinate[] pts = new Coordinate[nPts];

  for (int i = 0; i < nPts; i++) {
    double xLen = width * Math.random();
    double yLen = hgt * Math.random();
    pts[i] = randomPtInRectangleAround(env.centre(), xLen, yLen);
  }
  return geomFact.createLineString(pts);
}
 
Example 13
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 14
Source File: TestFactory.java    From TomboloDigitalConnector with MIT License 5 votes vote down vote up
/**
 * makeLineStringGeometry
 * Returns a lineString at the offset provided
 * @param xOffset
 * @param yOffset
 * @param length length of the line
 * @return A LineString geometry from xOffset, yOffset to xOffset + length, yOffset + length
 */
public static Geometry makeLineStringGeometry(Double xOffset, Double yOffset, Double length) {
    GeometryFactory geometryFactory = new GeometryFactory();
    Coordinate[] coordinates = {new Coordinate(xOffset, yOffset),
            new Coordinate(xOffset + length, yOffset)};
    Geometry lineString =  geometryFactory.createLineString(coordinates);
    lineString.setSRID(Subject.SRID);
    return lineString;
}
 
Example 15
Source File: GeoServiceTransformableAreaTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
private Geometry getLineString() {
	GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
	return factory.createLineString(new Coordinate[] { new Coordinate(5, 4), new Coordinate(30, 10),
			new Coordinate(120, 150), new Coordinate(50, 50) });
}
 
Example 16
Source File: GeoServiceTest.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
private Geometry getLineString() {
	GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
	return factory.createLineString(new Coordinate[] {
			new Coordinate(5, 4), new Coordinate(30, 10), new Coordinate(120, 150), new Coordinate(50, 50)});
}
 
Example 17
Source File: GeoJsonReader.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Geometry createLineString(Map<String, Object> geometryMap,
    GeometryFactory geometryFactory) throws ParseException {

  Geometry result = null;

  try {

    @SuppressWarnings("unchecked")
    List<List<Number>> coordinatesList = (List<List<Number>>) geometryMap
        .get(GeoJsonConstants.NAME_COORDINATES);

    CoordinateSequence coordinates = createCoordinateSequence(coordinatesList);

    result = geometryFactory.createLineString(coordinates);

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

  return result;
}
 
Example 18
Source File: GeoJsonReader.java    From jts with GNU Lesser General Public License v2.1 3 votes vote down vote up
private Geometry createMultiLineString(Map<String, Object> geometryMap,
    GeometryFactory geometryFactory) throws ParseException {

  Geometry result = null;

  try {

    @SuppressWarnings("unchecked")
    List<List<List<Number>>> linesList = (List<List<List<Number>>>) geometryMap
        .get(GeoJsonConstants.NAME_COORDINATES);

    LineString[] lineStrings = new LineString[linesList.size()];

    int i = 0;
    for (List<List<Number>> coordinates : linesList) {

      lineStrings[i] = geometryFactory
          .createLineString(createCoordinateSequence(coordinates));

      ++i;
    }

    result = geometryFactory.createMultiLineString(lineStrings);

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

  return result;
}
 
Example 19
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;
}