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

The following examples show how to use com.vividsolutions.jts.geom.GeometryFactory#buildGeometry() . 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: CreateRandomShapeFunctions.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static Geometry randomRadialPoints(Geometry g, int nPts) {
  Envelope env = FunctionsUtil.getEnvelopeOrDefault(g);
  GeometryFactory geomFact = FunctionsUtil.getFactoryOrDefault(g);
  double xLen = env.getWidth();
  double yLen = env.getHeight();
  double rMax = Math.min(xLen, yLen) / 2.0;
  
  double centreX = env.getMinX() + xLen/2;
  double centreY = env.getMinY() + yLen/2;
  
  List pts = new ArrayList();

  for (int i = 0; i < nPts; i++) {
    double rand = Math.random();
    // use rand^2 to accentuate radial distribution
    double r = rMax * rand * rand;
    // produces even distribution
    //double r = rMax * Math.sqrt(rand);
    double ang = 2 * Math.PI * Math.random();
    double x = centreX + r * Math.cos(ang);
    double y = centreY + r * Math.sin(ang);
    pts.add(geomFact.createPoint(new Coordinate(x, y)));
  }
  return geomFact.buildGeometry(pts);
}
 
Example 2
Source File: CreateRandomShapeFunctions.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static Geometry randomSegments(Geometry g, int nPts) {
  Envelope env = FunctionsUtil.getEnvelopeOrDefault(g);
  GeometryFactory geomFact = FunctionsUtil.getFactoryOrDefault(g);
  double xLen = env.getWidth();
  double yLen = env.getHeight();

  List lines = new ArrayList();

  for (int i = 0; i < nPts; i++) {
    double x0 = env.getMinX() + xLen * Math.random();
    double y0 = env.getMinY() + yLen * Math.random();
    double x1 = env.getMinX() + xLen * Math.random();
    double y1 = env.getMinY() + yLen * Math.random();
    lines.add(geomFact.createLineString(new Coordinate[] {
        new Coordinate(x0, y0), new Coordinate(x1, y1) }));
  }
  return geomFact.buildGeometry(lines);
}
 
Example 3
Source File: CreateRandomShapeFunctions.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static Geometry randomSegmentsInGrid(Geometry g, int nPts) {
  Envelope env = FunctionsUtil.getEnvelopeOrDefault(g);
  GeometryFactory geomFact = FunctionsUtil.getFactoryOrDefault(g);

  int nCell = (int) Math.sqrt(nPts) + 1;

  double xLen = env.getWidth() / nCell;
  double yLen = env.getHeight() / nCell;

  List lines = new ArrayList();

  for (int i = 0; i < nCell; i++) {
    for (int j = 0; j < nCell; j++) {
      double x0 = env.getMinX() + i * xLen + xLen * Math.random();
      double y0 = env.getMinY() + j * yLen + yLen * Math.random();
      double x1 = env.getMinX() + i * xLen + xLen * Math.random();
      double y1 = env.getMinY() + j * yLen + yLen * Math.random();
      lines.add(geomFact.createLineString(new Coordinate[] {
          new Coordinate(x0, y0), new Coordinate(x1, y1) }));
    }
  }
  return geomFact.buildGeometry(lines);
}
 
Example 4
Source File: GeometrySnapRounder.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Geometry toNodedLines(Collection segStrings, GeometryFactory geomFact) {
  List lines = new ArrayList();
  for (Iterator it = segStrings.iterator(); it.hasNext(); ) {
    NodedSegmentString nss = (NodedSegmentString) it.next();
    // skip collapsed lines
    if (nss.size() < 2)
      continue;
    //Coordinate[] pts = getCoords(nss);
    Coordinate[] pts = nss.getNodeList().getSplitCoordinates();
    
    lines.add(geomFact.createLineString(pts));
  }
  return geomFact.buildGeometry(lines);
}
 
Example 5
Source File: CreateRandomShapeFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry randomPointsInTriangle(Geometry g, int nPts) {
  GeometryFactory geomFact = FunctionsUtil.getFactoryOrDefault(g);
  Coordinate[] gpts = g.getCoordinates();
  Coordinate tri0 = gpts[0];
  Coordinate tri1 = gpts[1];
  Coordinate tri2 = gpts[2];
  
  List pts = new ArrayList();

  for (int i = 0; i < nPts; i++) {
    pts.add(geomFact.createPoint(randomPointInTriangle(tri0, tri1, tri2)));
  }
  return geomFact.buildGeometry(pts);
}
 
Example 6
Source File: LineDissolvePerfTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
Geometry extractLines(Collection geoms)
{
  GeometryFactory factory = null;
  List lines = new ArrayList();
  for (Iterator i = geoms.iterator(); i.hasNext(); ) {
    Geometry g = (Geometry) i.next();
    if (factory == null)
        factory = g.getFactory();
    lines.addAll(LinearComponentExtracter.getLines(g));
  }
  return factory.buildGeometry(geoms);
}