@Override public void add(GeoSpatialFeature feature) { double addedWeight = feature.getWeight(); double x = 0, y = 0, z = 0, lat = 0, lon = 0; // convert lat/lon to cartesian coordinates lat = Math.toRadians(feature.getLatitude()); lon = Math.toRadians(feature.getLongitude()); x = Math.sin(lat) * Math.cos(lon); y = Math.sin(lat) * Math.sin(lon); z = Math.cos(lat); // Increase the weight of this centroid according to what was added. weight += addedWeight; // revise the centroid cartesian coordinates cx += x * addedWeight; cy += y * addedWeight; cz += z * addedWeight; }
@Override public void remove(GeoSpatialFeature feature) { double removedWeight = feature.getWeight(); double x = 0, y = 0, z = 0, lat = 0, lon = 0; // convert lat/lon to cartesian coordinates lat = Math.toRadians(feature.getLatitude()); lon = Math.toRadians(feature.getLongitude()); x = Math.sin(lat) * Math.cos(lon); y = Math.sin(lat) * Math.sin(lon); z = Math.cos(lat); // decrement the centroid cartesian coordinates cx -= x * removedWeight; cy -= y * removedWeight; cz -= z * removedWeight; // Decrease the weight of this centroid according to what was removed. weight = weight - removedWeight; }
@Override public void remove(GeoSpatialFeature feature) { double removedWeight = feature.getWeight(); double newWeight = weight - removedWeight; if (weight <= 0.0) { System.out.println("Attempt to remove from empty GeoSpatialCentroid"); } else { clat = (clat * weight - feature.getLatitude() * removedWeight) / newWeight; clon = (clon * weight - feature.getLongitude() * removedWeight) / newWeight; weight = newWeight; } }
@Override public void add(GeoSpatialFeature feature) { double addedWeight = feature.getWeight(); double newWeight = weight + addedWeight; // incrementally revise the centroid coordinates clat = (clat * weight + feature.getLatitude() * addedWeight) / newWeight; clon = (clon * weight + feature.getLongitude() * addedWeight) / newWeight; weight = newWeight; }