Java tutorial
/* MIT License Copyright (c) 2017 Hamdi Kavak Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.hamdikavak.humanmobility.modeling; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.joda.time.Days; import org.joda.time.LocalDateTime; import com.hamdikavak.humanmobility.modeling.exception.TimeUnitNotSupportedException; import com.hamdikavak.humanmobility.modeling.helpers.LocationTraceHelper; import com.hamdikavak.humanmobility.modeling.spatial.GeoCoordinate; import com.hamdikavak.humanmobility.modeling.spatial.LocationTrace; import com.hamdikavak.humanmobility.modeling.spatial.SpatialDistanceUnit; import com.hamdikavak.humanmobility.modeling.spatial.SpatialOperationHandler; /** * A class to calculate radius of gyration for a given set of points or radius * of gyration over time. * * @author Hamdi Kavak * @version 0.3.0 * */ public class RadiusOfGyration { protected final static Logger logger = Logger.getLogger(RadiusOfGyration.class); private SpatialOperationHandler spatialOperation; public RadiusOfGyration() { this.spatialOperation = new SpatialOperationHandler(); } /** * Radius of gyration calculation as in Gonzalez et al. (2008). * * @param traces location traces of an individual * @return calculated radius of gyration in meters. */ public double calculateRadiusOfGyration(List<LocationTrace> traces) { return calculateRadiusOfGyration(traces, SpatialDistanceUnit.Meter); } /** * Radius of gyration calculation for a given spatial distance unit. * * @param traces location traces of an individual * @param unit spatial distance unit * @return calculated radius of gyration */ public double calculateRadiusOfGyration(List<LocationTrace> traces, SpatialDistanceUnit unit) { GeoCoordinate centerOfMass; double totalDeltaD = 0d, radiusOfGyration; if (traces.size() < 2) { radiusOfGyration = 0; } else { centerOfMass = new CenterOfMass().calculate(traces); for (LocationTrace aTrace : traces) { double deltaD = spatialOperation.calculateDistance(aTrace.getCoordinate(), centerOfMass, unit); totalDeltaD += (deltaD * deltaD); } radiusOfGyration = Math.sqrt((1d / (double) traces.size()) * totalDeltaD); } return radiusOfGyration; } /** * Calculates a list of progressing radius of gyration numbers based on time * unit given. Currently, day is supported. * * @param traces * location traces of an individual * @param unit * spatial distance unit * @param timeUnit * time unit for radius of gyration calculation. Day is supported. * @return an array of calculated radius of gyration. * @throws TimeUnitNotSupportedException */ public double[] calculateRadiusOfGyrationOverTime(List<LocationTrace> traces, SpatialDistanceUnit unit, TimeUnit timeUnit) throws TimeUnitNotSupportedException { if (timeUnit != TimeUnit.DAYS) { throw new TimeUnitNotSupportedException(timeUnit + " is not supported. Please pass days as time unit."); } LocationTraceHelper traceHelper = new LocationTraceHelper(); List<LocationTrace> selectedTraces; LocalDateTime firstTraceTime = traces.get(0).getLocalTime().minusMinutes(1); LocalDateTime lastTraceTime = traces.get(traces.size() - 1).getLocalTime(); double[] rogResults; LocalDateTime curentEndDate; int numberOfDays = Days.daysBetween(firstTraceTime, lastTraceTime).getDays(); rogResults = new double[numberOfDays - 1]; for (int i = 1; i < numberOfDays; i++) { curentEndDate = firstTraceTime.plusDays(i).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0); selectedTraces = traceHelper.selectBetweenDates(traces, firstTraceTime, curentEndDate); rogResults[i - 1] = calculateRadiusOfGyration(selectedTraces); } return rogResults; } }