def union(self, other): north = max(self.north, other.north) east = max(self.east, other.east) south = min(self.south, other.south) west = min(self.west, other.west) return LatLngBounds(LatLng(south, west), LatLng(north, east))
def get_closest_pandr(origin, park_and_rides): closest_name = "" closest_distance = 10000000 for pandr_name, pandr_latlng in park_and_rides.items(): distance = LatLng(*pandr_latlng).distance_from(LatLng(*origin)) if distance < closest_distance: closest_name = pandr_name closest_distance = distance logging.info("closest: %s, %s", closest_name, closest_distance) return park_and_rides[closest_name]
def get_score(origin, destination, distance, mode): population_modifier = enviro.get_route_population_density_modifier( LatLng(*origin), LatLng(*destination)) (co2_score, nox_score) = enviro.get_scores(enviro.get_mode(mode), distance, pop=population_modifier) return { "co2_score": co2_score, "nox_score": nox_score, "total_score": co2_score + nox_score }
def get_route_score(): # try: latlng_start = LatLng.from_string(request.args["start"]) latlng_end = LatLng.from_string(request.args["end"]) transport_mode = enviro.get_mode(request.args["mode"]) distance = float(request.args["distance"]) # except: # abort(400) population_modifier = enviro.get_route_population_density_modifier(latlng_start, latlng_end) (co2_score, nox_score) = enviro.get_scores(transport_mode, distance, pop=population_modifier) return jsonify({ "co2_score": co2_score, "nox_score": nox_score, "total_score": co2_score+nox_score })
def from_coords(coords): '''Creates a polyline from a list/tuple of cartesian coordinates. :param coords: List of coordinates, where each coordinate is a tuple/list with x/y coordinates (longitude, latitude) :type coords: list :returns: Polyline constructed from supplied coordinates. :rtype: Polyline ''' try: return Polyline(LatLng(p[1], p[0]) for p in coords) except: raise Exception( "Unable to create a Polyline from given coordinates")
def clean_points(points): '''Generator: turns a list of points into LatLng objects. The list can contain either tuples/lists or LatLng objects. :param points: List of points :type points: list ''' prev = None for point in points: if point == prev: continue yield point if point.__class__ == LatLng else LatLng(*point) prev = point
} return DensityData(**data) def __str__(self, sep="\t"): return sep.join([ self.lsoa, self.pop, self.area, self.density, str(self.latlng.lat), str(self.latlng.lng) ]) def distance_from(self, other): return self.latlng.distance_from(other) with open("population.tsv") as f: local_data = [DensityData.from_line(l) for l in f.readlines()] def find_nearest(to_find, data=None): data = data or local_data sorter = lambda d: d.distance_from(to_find) return sorted(data, key=sorter)[0] if __name__ == "__main__": to_find = LatLng(float(sys.argv[1]), float(sys.argv[2])) print(find_nearest(to_find, local_data).density)
def buffer(self, value): sw = LatLng(self.south, self.west).buffer(value) ne = LatLng(self.north, self.east).buffer(value) sw.north = ne.north sw.east = ne.east return sw
def center(self): lat = self.south + (self.north - self.south) / 2 lng = self.east + (self.west - self.east) / 2 return LatLng(lat, lng)
def south_east(self): return LatLng(self.south, self.east)
def north_west(self): return LatLng(self.north, self.west)
def south_west(self): return LatLng(self.south, self.west)
def north_east(self): return LatLng(self.north, self.east)
def from_line(cls, line, sep="\t"): parts = line.strip().split(sep) latlng = LatLng(float(parts[5]), float(parts[4])) pop = int_from_str_with_thousep(parts[1]) density = int_from_str_with_thousep(parts[3]) return cls(parts[0], pop, parts[2], density, latlng)
def from_linestring(linestring): return Polyline([LatLng(p[1], p[0]) for p in linestring])
def get_route_population_density_modifier(start, end): midpoint = LatLng.from_midpoint(start, end) population_density = find_nearest(midpoint).density return population_density_to_modifier(population_density)
nox_score = 1 / (nox_per_km * distance_in_km * mods.NOX * mods.POP * mods.AQI) return (co2_score, nox_score) def get_combined_score(data, distance_in_km, **kwargs): (co2_score, nox_score) = get_scores(data, distance_in_km, **kwargs) return co2_score + nox_score if __name__ == "__main__": args = docopt.docopt(__doc__) mode = args["<mode>"].upper() mode = get_mode(mode) start = LatLng.from_string(args["<start_latlng>"]) end = LatLng.from_string(args["<end_latlng>"]) distance_in_km = start.distance_from(end) population_density_mod = get_route_population_density_modifier(start, end) print("Baseline scores:") print(get_scores(mode, distance_in_km)) print(get_combined_score(mode, distance_in_km)) print("With population data:") print(get_scores(mode, distance_in_km, pop=population_density_mod)) print(get_combined_score(mode, distance_in_km, pop=population_density_mod))