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

The following examples show how to use com.vividsolutions.jts.geom.Geometry#distance() . 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: AbstractTopology.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public IAgent getAgentClosestTo(final IScope scope, final IShape source, final IAgentFilter filter) {
	insertAgents(scope, filter);
	if (!isTorus()) { return getSpatialIndex().firstAtDistance(scope, source, 0, filter); }
	IAgent result = null;
	final Geometry g0 = returnToroidalGeom(source.getGeometry());
	final Map<Geometry, IAgent> agents = getTororoidalAgents(source, scope, filter);

	double distMin = Double.MAX_VALUE;
	for (final Geometry g1 : agents.keySet()) {
		final IAgent ag = agents.get(g1);
		if (source.getAgent() != null && ag == source.getAgent()) {
			continue;
		}
		final double dist = g0.distance(g1);
		if (dist < distMin) {
			distMin = dist;
			result = ag;
		}
	}
	return result;
}
 
Example 2
Source File: AbstractTopology.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Collection<IAgent> getNeighborsOf(final IScope scope, final IShape source, final Double distance,
		final IAgentFilter filter) throws GamaRuntimeException {
	insertAgents(scope, filter);

	if (!isTorus()) { return getSpatialIndex().allAtDistance(scope, source, distance, filter); }

	// FOR TORUS ENVIRONMENTS ONLY

	final Geometry g0 = returnToroidalGeom(source.getGeometry());
	try (ICollector<IAgent> agents = Collector.getSet()) {
		final Map<Geometry, IAgent> agentsMap = getTororoidalAgents(source, scope, filter);
		final IAgent sourceAgent = source.getAgent();
		for (final Geometry g1 : agentsMap.keySet()) {
			final IAgent ag = agentsMap.get(g1);
			if (sourceAgent != null && ag == sourceAgent) {
				continue;
			}
			final double dist = g0.distance(g1);
			if (dist <= distance) {
				agents.add(ag);
			}
		}
		return agents.items();
	}

}
 
Example 3
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;
   }
}
 
Example 4
Source File: JTSModel.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public OSMElement queryFromTap(ILatLng latLng, float zoom) {
    double lat = latLng.getLatitude();
    double lng = latLng.getLongitude();
    Coordinate coord = new Coordinate(lng, lat);
    Envelope envelope = createTapEnvelope(coord, lat, lng, zoom);

    List results = spatialIndex.query(envelope);

    int len = results.size();
    if (len == 0 ) {
        return null;
    }
    if (len == 1) {
        return (OSMElement) results.get(0);
    }

    Point clickPoint = geometryFactory.createPoint(coord);
    OSMElement closestElement = null;
    double closestDist = Double.POSITIVE_INFINITY; // should be replaced in first for loop iteration
    for (Object res : results) {
        OSMElement el = (OSMElement) res;
        if (closestElement == null) {
            closestElement = el;
            closestDist = el.getJTSGeom().distance(clickPoint);
            continue;
        }
        Geometry geom = el.getJTSGeom();
        double dist = geom.distance(clickPoint);

        if (dist > closestDist) {
            continue;
        }

        if (dist < closestDist) {
            closestElement = el;
            closestDist = dist;
            continue;
        }

        // If we are here, then the distances are the same,
        // so we prioritize which element is better based on their type.
        closestElement = prioritizeElementByType(closestElement, el);

    }

    Geometry closestElementGeom = closestElement.getJTSGeom();
    if (closestElementGeom != null && closestElementGeom.intersects(geometryFactory.createPoint(coord))) {
        return closestElement;
    }
    return null;
}
 
Example 5
Source File: GeometryItemDistance.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Computes the distance between two {@link Geometry} items,
 * using the {@link Geometry#distance(Geometry)} method.
 * 
 * @param item1 an item which is a Geometry
 * @param item2 an item which is a Geometry
 * @return the distance between the geometries
 * @throws ClassCastException if either item is not a Geometry
 */
public double distance(ItemBoundable item1, ItemBoundable item2) {
  Geometry g1 = (Geometry) item1.getItem();
  Geometry g2 = (Geometry) item2.getItem();
  return g1.distance(g2);    
}