Java Code Examples for org.locationtech.jts.geom.Coordinate#distance()

The following examples show how to use org.locationtech.jts.geom.Coordinate#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: TPSInterpolator.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public double getValue( Coordinate[] controlPoints, Coordinate interpolated ) {
    int controlPointsNum = controlPoints.length;
    GeneralMatrix v = null;
    try {
        v = makeMatrix(controlPoints);
    } catch (Exception e) {
        return HMConstants.doubleNovalue;
    }
    double a1 = v.getElement(v.getNumRow() - 3, 0);
    double a2 = v.getElement(v.getNumRow() - 2, 0);
    double a3 = v.getElement(v.getNumRow() - 1, 0);

    double sum = 0;
    for( int i = 0; i < controlPointsNum; i++ ) {
        double dist = interpolated.distance(controlPoints[i]);
        sum = sum + (v.getElement(i, 0) * functionU(dist));
    }

    double value = (a1 + (a2 * interpolated.x) + (a3 * interpolated.y) + sum);
    interpolated.z = value;
    return value;
}
 
Example 2
Source File: TestENU.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
public void testWithGeotools() throws MatrixException {
    Coordinate c1 = new Coordinate(11, 46, 0);
    Coordinate c2 = new Coordinate(11.001, 46.001, 0);

    GeodeticCalculator gc = new GeodeticCalculator(DefaultGeographicCRS.WGS84);
    gc.setStartingGeographicPoint(c1.x, c1.y);
    gc.setDestinationGeographicPoint(c2.x, c2.y);
    double orthodromicDistance = gc.getOrthodromicDistance();

    ENU enu = new ENU(c1);
    Coordinate ce1 = enu.wgs84ToEnu(c1);
    Coordinate ce2 = enu.wgs84ToEnu(c2);

    double distance = ce1.distance(ce2);
    assertTrue(isDeltaOk(orthodromicDistance, distance));
    
    Coordinate c1Back = enu.enuToWgs84(ce1);
    Coordinate c2Back = enu.enuToWgs84(ce2);
    
    assertEquals(0, c1.distance(c1Back), 0.000001);
    assertEquals(0, c2.distance(c2Back), 0.000001);
    
}
 
Example 3
Source File: OmsLW04_BankfullWidthAnalyzer.java    From hortonmachine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * check the width of the section against the given thresholds
 * and create the section linestring
 */
private void assign( LinkedHashMap<SimpleFeature, double[]> validPointsMap,
        LinkedHashMap<SimpleFeature, String> problemPointsMap, ConcurrentLinkedQueue<Object[]> validPointsLineList,
        SimpleFeature netFeature, Object linkId, Object pfaf, double progressive, double[] attributes,
        Coordinate nearestOnChannelCoordinate, Coordinate coordinate ) {
    double width = coordinate.distance(nearestOnChannelCoordinate);
    if (width > pMaxNetworkWidth) {
        problemPointsMap.put(netFeature, FOUND_INVALID_NETWORK_WIDTH_LARGE);
    } else if (width < pMinNetworkWidth) {
        problemPointsMap.put(netFeature, FOUND_INVALID_NETWORK_WIDTH_SMALL);
    } else {
        attributes[0] = width;
        attributes[1] = WIDTH_FROM_CHANNELEDIT;

        LineString widthLine = gf.createLineString(new Coordinate[]{coordinate, nearestOnChannelCoordinate});
        Object[] newLineAttr = {widthLine, pfaf, linkId, linkId, progressive};
        validPointsLineList.add(newLineAttr);
        validPointsMap.put(netFeature, attributes);
    }
}
 
Example 4
Source File: IDWInterpolator.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
public double getValue( Coordinate[] controlPoints, Coordinate interpolated ) {
    if (controlPoints.length == 0) {
        return HMConstants.doubleNovalue;
    }

    double sumdValue = 0;
    double sumweight = 0;

    for( Coordinate coordinate : controlPoints ) {
        double distance = coordinate.distance(interpolated);
        /*
         * the index if built on envelope, we need a radius check.
         * If not near, do not consider it.
         */
        if (distance > buffer) {
            continue;
        }
        if (distance < 0.00001) {
            distance = 0.00001;
        }
        double weight = (1 / Math.pow(distance, 2));

        sumdValue = sumdValue + coordinate.z * weight;

        sumweight = sumweight + weight;
    }

    double value = sumdValue / sumweight;
    return value;

}
 
Example 5
Source File: OmsLineSmootherMcMaster.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks if the given geometry is connected to any other line.
 * 
 * @param geometryN the geometry to test.
 * @return true if the geometry is alone in the space, i.e. not connected at
 *              one of the ends to any other geometry.
 */
private boolean isAlone( Geometry geometryN ) {
    Coordinate[] coordinates = geometryN.getCoordinates();
    if (coordinates.length > 1) {
        Coordinate first = coordinates[0];
        Coordinate last = coordinates[coordinates.length - 1];
        for( SimpleFeature line : linesList ) {
            Geometry lineGeom = (Geometry) line.getDefaultGeometry();
            int numGeometries = lineGeom.getNumGeometries();
            for( int i = 0; i < numGeometries; i++ ) {
                Geometry subGeom = lineGeom.getGeometryN(i);
                Coordinate[] lineCoordinates = subGeom.getCoordinates();
                if (lineCoordinates.length < 2) {
                    continue;
                } else {
                    Coordinate tmpFirst = lineCoordinates[0];
                    Coordinate tmpLast = lineCoordinates[lineCoordinates.length - 1];
                    if (tmpFirst.distance(first) < SAMEPOINTTHRESHOLD || tmpFirst.distance(last) < SAMEPOINTTHRESHOLD
                            || tmpLast.distance(first) < SAMEPOINTTHRESHOLD || tmpLast.distance(last) < SAMEPOINTTHRESHOLD) {
                        return false;
                    }
                }
            }
        }
    }
    // 1 point line or no connection, mark it as alone for removal
    return true;
}
 
Example 6
Source File: OmsEpanetFeaturesSynchronizer.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
private SimpleFeature findWithinTolerance( Coordinate c, List<SimpleFeature>... nodesLists ) {
    for( List<SimpleFeature> nodeList : nodesLists ) {
        for( SimpleFeature node : nodeList ) {
            Geometry geometry = (Geometry) node.getDefaultGeometry();
            Coordinate coord = geometry.getCoordinate();
            if (coord.distance(c) <= pTol) {
                return node;
            }
        }
    }
    return null;
}
 
Example 7
Source File: OmsTrentoP.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Find the pipes that are draining in this pipe (defined by the index parameter).
 * 
 * @param index
 *            the ID of this pipe.
 * @param cord
 *            the Coordinate of the link where drain.
 */
private void findIdThatDrainsIntoIndex( int index, Coordinate cord ) {
    int t = 0;
    double toll = 0.1;
    for( int i = 0; i < networkPipes.length; i++ ) {
        // if it is this pipe then go haead.
        if (index == i) {
            continue;
        }
        // there isn-t other pipe that can drain in this.
        else if (t == pMaxJunction) {
            break;
        }
        // the id is already set.
        else if (networkPipes[i].getIdPipeWhereDrain() != null) {
            continue;
        }
        // extract the coordinate of the point of the linee of the new pipe.
        Coordinate[] coords = networkPipes[i].point;
        // if one of the coordinates are near of coord then the 2 pipe are
        // linked.
        int lastIndex = coords.length - 1;
        if (cord.distance(coords[0]) < toll) {
            networkPipes[i].setIdPipeWhereDrain(networkPipes[index].getId());
            networkPipes[i].setIndexPipeWhereDrain(index);
            findIdThatDrainsIntoIndex(i, coords[lastIndex]);
            t++;
        } else if (cord.distance(coords[lastIndex]) < toll) {
            networkPipes[i].setIdPipeWhereDrain(networkPipes[index].getId());
            networkPipes[i].setIndexPipeWhereDrain(index);
            findIdThatDrainsIntoIndex(i, coords[0]);
            t++;
        }

    }

}
 
Example 8
Source File: OmsJami.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param basinBaricenterCoordinate the basin baricenter coordinate used to 
 *                      Calculate distances with stations.
 * @param stationIdsToSearch the list of stations.
 * @param stationId2CoordinateMap
 * @param statValues the array of data to consider. Used to identify stations
 *                      that have no data for an instant.
 * @param idStationIndexMap
 * @return the list of needed nearest stations.
 */
private List<Integer> extractStationsToUse( Coordinate basinBaricenterCoordinate, List<Integer> stationIdsToSearch,
        HashMap<Integer, Coordinate> stationId2CoordinateMap, double[] statValues, HashMap<Integer, Integer> idStationIndexMap ) {

    List<Integer> stationsToUse = new ArrayList<Integer>();
    List<Integer> stationsLeftOver = new ArrayList<Integer>();
    Map<Double, Integer> sortedByDistanceStationsMap = new TreeMap<Double, Integer>();
    for( Integer stId : stationIdsToSearch ) {
        /*
         * check the values of the stations. If there are novalues and there
         * are enough stations left, disable them
         */
        double currentValue = statValues[stationid2StationindexMap.get(stId)];
        if (isNovalue(currentValue)) {
            // if the station has a novalue, jump over it, even without
            // adding the station.
            // out.println("Jump over station: " + stId);
            stationsLeftOver.add(stId);
            continue;
        }

        /*
         * if it gets here, the station has a value. 
         * Put it to the list of stations to be checked 
         * for which is nearer. 
         */
        Coordinate stationCoord = stationId2CoordinateMap.get(stId);
        double distance = basinBaricenterCoordinate.distance(stationCoord);
        sortedByDistanceStationsMap.put(distance, stId);
    }
    Collection<Integer> statIds = sortedByDistanceStationsMap.values();
    Iterator<Integer> iterator = statIds.iterator();
    for( int i = 0; i < statIds.size(); i++ ) {
        if (iterator.hasNext() && i < pNum) {
            stationsToUse.add(iterator.next());
        } else if (iterator.hasNext() && i >= pNum) {
            stationsLeftOver.add(iterator.next());
        } else {
            System.out.println("SHOULD THIS EVER HAPPEN???");
            break;
        }
    }

    /*
     * if not enough stations were collected, add also stations that
     * don't have values. Those stations are taken in random way, 
     * since their value won't be considered.  
     */
    if (stationsToUse.size() < pNum) {
        for( int i = 0; i < pNum - stationsToUse.size(); i++ ) {
            stationsToUse.add(stationsLeftOver.get(i));
        }
    }

    return stationsToUse;
}
 
Example 9
Source File: GpsLogInfoTool.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
public boolean onToolTouchEvent(MotionEvent event) {
    if (mapView == null || mapView.isClickable()) {
        return false;
    }

    // handle drawing
    float currentX = event.getX();
    float currentY = event.getY();
    int deltaPixels = 100;

    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            Coordinate currentGeoPoint = projection.fromPixels(round(currentX), round(currentY));
            Coordinate plusPoint = projection.fromPixels(round(currentX + deltaPixels), round(currentY + deltaPixels));

            double touchLon = currentGeoPoint.x;
            double touchLat = currentGeoPoint.y;
            double lonPlus = plusPoint.x;
            double latPlus = plusPoint.y;
            double deltaX = Math.abs(touchLon - lonPlus);
            double deltaY = Math.abs(touchLat - latPlus);
            Coordinate touchCoord = new Coordinate(touchLon, touchLat);
            Envelope queryEnvelope = new Envelope(touchCoord);
            queryEnvelope.expandBy(deltaX, deltaY);

            List<GpsLogInfo> result = gpsLogInfoTree.query(queryEnvelope);
            if (result.size() == 0) {
                return true;
            } else {
                GpsLogInfo nearest = null;
                double minDist = Double.POSITIVE_INFINITY;
                for (GpsLogInfo info : result) {
                    double dist = touchCoord.distance(info.pointXYZ);
                    if (dist < minDist) {
                        minDist = dist;
                        nearest = info;
                    }
                }
                gpsLogInfo = nearest;
            }
            break;
        case MotionEvent.ACTION_UP:
            gpsLogInfo = null;
            break;
    }
    EditManager.INSTANCE.invalidateEditingView();
    return true;
}
 
Example 10
Source File: TPSInterpolator.java    From hortonmachine with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Calculates U function where distance = ||p<sub>i</sub>, p<sub>j</sub>|| (from source points)
 * 
 * @param p_i p_i
 * @param p_j p_j
 * @return log(distance)*distance<sub>2</sub> or 0 if distance = 0
 */
private double calculateFunctionU( Coordinate p_i, Coordinate p_j ) {
    double distance = p_i.distance(p_j);
    return functionU(distance);
}