Search in sources :

Example 36 with Station

use of org.hwyl.sexytopo.model.survey.Station in project sexytopo by richsmith.

the class SurveyJsonTranslater method toLeg.

public static Leg toLeg(Map<String, Station> namesToStations, JSONObject json) throws JSONException {
    float distance = (float) json.getDouble(DISTANCE_TAG);
    float azimuth = (float) json.getDouble(AZIMUTH_TAG);
    float inclination = (float) json.getDouble(INCLINATION_TAG);
    boolean wasShotBackwards = json.getBoolean(WAS_SHOT_BACKWARDS_TAG);
    String destinationName = json.getString(DESTINATION_TAG);
    Leg leg;
    if (destinationName.equals(SexyTopo.BLANK_STATION_NAME)) {
        leg = new Leg(distance, azimuth, inclination, wasShotBackwards);
    } else {
        if (!namesToStations.containsKey(destinationName)) {
            throw new JSONException("Survey file corrupted: station " + destinationName + " missing or out of order");
        }
        List<Leg> promotedFromList = new ArrayList<>();
        try {
            JSONArray array = json.getJSONArray(PROMOTED_FROM_TAG);
            for (JSONObject object : Util.toList(array)) {
                Leg promotedFrom = toLeg(namesToStations, object);
                promotedFromList.add(promotedFrom);
            }
        } catch (Exception ignore) {
        // not ideal but not the end of the world; we'd probably prefer to have our data
        }
        Leg[] promotedFrom = promotedFromList.toArray(new Leg[] {});
        Station destination = namesToStations.get(destinationName);
        leg = new Leg(distance, azimuth, inclination, destination, promotedFrom, wasShotBackwards);
    }
    return leg;
}
Also used : Station(org.hwyl.sexytopo.model.survey.Station) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) Leg(org.hwyl.sexytopo.model.survey.Leg)

Example 37 with Station

use of org.hwyl.sexytopo.model.survey.Station in project sexytopo by richsmith.

the class SurveyJsonTranslater method toSurvey.

public static void toSurvey(Survey survey, JSONObject json) throws JSONException, ParseException {
    errors = false;
    String name = json.getString(SURVEY_NAME_TAG);
    if (!survey.getName().equals(name)) {
        Log.e("This is weird; the survey name in the file is different to the filename. " + "Assuming filename is the correct name.");
    }
    try {
        // have to parse trips before stations etc. so trips can be referenced by them
        if (json.has(TRIP_TAG)) {
            JSONObject tripObject = json.getJSONObject(TRIP_TAG);
            Trip trip = toTrip(tripObject);
            survey.setTrip(trip);
        }
    } catch (JSONException exception) {
        Log.e("Failed to load trip: " + exception);
    // carry on... unfortunate, but not *that* important
    }
    try {
        JSONArray stationsArray = json.getJSONArray(STATIONS_TAG);
        loadSurveyData(survey, stationsArray);
    } catch (JSONException exception) {
        Log.e("Failed to load stations: " + exception);
    }
    try {
        String activeStationName = json.getString(ACTIVE_STATION_TAG);
        Station activeStation = survey.getStationByName(activeStationName);
        survey.setActiveStation(activeStation);
    } catch (Exception ignore) {
    // ah, never mind... not mission-critical
    }
    if (errors) {
        String message = "Partial errors encountered; survey load was incomplete";
        Toast.makeText(SexyTopo.context, message, Toast.LENGTH_SHORT).show();
    }
}
Also used : Station(org.hwyl.sexytopo.model.survey.Station) Trip(org.hwyl.sexytopo.model.survey.Trip) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONException(org.json.JSONException) ParseException(java.text.ParseException)

Example 38 with Station

use of org.hwyl.sexytopo.model.survey.Station in project sexytopo by richsmith.

the class SurveyJsonTranslater method loadSurveyData.

public static void loadSurveyData(Survey survey, JSONArray json) throws JSONException {
    Map<String, Station> namesToStations = new HashMap<>();
    List<JSONObject> stationData = Util.toList(json);
    // first pass: add all the stations in case there's some weird data order
    boolean first = true;
    for (JSONObject stationObject : stationData) {
        Station station;
        try {
            station = toStation(stationObject);
        } catch (Exception exception) {
            Log.e("Error loading a station; skipping. Exception was: " + exception + "; text was: " + stationObject);
            errors = true;
            continue;
        }
        String name = station.getName();
        if (namesToStations.containsKey(station.getName())) {
            Log.e("Found duplicate station " + name + "; skipping");
            errors = true;
            continue;
        }
        namesToStations.put(station.getName(), station);
        if (first) {
            first = false;
            survey.setOrigin(station);
        }
    }
    // second pass: add the legs
    Map<Integer, Leg> indexToLegs = new HashMap<>();
    List<Leg> unindexedLegs = new ArrayList<>();
    List<Station> connectedDestinations = new ArrayList<>();
    for (JSONObject stationObject : stationData) {
        String name = stationObject.getString(STATION_NAME_TAG);
        Station station = namesToStations.get(name);
        JSONArray legArray = stationObject.getJSONArray(ONWARD_LEGS_TAG);
        for (JSONObject legObject : Util.toList(legArray)) {
            Leg leg;
            try {
                leg = toLeg(namesToStations, legObject);
                if (leg.hasDestination()) {
                    if (connectedDestinations.contains(leg.getDestination())) {
                        Log.e("Duplicate connection found for " + leg.getDestination().getName() + "; skipping leg");
                        errors = true;
                        continue;
                    } else {
                        connectedDestinations.add(leg.getDestination());
                    }
                }
                if (leg.hasDestination() && leg.getDestination() == survey.getOrigin()) {
                    survey.setOrigin(station);
                }
            } catch (Exception exception) {
                Log.e("Error loading a leg. Exception was " + exception + "; text was " + legObject);
                errors = true;
                continue;
            }
            if (legObject.has(INDEX_TAG)) {
                int index = legObject.getInt(INDEX_TAG);
                indexToLegs.put(index, leg);
            } else {
                unindexedLegs.add(leg);
            }
            station.addOnwardLeg(leg);
        }
    }
    for (Leg leg : unindexedLegs) {
        survey.addLegRecord(leg);
    }
    TreeSet<Integer> indices = new TreeSet<>(indexToLegs.keySet());
    for (int i : indices) {
        Leg leg = indexToLegs.get(i);
        survey.addLegRecord(leg);
    }
    survey.checkSurveyIntegrity();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) ParseException(java.text.ParseException) Leg(org.hwyl.sexytopo.model.survey.Leg) Station(org.hwyl.sexytopo.model.survey.Station) JSONObject(org.json.JSONObject) TreeSet(java.util.TreeSet)

Example 39 with Station

use of org.hwyl.sexytopo.model.survey.Station in project sexytopo by richsmith.

the class ManualEntry method addStationWithLruds.

public static void addStationWithLruds(final TableActivity tableActivity, final Survey survey) {
    AlertDialog dialog = createDialog(R.layout.leg_edit_dialog_with_lruds, tableActivity, (leg, theDialog) -> {
        Station station = survey.getActiveStation();
        SurveyUpdater.updateWithNewStation(survey, leg);
        Station newActiveStation = survey.getActiveStation();
        survey.setActiveStation(station);
        createLrudIfPresent(survey, station, theDialog, R.id.editDistanceLeft, LRUD.LEFT);
        createLrudIfPresent(survey, station, theDialog, R.id.editDistanceRight, LRUD.RIGHT);
        createLrudIfPresent(survey, station, theDialog, R.id.editDistanceUp, LRUD.UP);
        createLrudIfPresent(survey, station, theDialog, R.id.editDistanceDown, LRUD.DOWN);
        survey.setActiveStation(newActiveStation);
        tableActivity.getSurveyManager().broadcastSurveyUpdated();
        tableActivity.syncTableWithSurvey();
    });
    dialog.setTitle(R.string.manual_add_station_title);
}
Also used : AlertDialog(android.app.AlertDialog) Station(org.hwyl.sexytopo.model.survey.Station)

Example 40 with Station

use of org.hwyl.sexytopo.model.survey.Station in project sexytopo by richsmith.

the class SpaceFlipper method flipVertically.

@SuppressWarnings("ConstantConditions")
public static Space<Coord2D> flipVertically(Space<Coord2D> space) {
    Space<Coord2D> flippedSpace = new Space<>();
    Map<Station, Coord2D> stationMap = space.getStationMap();
    for (Station station : stationMap.keySet()) {
        Coord2D point = stationMap.get(station);
        flippedSpace.addStation(station, point.flipVertically());
    }
    Map<Leg, Line<Coord2D>> legMap = space.getLegMap();
    for (Leg leg : legMap.keySet()) {
        Line<Coord2D> line = legMap.get(leg);
        Coord2D start = line.getStart();
        Coord2D end = line.getEnd();
        Line<Coord2D> flippedLine = new Line<>(start.flipVertically(), end.flipVertically());
        flippedSpace.addLeg(leg, flippedLine);
    }
    return flippedSpace;
}
Also used : Space(org.hwyl.sexytopo.model.graph.Space) Station(org.hwyl.sexytopo.model.survey.Station) Line(org.hwyl.sexytopo.model.graph.Line) Coord2D(org.hwyl.sexytopo.model.graph.Coord2D) Leg(org.hwyl.sexytopo.model.survey.Leg)

Aggregations

Station (org.hwyl.sexytopo.model.survey.Station)85 Leg (org.hwyl.sexytopo.model.survey.Leg)32 Survey (org.hwyl.sexytopo.model.survey.Survey)30 Coord2D (org.hwyl.sexytopo.model.graph.Coord2D)20 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 SuppressLint (android.annotation.SuppressLint)8 AlertDialog (android.app.AlertDialog)8 JSONException (org.json.JSONException)7 Line (org.hwyl.sexytopo.model.graph.Line)6 SurveyConnection (org.hwyl.sexytopo.model.survey.SurveyConnection)6 JSONObject (org.json.JSONObject)6 View (android.view.View)5 Set (java.util.Set)5 Direction (org.hwyl.sexytopo.model.graph.Direction)5 Space (org.hwyl.sexytopo.model.graph.Space)5 JSONArray (org.json.JSONArray)5 Context (android.content.Context)4 HashSet (java.util.HashSet)4