/** * Return the ListEdge in the road network which is closest to the given coordinate, within the * given resolution * * @param c * @param resolution * @return */ public ListEdge getClosestEdge(Coordinate c, double resolution) { // find the set of all edges within *resolution* of the given point Bag objects = networkEdgeLayer.getObjectsWithinDistance(fa.createPoint(c), resolution); if (objects == null || networkEdgeLayer.getGeometries().size() <= 0) return null; // problem with the network edge layer Point point = fa.createPoint(c); // find the closest edge among the set of edges double bestDist = resolution; ListEdge bestEdge = null; for (Object o : objects) { double dist = ((MasonGeometry) o).getGeometry().distance(point); if (dist < bestDist) { bestDist = dist; bestEdge = (ListEdge) ((AttributeValue) ((MasonGeometry) o).getAttribute("ListEdge")).getValue(); } } // if it exists, return it if (bestEdge != null) return bestEdge; // otherwise return failure else return null; }
private School findAppropriateSchool(int age, Point location) { GeomVectorField zones; if (age < 11) { zones = elementarySchoolZones; } else if (age < 15) { zones = middleSchoolZones; } else { zones = highSchoolZones; } Bag catchment = zones.getContainingObjects(location); if (catchment.numObjs != 1) { System.out.format( "Error: school search (age: %d, location: %s) found %d catchments.\n", age, location, catchment.numObjs); for (int i = 0; i < catchment.numObjs; i++) { System.out.format( " Catchment %d: %s\n", i, ((MasonGeometry) catchment.get(i)).getAttribute("SCHID_3")); } return null; } MasonGeometry mg = (MasonGeometry) catchment.get(0); Integer num = mg.getIntegerAttribute("SCHOOL_NUM"); return schoolMap.get(num); }
private void addAgents() { // Agent a = null; for (int i = 0; i < NUM_AGENTS; i++) { // pick a random political region to plop the agent in Bag allRegions = county.getGeometries(); if (allRegions.isEmpty()) { // Something went wrong. We *should* have regions. throw new RuntimeException("No regions found."); } MasonGeometry region = ((MasonGeometry) allRegions.objs[random.nextInt(allRegions.numObjs)]); // give each agent a random direction to initially move in Agent a = new Agent(random.nextInt(8)); // set each agent in the center of corresponding region a.setLocation(region.getGeometry().getCentroid()); // place the agents in the GeomVectorField agents.addGeometry(new MasonGeometry(a.getGeometry())); // add the new agent the schedule schedule.scheduleRepeating(a); } }
@Override public void start() { super.start(); agents.clear(); // remove any agents from previous runs // add agents to the simulation addAgents(); // ensure both GeomFields Color same area agents.setMBR(county.getMBR()); }
/** * populate network with lines from a GeomVectorField * * @param field containing line segments * <p>Assumes that 'field' contains co-planar linear objects */ public void createFromGeomField(GeomVectorField field) { Bag geometries = field.getGeometries(); for (int i = 0; i < geometries.numObjs; i++) { if (((MasonGeometry) geometries.get(i)).geometry instanceof LineString) { addLineString((MasonGeometry) geometries.get(i)); } } // Abandoned work on rectifying non-planar data. MAC 8/24/10. // field.clear(); // // Collection<LineString> lines = lineMerger.getMergedLineStrings(); // // PrecisionModel pm = new PrecisionModel(PrecisionModel.FLOATING); // // GeometryNoder noder = new GeometryNoder(pm); // // Collection<LineString> more_lines = noder.node(lines); // // // Now add nodes and edges to graph // // for (LineString line : more_lines) // { // addLineString(line); // field.addGeometry(new MasonGeometry(line)); // } }
// reset the agent layer's MBR public void resetLayers() { MBR = roadLayer.getMBR(); // MBR.init(740000, 780000, 2000000, 2040000); // 35 22 MBR.init(740000, 779000, 2009000, 2034000); // 35 22 // MBR.init(745000, 775000, 2015000, 2030000); this.humanLayer.setMBR(MBR); // this.baseLayer.setMBR(MBR); }
public ColorWorld(long seed) { super(seed); // this line allows us to replace the standard MasonGeometry with our // own subclass of MasonGeometry; see CountingGeomWrapper.java for more info. // Note: this line MUST occur prior to ingesting the data URL politicalBoundaries = ColorWorld.class.getResource("data/pol.shp"); Bag empty = new Bag(); try { ShapeFileImporter.read(politicalBoundaries, county, empty, CountingGeomWrapper.class); } catch (Exception ex) { Logger.getLogger(ColorWorld.class.getName()).log(Level.SEVERE, null, ex); } // we use either the ConvexHull or Union to determine if the agents are within // Fairfax county or not county.computeConvexHull(); county.computeUnion(); }
/** * Return the GeoNode in the road network which is closest to the given coordinate * * @param c * @return */ public GeoNode getClosestGeoNode(Coordinate c) { // find the set of all nodes within *resolution* of the given point Bag objects = networkLayer.getObjectsWithinDistance(fa.createPoint(c), resolution); if (objects == null || networkLayer.getGeometries().size() <= 0) return null; // problem with the network layer // among these options, pick the best double bestDist = resolution; // MUST be within resolution to count GeoNode best = null; for (Object o : objects) { double dist = ((GeoNode) o).geometry.getCoordinate().distance(c); if (dist < bestDist) { bestDist = dist; best = ((GeoNode) o); } } // if there is a best option, return that! if (best != null && bestDist == 0) return best; // otherwise, closest GeoNode is associated with the closest Edge, so look for that! ListEdge edge = getClosestEdge(c); // find that edge if (edge == null) { edge = getClosestEdge(c, resolution * 10); if (edge == null) return null; } // of that edge's endpoints, find the closer of the two and return it GeoNode n1 = (GeoNode) edge.getFrom(); GeoNode n2 = (GeoNode) edge.getTo(); if (n1.geometry.getCoordinate().distance(c) <= n2.geometry.getCoordinate().distance(c)) return n1; else return n2; }
public void setupAgents(GeomVectorField populationLayer) { Bag nodeBag = majorRoadNodesLayer.getGeometries(); int numNodes = nodeBag.size(); for (Object o : populationLayer.getGeometries()) { MasonGeometry g = (MasonGeometry) o; Coordinate c = g.geometry.getCoordinate(); for (int i = 0; i < 50; i++) { // GeoNode gn = (GeoNode) nodeBag.get(random.nextInt(numNodes)); // Coordinate myHome = (Coordinate) gn.geometry.getCoordinate().clone(); // double distance = Math.abs(random.nextGaussian()) * 1500; // double degrees = random.nextDouble() * 2 * Math.PI; // double xOffset = distance * Math.cos(degrees) + c.x; double xOffset = random.nextGaussian() * 100 + c.x; // double yOffset = distance * Math.sin(degrees) + c.y; double yOffset = random.nextGaussian() * 100 + c.y; Coordinate myHome = new Coordinate(xOffset, yOffset); Geometry point = fa.createPoint(myHome); if (!landArea.contains(point)) continue; HumanTeleporter hum = new HumanTeleporter("id_" + random.nextLong(), myHome, myHome, this); humans.add(hum); } } }
private void countCatchments(GeomVectorField catchments) { Bag geoms = catchments.getGeometries(); for (int i = 0; i < geoms.numObjs; i++) { MasonGeometry mg = (MasonGeometry) geoms.get(i); Integer num = mg.getIntegerAttribute("SCHOOL_NUM"); if (num != null) { School s = schoolMap.get(num); if (s != null) { s.catchmentCount++; } else { System.out.format("School %s not found.\n", num); } } } }
private void createSchoolsFromData(GeomVectorField schoolField) { Bag geoms = schoolField.getGeometries(); for (int i = 0; i < geoms.numObjs; i++) { MasonGeometry mg = (MasonGeometry) geoms.get(i); Integer num = mg.getIntegerAttribute("SCHOOL_NUM"); String name = mg.getStringAttribute("SCHOOL_NAM"); String type = mg.getStringAttribute("SCHOOL_TYP"); if (num != null) { School s = new School(this, name, type); schoolMap.put(num, s); schools.add(s); } } }
private void createHouseholds() { // create households households.clear(); householdsField.clear(); householdsField.setMBR(highSchoolZones.getMBR()); Envelope bounds = highSchoolZones.getMBR(); // this is needed because Geometry exemplar = ((MasonGeometry) highSchoolZones.getGeometries().get(0)).getGeometry(); for (int i = 0; i < numHouseholds; i++) { Household h = new Household(this); households.add(h); Coordinate coord; do { coord = getRandomCoordinate(bounds); } while (!highSchoolZones.isCovered(coord)); Point pt = GeometryFactory.createPointFromInternalCoord(coord, exemplar); h.location = pt; householdsField.addGeometry(new MasonGeometry(pt)); } }
/** Read in data and set up the simulation */ public void start() { super.start(); try { GeomVectorField populationLayer = new GeomVectorField(grid_width, grid_height); ////////////////////////////////////////////// ///////////// READING IN DATA //////////////// ////////////////////////////////////////////// readInVectorLayer(baseLayer, dirName + "haiti/haiti_meters.shp", "area", new Bag()); readInVectorLayer( populationLayer, dirName + "population/popCentroids_meters.shp", "residential areas", new Bag()); readInVectorLayer(roadLayer, dirName + "roads/roads_meters.shp", "road network", new Bag()); readInVectorLayer( waterwayLayer, dirName + "waterways/rivers_meters.shp", "waterways", new Bag()); readInVectorLayer( medicalLayer, dirName + "healthFacilities/health_meters.shp", "health facilities", new Bag()); ////////////////////////////////////////////// ////////////////// CLEANUP /////////////////// ////////////////////////////////////////////// // standardize the MBRs so that the visualization lines up MBR = roadLayer.getMBR(); // MBR.init(740000, 780000, 2000000, 2040000); // 35 22 MBR.init(740000, 779000, 2009000, 2034000); // 35 22 // MBR.init(750000, 772000, 2009500, 2028500); // 22 18 // MBR.init(756000, 766000, 2015500, 2022500); roadLayer.setMBR(MBR); // baseLayer.setMBR(MBR); this.grid_width = roadLayer.fieldWidth; this.grid_height = roadLayer.fieldHeight; // base layer landArea = (Geometry) ((MasonGeometry) baseLayer.getGeometries().get(0)).geometry.clone(); for (Object o : baseLayer.getGeometries()) { MasonGeometry g = (MasonGeometry) o; landArea = landArea.union(g.geometry); } // clean up the road network System.out.print("Cleaning the road network..."); roads = NetworkUtilities.multipartNetworkCleanup(roadLayer, roadNodes, resolution, fa, random, 0); roadNodes = roads.getAllNodes(); testNetworkForIssues(roads); /* // set up roads as being "open" and assemble the list of potential terminii roadLayer = new GeomVectorField(grid_width, grid_height); for(Object o: roadNodes){ GeoNode n = (GeoNode) o; networkLayer.addGeometry(n); boolean potential_terminus = false; // check all roads out of the nodes for(Object ed: roads.getEdgesOut(n)){ // set it as being (initially, at least) "open" ListEdge edge = (ListEdge) ed; ((MasonGeometry)edge.info).addStringAttribute("open", "OPEN"); networkEdgeLayer.addGeometry( (MasonGeometry) edge.info); roadLayer.addGeometry((MasonGeometry) edge.info); ((MasonGeometry)edge.info).addAttribute("ListEdge", edge); String type = ((MasonGeometry)edge.info).getStringAttribute("highway"); if(type.equals("motorway") || type.equals("primary") || type.equals("trunk")) potential_terminus = true; } // check to see if it's a terminus if(potential_terminus && !MBR.contains(n.geometry.getCoordinate()) && roads.getEdges(n, null).size() == 1){ terminus_points.add(n); } } */ // reset MBRS in case it got messed up during all the manipulation roadLayer.setMBR(MBR); networkLayer.setMBR(MBR); networkEdgeLayer.setMBR(MBR); waterwayLayer.setMBR(MBR); baseLayer.setMBR(MBR); medicalLayer.setMBR(MBR); humanLayer.setMBR(MBR); homesLayer.setMBR(MBR); diseasesLayer.setMBR(MBR); System.out.println("done"); ///////////////////// ///////// Clean up roads for Agents to use /////////// ///////////////////// /* Network majorRoads = extractMajorRoads(); testNetworkForIssues(majorRoads); // assemble list of secondary versus local roads ArrayList <Edge> myEdges = new ArrayList <Edge> (); GeomVectorField secondaryRoadsLayer = new GeomVectorField(grid_width, grid_height); GeomVectorField localRoadsLayer = new GeomVectorField(grid_width, grid_height); for(Object o: majorRoads.allNodes){ majorRoadNodesLayer.addGeometry((GeoNode)o); for(Object e: roads.getEdges(o, null)){ Edge ed = (Edge) e; myEdges.add(ed); String type = ((MasonGeometry)ed.getInfo()).getStringAttribute("highway"); if(type.equals("secondary")) secondaryRoadsLayer.addGeometry((MasonGeometry) ed.getInfo()); else if(type.equals("local")) localRoadsLayer.addGeometry((MasonGeometry) ed.getInfo()); } } */ System.gc(); ////////////////////////////////////////////// ////////////////// AGENTS /////////////////// ////////////////////////////////////////////// // set up the agents in the simulation setupAgents(populationLayer); humanLayer.setMBR(MBR); homesLayer.setMBR(MBR); /* // for each of the Agents, set up relevant, environment-specific information int aindex = 0; for(Human a: humans){ if(a.familiarRoadNetwork == null){ // the Human knows about major roads Network familiar = majorRoads.cloneGraph(); // connect the major network to the Human's location connectToMajorNetwork(a.getNode(), familiar); a.familiarRoadNetwork = familiar; // add local roads into the network for(Object o: humanLayer.getObjectsWithinDistance(a, 50)){ Human b = (Human) o; if(b == a || b.familiarRoadNetwork != null || b.getNode() != a.getNode()) continue; b.familiarRoadNetwork = familiar.cloneGraph(); } } // connect the Human's work into its personal network if(a.getWork() != null) connectToMajorNetwork(getClosestGeoNode(a.getWork()), a.familiarRoadNetwork); // set up its basic paths (fast and quicker and recomputing each time) a.setupPaths(); if(aindex % 100 == 0){ // print report of progress System.out.println("..." + aindex + " of " + humans.size()); } aindex++; } */ // Disease d = new Disease(); Cholera d = new Cholera(); HumanTeleporter h = humans.get(random.nextInt(humans.size())); h.acquireDisease(d); // seed the simulation randomly // seedRandom(System.currentTimeMillis()); // schedule the reporter to run // setupReporter(); } catch (Exception e) { e.printStackTrace(); } }
private void readData() { try { // read the data ShapeFileImporter.read( SickStudentsModel.class.getResource("data/ES_ATTENDANCE_AREAS.shp"), elementarySchoolZones); ShapeFileImporter.read( SickStudentsModel.class.getResource("data/MS_ATTENDANCE_AREAS.shp"), middleSchoolZones); ShapeFileImporter.read( SickStudentsModel.class.getResource("data/HS_ATTENDANCE_AREAS.shp"), highSchoolZones); ShapeFileImporter.read( SickStudentsModel.class.getResource("data/ElementarySchools.shp"), elementarySchools); ShapeFileImporter.read( SickStudentsModel.class.getResource("data/MiddleSchools.shp"), middleSchools); ShapeFileImporter.read( SickStudentsModel.class.getResource("data/HighSchools.shp"), highSchools); // Make all the bounding rectangles match one another Envelope MBR = elementarySchoolZones.getMBR(); MBR.expandToInclude(middleSchoolZones.getMBR()); MBR.expandToInclude(highSchoolZones.getMBR()); MBR.expandToInclude(elementarySchools.getMBR()); MBR.expandToInclude(middleSchools.getMBR()); MBR.expandToInclude(highSchools.getMBR()); elementarySchoolZones.setMBR(MBR); middleSchoolZones.setMBR(MBR); highSchoolZones.setMBR(MBR); elementarySchools.setMBR(MBR); middleSchools.setMBR(MBR); highSchools.setMBR(MBR); } catch (Exception ex) { System.out.println("Error opening shapefile!" + ex); System.exit(-1); } }