/** * Randomly samples a coordinate from within a zone. * * <p>TODO This must still be implemented once we have a shapefile for the transport zones (TZs) * for City of Cape Town. * * @param zone * @return */ private Coord sampleCoord(String zone, String activityType) { MyZone mz = null; if (zone.equals("") || zone.equals(" ")) { /* Do nothing; don't try and get a zone. */ } else { mz = zoneMap.get(zone); if (mz == null) { LOG.error("Cannot find zone " + zone + " in Map."); } } /* Update the map indicating how many of each activity occurs in * each zone. */ if (mz != null) { ObjectAttributes oa = mz.getObjectAttributes(); Object o = oa.getAttribute(mz.getId().toString(), activityType); if (o == null) { oa.putAttribute(mz.getId().toString(), activityType, 1); } else { if (o instanceof Integer) { int oldValue = (int) o; oa.putAttribute(mz.getId().toString(), activityType, oldValue + 1); } else { LOG.error( "The activity count for zone " + mz.getId().toString() + "'s activity type '" + activityType + " should of type Integer, but is " + o.getClass().toString()); } } } double x = 0.0; double y = 0.0; if (mz != null) { Point p = mz.sampleRandomInteriorPoint(); x = p.getX(); y = p.getY(); } else { numberOfUnknownActivityLocations++; } Coord c = CoordUtils.createCoord(x, y); return c; }
private Coord sampleActivityLocation(String type) { Coord c = null; if (!this.propabilityMap.containsKey(type)) { /* First build the cumulative probability map. */ LOG.info("Building a cumulative probability map for activity type '" + type + "'"); /* a. Get the total number of activity observations. */ int total = 0; for (MyZone mz : this.zoneMap.values()) { Object o = mz.getObjectAttributes().getAttribute(mz.getId().toString(), type); if (o != null && o instanceof Integer) { total += (int) o; } } /* b. Calculate the cumulative probability for each zone. */ TreeMap<String, Double> map = new TreeMap<>(); int cumsum = 0; for (String zoneId : this.zoneMap.keySet()) { MyZone zone = this.zoneMap.get(zoneId); Object o = zone.getObjectAttributes().getAttribute(zoneId, type); if (o != null && o instanceof Integer) { cumsum += (int) o; double cumprob = ((double) cumsum) / ((double) total); map.put(zoneId, cumprob); } } this.propabilityMap.put(type, map); } /* Sample from the cumulative probabilities. */ double r = MatsimRandom.getLocalInstance().nextDouble(); String sampledZone = null; TreeMap<String, Double> theZoneMap = this.propabilityMap.get(type); Iterator<String> iterator = theZoneMap.navigableKeySet().iterator(); while (sampledZone == null && iterator.hasNext()) { String zoneId = iterator.next(); double d = theZoneMap.get(zoneId); if (r <= d) { sampledZone = zoneId; } } if (sampledZone == null) { LOG.error("Should never NOT find a sampled zone."); } Point p = this.zoneMap.get(sampledZone).sampleRandomInteriorPoint(); c = CoordUtils.createCoord(p.getX(), p.getY()); return c; }
public SurveyParser(String shapefile, int idField) { this.sc = ScenarioUtils.createScenario(ConfigUtils.createConfig()); this.tripMap = new HashMap<>(); this.zoneMap = new HashMap<>(); this.gridMap = new HashMap<>(); this.propabilityMap = new TreeMap<>(); /* Parse the transport zone shapefile, and add each zone to the Map. */ MyMultiFeatureReader mfr = new MyMultiFeatureReader(); try { mfr.readMultizoneShapefile(shapefile, idField); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not read transport zones from " + shapefile); } for (MyZone zone : mfr.getAllZones()) { zoneMap.put(zone.getId().toString(), zone); } }