Java-City-of-Mississauga-Sewer-Assets / Mississauga Sewer Analysis GH / SewerAnalysis / Sewer.java
Sewer.java
Raw
package SewerAnalysis;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Class Sewer
 */
public class Sewer implements Comparable<Sewer> {

    public long assetId;
    public String assetType, worksYard;
    public Date installDate;
    public short ward;
    public double lon, lat;

    /**
     * Sewer Constructor
     * Construct Sewer objects, each of which contain the following parameters provided in sewers-mississauga.csv;
     *
     * @param assetId, the asset ID
     * @param assetType, the type of asset (e.g. manhole, catch basin)
     * @param installDate, the date of installation
     * @param worksYard, the water treatment yard that the sewer exits to
     * @param ward, the ward that maintains the sewer
     * @param lon, longitude coordinate
     * @param lat, latitude coordinate
     */
    public Sewer(long assetId, String assetType, String installDate, String worksYard, short ward, double lon, double lat) {
        this.assetId = assetId;
        this.assetType = assetType;
        this.worksYard = worksYard;
        SimpleDateFormat sdformat = new SimpleDateFormat("yyyy/MM/dd");
        // convert string installDate to date format
        try {
            this.installDate = sdformat.parse(installDate);
        } catch (ParseException e) {
            this.installDate = null;
        }
        this.ward = ward;
        this.lon = lon;
        this.lat = lat;
    }

    /**
     * Compares two sewers based on installation dates
     *
     * @param o another Sewer object
     * @return 0 if either install dates are null, 1 if this.installDate is greater than o.installDate, else -1
     */
    @Override
    public int compareTo(Sewer o) {
        if (this.installDate == null || o.installDate == null) {
            return 0;
        } else {
            return this.installDate.compareTo(o.installDate);
        }
    }

    /**
     * Calculates the Haversine distance between two sewers
     * Haversine formula: <a href="https://en.wikipedia.org/wiki/Haversine_formula">...</a>
     *
     * @param o another Sewer object
     * @return distance (in metres) over the Earth's surface using the Haversine formula
     */
    public double distanceTo(Sewer o){
        double Earth_radius = 6371.0;

        double lat1 = Math.toRadians(this.lat), lon1 = Math.toRadians(this.lon), lat2 = Math.toRadians(o.lat),
                lon2 = Math.toRadians(o.lon), a = Math.pow(Math.sin((lat2 - lat1) / 2), 2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin((lon2 - lon1) / 2), 2);

        return (Earth_radius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))) * 1000;
    }
}