Exemple #1
0
def load_graph(a):
    global G
    if (a==0):
        try:
            G = ox.load_graphml('network.graphml') #load
            print("loaded from graphml file")   
        except: pass
    elif (a==1):
        try:
            G = ox.graph_from_xml('network.osm', bidirectional=False, simplify=True, retain_all=False)
            e = list(G.edges(data=True))
            sub_ed = []
            for el in e:
                if 'highway' in el[2]:
                    if (el[2]['highway'] in ['primary','motorway','trunk','secondary','tertiary','residential','road','unclassified']):
                        sub_ed.append(tuple([el[0],el[1],0]))

            G = nx.edge_subgraph(G,sub_ed)
            print("loaded from osm xml")
        except: pass
    if (G is None):
        try:
            G = ox.graph_from_place('Ижевск, Россия', network_type='drive',simplify=True)
            print("downloaded from internet")
        except: pass

    if (G is None):
        print('failed to load map. To download network from internet check internet connection. To load from file it\'s name should be \'network.graphml\' or \'network.xml\'')
        raise 0
    return
Exemple #2
0
def load_drive_graph(path='db/shenzhen20170701-all.osm', ):
    """
    load original open street map data of a city and extract drivable road network
    :param path: original osm xml format data
    :return: drivable graph
    """
    shenzhen_all = ox.graph_from_xml(path)
    sz_nodes, sz_edges = ox.graph_to_gdfs(shenzhen_all,
                                          fill_edge_geometry=True)

    not_valid_highway = 'cycleway|footway|path|pedestrian|steps|track|corridor|elevator' \
                        '|escalator|proposed|construction|bridleway|abandoned|platform' \
                        '|raceway|service'.split('|')
    not_valid_service = 'parking|parking_aisle|driveway|private|emergency_access'.split(
        '|')
    print('original_edges', sz_edges.shape)

    sz_gdfs = sz_edges.loc[((sz_edges['highway'].notna())
                            & (sz_edges['area'] != 'yes')
                            & (sz_edges['access'] != 'private')
                            & (~sz_edges['service'].isin(not_valid_service)))]

    for tag in not_valid_highway:
        sz_gdfs = sz_gdfs.loc[sz_gdfs['highway'] != tag].copy(deep=True)
    print('drivable_edges:', sz_gdfs.shape)

    shenzhen_drive = ox.graph_from_gdfs(sz_nodes, sz_gdfs)

    return shenzhen_drive
Exemple #3
0
    def _affine_transformation_and_graph(self):
        """Performs initial conversion of the lat lon to cartesian
        """
        # Graph
        read_path = '/'.join([
            self.config['urdf_data_path'],
            self.config['simulation']['map_to_use'], 'map.osm'
        ])
        G = ox.graph_from_xml(read_path, simplify=True, bidirectional='walk')
        self.G = nx.convert_node_labels_to_integers(G)

        # Transformation matrix
        read_path = '/'.join([
            self.config['urdf_data_path'],
            self.config['simulation']['map_to_use'], 'coordinates.csv'
        ])
        points = pd.read_csv(read_path)
        target = points[['x', 'z']].values
        source = points[['lat', 'lon']].values

        # Pad the points with ones
        X = np.hstack((source, np.ones((source.shape[0], 1))))
        Y = np.hstack((target, np.ones((target.shape[0], 1))))
        self.A, res, rank, s = np.linalg.lstsq(X, Y, rcond=None)

        return None
Exemple #4
0
 def __init__(self, config):
     read_path = '/'.join([
         config['urdf_data_path'], config['simulation']['map_to_use'],
         'map.osm'
     ])
     self.G = ox.graph_from_xml(read_path,
                                simplify=True,
                                bidirectional='walk')
     self.A = self.find_homogenous_affine_transformation(config)
     return None
Exemple #5
0
def preprocess():

    # Create the 3D world from the map.osm file
    os.system(
        "java -jar OSM2World.jar --config texture_config.properties -i map.osm -o map.obj"  # noqa
    )

    # Save OSMNX graph
    G = ox.graph_from_xml('map.osm', simplify=True, bidirectional='walk')
    G = nx.convert_node_labels_to_integers(G)

    # Use the blender to bake the texture
    blender_path = "/Applications/blender.app/Contents/MacOS/blender"
    os.system(blender_path + " --background  --python bake_texture.py")

    # Tidy up things and move the files to respective folders
    print('---------------------------------------')
    print('Preprocessing completed successfully')
    print('---------------------------------------')

    name = input("Please provide a name for the new asset: ")

    # Create a directory
    parent_folder = name
    meshes_folder = name + '/meshes'
    Path(parent_folder).mkdir(parents=True, exist_ok=True)
    Path(meshes_folder).mkdir(parents=True, exist_ok=True)

    # Move map.osm and coordinates inside the parent folder
    def move_files(file_name, directory):
        try:
            shutil.copy(file_name, directory + '/' + file_name)
        except FileNotFoundError:
            pass

    files = [
        'map.osm', 'coordinates.csv', 'environment.urdf',
        'environment_collision_free.urdf'
    ]
    for file in files:
        move_files(file, parent_folder)

    files = ['map.obj', 'map.png', 'map.mtl']
    for file in files:
        move_files(file, meshes_folder)

    # Finaly move the whole folder to data folder
    shutil.move(name, '../data/assets/')
Exemple #6
0
def test_osm_xml():
    # test loading a graph from a local .osm xml file
    node_id = 53098262
    neighbor_ids = 53092170, 53060438, 53027353, 667744075

    with bz2.BZ2File("tests/input_data/West-Oakland.osm.bz2") as f:
        handle, temp_filename = tempfile.mkstemp(suffix=".osm")
        os.write(handle, f.read())
        os.close(handle)

    for filename in ("tests/input_data/West-Oakland.osm.bz2", temp_filename):
        G = ox.graph_from_xml(filename)
        assert node_id in G.nodes

        for neighbor_id in neighbor_ids:
            edge_key = (node_id, neighbor_id, 0)
            assert neighbor_id in G.nodes
            assert edge_key in G.edges
            assert G.edges[edge_key]["name"] in ("8th Street", "Willow Street")

    os.remove(temp_filename)

    # test .osm xml saving
    default_all_oneway = ox.settings.all_oneway
    ox.settings.all_oneway = True
    G = ox.graph_from_point(location_point, dist=500, network_type="drive")
    ox.io.save_graph_xml(G, merge_edges=False)

    # test osm xml output merge edges
    ox.io.save_graph_xml(G,
                         merge_edges=True,
                         edge_tag_aggs=[("length", "sum")])

    # test osm xml output from gdfs
    nodes, edges = ox.graph_to_gdfs(G)
    ox.io.save_graph_xml([nodes, edges])

    # test ordered nodes from way
    df = pd.DataFrame({
        "u": [54, 2, 5, 3, 10, 19, 20],
        "v": [76, 3, 8, 10, 5, 20, 15]
    })
    ordered_nodes = ox.osm_xml._get_unique_nodes_ordered_from_way(df)
    assert ordered_nodes == [2, 3, 10, 5, 8]

    ox.settings.all_oneway = default_all_oneway
Exemple #7
0
    def _initial_buildings_setup(self):
        """Perfrom initial building setup.
        """
        read_path = '/'.join([
            self.config['urdf_data_path'],
            self.config['simulation']['map_to_use'], 'buildings.csv'
        ])

        # Check if building information is already generated
        if Path(read_path).is_file():
            buildings = pd.read_csv(read_path)
        else:
            read_path = '/'.join([
                self.config['urdf_data_path'],
                self.config['simulation']['map_to_use'], 'map.osm'
            ])
            G = ox.graph_from_xml(read_path)
            # TODO: This method doesn't work if the building info is not there in OSM
            nodes, streets = ox.graph_to_gdfs(G)

            west, north, east, south = nodes.geometry.total_bounds
            polygon = ox.utils_geo.bbox_to_poly(north, south, east, west)
            gdf = ox.footprints.footprints_from_polygon(polygon)
            buildings_proj = ox.project_gdf(gdf)

            # Save the dataframe representing buildings
            buildings = pd.DataFrame()
            buildings['lon'] = gdf['geometry'].centroid.x
            buildings['lat'] = gdf['geometry'].centroid.y
            buildings['area'] = buildings_proj.area
            buildings['perimeter'] = buildings_proj.length
            try:
                buildings['height'] = buildings_proj['height']
            except KeyError:
                buildings['height'] = 10  # assumption
            buildings['id'] = np.arange(len(buildings_proj))

            # Save the building info
            save_path = read_path = '/'.join([
                self.config['urdf_data_path'],
                self.config['simulation']['map_to_use'], 'buildings.csv'
            ])
            buildings.to_csv(save_path, index=False)

        self.buildings = buildings
        return None
Exemple #8
0
def test_graph_from_xml():
    # test loading a graph from a local .osm xml file
    node_id = 53098262
    neighbor_ids = 53092170, 53060438, 53027353, 667744075

    with bz2.BZ2File("tests/input_data/West-Oakland.osm.bz2") as f:
        handle, temp_filename = tempfile.mkstemp(suffix=".osm")
        os.write(handle, f.read())
        os.close(handle)

    for filename in ("tests/input_data/West-Oakland.osm.bz2", temp_filename):
        G = ox.graph_from_xml(filename)
        assert node_id in G.nodes

        for neighbor_id in neighbor_ids:
            edge_key = (node_id, neighbor_id, 0)
            assert neighbor_id in G.nodes
            assert edge_key in G.edges
            assert G.edges[edge_key]["name"] in ("8th Street", "Willow Street")

    os.remove(temp_filename)
Exemple #9
0
def pedestrians_first(boundaries, 
                      id_code,
                      name,
                      folder_name='', 
                      buffer_dist=100,#m
                      headway_threshold=10,#min
                      to_test = [
                           'healthcare',
                           'schools',
                           'h+s',
                           'libraries',
                           'carfree',
                           'blocks',
                           'density',
                           'transit',
                           'pnb',
                           #'special',
                           ],
                      distances = { #network buffers, in meters
                            'healthcare': 1000,
                            'schools': 1000,
                            'libraries': 1000,
                            'transit': 500,
                            'special': 500,
                            'pnb': 250,
                            },
                      overpass = False,
                      patch_length = 5, #km
                      boundary_buffer = 0, #km
                      blocks_simplification = 15, #m
                      gtfs_files = [],
                      ):    
    dt = datetime.datetime.now()
    logger = logging.getLogger()
    logger.setLevel(logging.CRITICAL)
    formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

    fh = logging.FileHandler('log_filename.txt')
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    
    useful_tags = ox.settings.useful_tags_way + ['cycleway', 'cycleway:left', 'cycleway:right']
    ox.config(use_cache=True, log_console=True, useful_tags_way=useful_tags)
    
    if folder_name != '' and not folder_name[-1:] == '/':
        folder_name += '/'
    
    if boundary_buffer > 0:
        bound_latlon = gpd.GeoDataFrame(geometry = [boundaries])
        bound_latlon.crs = {'init':'epsg:4326'}
        longitude = round(numpy.mean(bound_latlon.geometry.centroid.x),10)
        utm_zone = int(math.floor((longitude + 180) / 6) + 1)
        utm_crs = '+proj=utm +zone={} +ellps=WGS84 +datum=WGS84 +units=m +no_defs'.format(utm_zone)
        bound_utm = bound_latlon.to_crs(utm_crs)
        bound_utm.geometry = bound_utm.geometry.buffer(boundary_buffer*1000)
        bound_latlon = bound_utm.to_crs(epsg=4326)
        boundaries = bound_latlon.geometry.unary_union
    
    bbox = boundaries.bounds
    
    crs = None 
    
    if type(boundaries) == shapely.geometry.multipolygon.MultiPolygon:
        patches = []
        for poly in list(boundaries):
            patches += make_patches(poly, patch_length=patch_length)
    else:
        patches = make_patches(boundaries, patch_length=patch_length)
    
    longitude_factor = 0.00898 # degrees per km
    longitude_factor_m = 0.00898 / 1000 # degrees per m
    latitude_factor = (math.cos(abs(boundaries.bounds[1])*0.0174533))/111.319
    latitude_factor_m = latitude_factor / 1000
    
    print('Evaluating Pedestrians First indicators in',name)
    print('Measuring',str(to_test))
    
    quilt_isochrone_polys = {}
    quilt_center_nodes = {}
    for service in to_test:
        quilt_isochrone_polys[service] = False
        quilt_center_nodes[service] = []
    
    patch_times = []
    
    all_coords={}
    
    testing_services = []
    for service in ['healthcare', 'schools', 'libraries']:
        if service in to_test:
            testing_services.append(service)
            #all_coords[service] = get_point_locations(boundaries, queries[service])
    
    if len(testing_services) > 0:
        handler = get_service_locations.ServiceHandler()
        handler.apply_file(folder_name+'/city.o5m', locations=True)
        for service in testing_services:
            all_coords[service] = handler.locationlist[service]
            citywide_carfree = handler.carfreelist
            
    #should I finish this?
    if 'highways' in to_test:     
        highway_filter = '["area"!~"yes"]["highway"~"motorway|trunk"]'
        G_hwys = ox.graph_from_polygon(boundaries,
                                custom_filter=highway_filter,
                                retain_all=True)
        G_hwys = ox.project_graph(G_hwys)
        edges_hwys = ox.utils_graph.graph_to_gdfs(G_hwys, nodes=False)
        edges_polyline = edges_hwys.geometry.unary_union
        increment_dist = 25
        distances = np.arange(0, edges_polyline.length, increment_dist)
        points = [edges_polyline.interpolate(distance) for distance in distances] + [edges_polyline.boundary[1]]
        multipoint = unary_union(points)
        #need to switch back to latlon and put in all_coords['highway']
        
    if 'special' in to_test:
        if os.path.isfile(folder_name+'/special.shp'):
            testing_services.append('special')
            special = gpd.read_file(folder_name+'/special.shp')
            all_coords['special'] = [(pt.y, pt.x) for pt in special.geometry]

    if 'transit' in to_test:
        testing_services.append('transit')
        #this approach does not let us combine OpenMobilityData files with local files.
        if len(gtfs_files) == 0:
            sources = gtfs_parser.get_feed_infos(gtfs_parser.get_relevant_locs(bbox))
            #note! This doesn't reflect a buffered boundary if boundary_buffer > 0
            transit_stop_sets = gtfs_parser.count_all_sources(sources, 
                                                              source_type = 'openmobilitydata',
                                                              headwaylim = headway_threshold * 2)
        else:
            transit_stop_sets = gtfs_parser.count_all_sources(gtfs_files, 
                                                              source_type = 'local_files',
                                                              headwaylim = headway_threshold * 2)
            
    if 'pnb' in to_test:
        testing_services.append('pnb')
        
        
    if len(to_test) > 0 and to_test != ["blocks"]:
        for p_idx, patch in enumerate(patches):
            try:
                patch_start = datetime.datetime.now()
                
                unbuffered_patch = patch
                
                max_service_dist_km = max(distances.values())/1000
                
                patch = shapely.geometry.box(
                        patch.bounds[0] - (max_service_dist_km * longitude_factor),
                        patch.bounds[1] - (max_service_dist_km * latitude_factor),
                        patch.bounds[2] + (max_service_dist_km * longitude_factor),
                        patch.bounds[3] + (max_service_dist_km * latitude_factor)
                        )
                        
                
                walk_filter = ('["area"!~"yes"]["highway"!~"link|motor'
                               '|proposed|construction|abandoned'
                               '|platform|raceway"]'
                               '["service"!~"parking_aisle|driveway"]'
                               '["foot"!~"no"]["service"!~"private"]'
                               '{}').format(ox.settings.default_access)
                
                if overpass:
                    G = ox.graph_from_polygon(patch, 
                                              custom_filter=walk_filter, 
                                              simplify=False, 
                                              retain_all=True)
                else:
                    boundingarg = '-b='
                    boundingarg += str(patch.bounds[0])+','
                    boundingarg += str(patch.bounds[1])+','
                    boundingarg += str(patch.bounds[2])+','
                    boundingarg += str(patch.bounds[3])
                    subprocess.check_call(['osmconvert',
                                           str(folder_name)+'/citywalk.o5m',
                                           boundingarg,
                                           #'--complete-ways',
                                           '--drop-broken-refs',
                                           '-o=patch.osm'])
                    G = ox.graph_from_xml('patch.osm', 
                                           simplify=False, retain_all=True)
                    os.remove('patch.osm')
                    if 'pnb' in to_test:
                        subprocess.check_call(['osmconvert',
                                           str(folder_name)+'/cityhighways.o5m',
                                           boundingarg,
                                           #'--complete-ways',
                                           '--drop-broken-refs',
                                           '-o=allhwyspatch.osm'])
                        G_allhwys = ox.graph_from_xml('allhwyspatch.osm', 
                                           simplify=False, retain_all=True)
                        os.remove('allhwyspatch.osm')
                        
                
                G.remove_nodes_from(list(nx.isolates(G)))
                G_allhwys.remove_nodes_from(list(nx.isolates(G_allhwys)))
                
                simple_G = ox.simplify_graph(G)
                center_nodes = {}
                for service in all_coords.keys():
                    if service in ['healthcare','schools','libraries','special']:
                        center_nodes[service] = []
                        for coord in all_coords[service]:
                            lat = coord[0]
                            lon = coord[1]
                            if unbuffered_patch.bounds[0] < lon < unbuffered_patch.bounds[2] and unbuffered_patch.bounds[1] < lat < unbuffered_patch.bounds[3]:
                                point = shapely.geometry.Point(lon,lat)
                                nearest = ox.get_nearest_node(simple_G, coord)
                                if not nearest in center_nodes[service]:    
                                    center_nodes[service].append(nearest)
                
                if 'pnb' in to_test:
                    center_nodes['pnb'] = []
                    allhwys_gdf = ox.graph_to_gdfs(G_allhwys, nodes=False)
                    print(allhwys_gdf.columns)
                    waytypes = []
                    if 'highway' in allhwys_gdf.columns:
                        waytypes.append(allhwys_gdf[allhwys_gdf['highway'] == 'cycleway'])
                    if 'cycleway' in allhwys_gdf.columns:
                        waytypes.append(allhwys_gdf[allhwys_gdf['cycleway'] == 'track'])
                    if 'cycleway:left' in allhwys_gdf.columns:
                        waytypes.append(allhwys_gdf[allhwys_gdf['cycleway:left'] == 'track'])
                    if 'cycleway:right' in allhwys_gdf.columns:
                        waytypes.append(allhwys_gdf[allhwys_gdf['cycleway:right'] == 'track'])
                    cycletracknodes = []
                    for waytype in waytypes:
                        for edge in waytype.index:
                            if not edge[0] in cycletracknodes:
                                cycletracknodes.append(edge[0])
                            if not edge[1] in cycletracknodes:
                                cycletracknodes.append(edge[1])
                    for node in cycletracknodes:
                        if node in simple_G.nodes:
                            center_nodes['pnb'].append(node)
                
                if 'transit' in to_test:
                    center_nodes['transit'] = []
                    transit_centers = {}
                    for service_idx, service in enumerate(transit_stop_sets):
                        for stop_id in service.keys():
                            lat = float(service[stop_id][1])
                            lon = float(service[stop_id][2])
                            headway = float(service[stop_id][0])
                            if unbuffered_patch.bounds[0] < lon < unbuffered_patch.bounds[2] and unbuffered_patch.bounds[1] < lat < unbuffered_patch.bounds[3]:
                                center_node = ox.get_nearest_node(simple_G, (lat, lon))
                                if center_node not in transit_centers:
                                    transit_centers[center_node] = {service_idx : headway} #store the headway value
                                elif service_idx not in transit_centers[center_node].keys():
                                    transit_centers[center_node][service_idx] = headway
                                elif headway < transit_centers[center_node][service_idx]:
                                    transit_centers[center_node][service_idx] = headway
                    for center_node in transit_centers.keys():
                        if len(transit_centers[center_node]) > 0:
                            inv_headway = 0
                            min_hw = 100
                            for service_idx in transit_centers[center_node].keys():
                                inv_headway += 1 / transit_centers[center_node][service_idx]
                                if transit_centers[center_node][service_idx] < min_hw:
                                    min_hw = transit_centers[center_node][service_idx]
                            headway = 1 / inv_headway
                            if headway <= headway_threshold:
                                center_nodes['transit'].append(center_node)
                                
                
                
                
                
                # Project Graph
                if not crs: #this only runs once, crs is invariable over patches
                    G = ox.project_graph(G)
                    crs = G.graph['crs'] 
                else:
                    G = ox.project_graph(G, to_crs=crs)
                G = ox.simplify_graph(G)
                
                isochrone_polys = {}
                failures = {}
                for service in to_test:
                    failures[service] = 0
                
                
                # Get polygons
                for service in testing_services:
                    isochrone_polys[service], fails = isochrones.make_iso_polys(G, center_nodes[service], distance=distances[service], edge_buff=buffer_dist)
                    failures[service] += fails
                    
                for service in isochrone_polys.keys():
                    if service not in quilt_isochrone_polys.keys() or not quilt_isochrone_polys[service]:
                        quilt_isochrone_polys[service] = isochrone_polys[service]
                    elif isochrone_polys[service]:
                        quilt_isochrone_polys[service] = shapely.ops.cascaded_union([quilt_isochrone_polys[service],isochrone_polys[service]])
                for service in center_nodes.keys():
                    quilt_center_nodes[service] = quilt_center_nodes[service] + center_nodes[service]
                
                patch_time = datetime.datetime.now() - patch_start
                    
                print("finished patch #",p_idx,'out of', len(patches),"in",str(patch_time))
                patch_times.append(patch_time)
            except ox._errors.EmptyOverpassResponse:
                pass
            except ValueError:
                pass #sorry 
            #except:
            #    print("GOT SOME ERROR FOR PATCH", p_idx)
            # except ValueError:
            #     print('ValueError')
            #     now = str(datetime.datetime.now())
            #     with open('error'+now+'.txt','w') as errout:
            #         traceback.print_exc(limit=3,file=errout)
            #     print('saved to error'+now+'.txt')
    
    #epsg = 32600+int(crs.split(' ')[1].split('=')[1]) #This is wild -- 
    #osmnx seems to just give all data in northern-hemisphere format
    #Sorry about the stupid parsing of the projection definition, I'm lazy 
    
    results = {'name':name,'id_code':id_code}
    
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
     
    boundaries_latlon = gpd.GeoDataFrame(geometry=[boundaries])
    boundaries_latlon.crs = {'init':'epsg:4326'}
    try:
        boundaries_utm = boundaries_latlon.to_crs(crs)
    except ValueError:
        longitude = round(numpy.mean(boundaries_latlon.geometry.centroid.x),10)
        utm_zone = int(math.floor((longitude + 180) / 6) + 1)
        utm_crs = '+proj=utm +zone={} +ellps=WGS84 +datum=WGS84 +units=m +no_defs'.format(utm_zone)
        crs = utm_crs
        boundaries_utm = boundaries_latlon.to_crs(crs)
        
    stats = rasterstats.zonal_stats(boundaries_latlon, 'pop_dens.tif', stats=['sum'])           
    total_pop = stats[0]['sum'] 
    
    for service in testing_services:
        if quilt_isochrone_polys[service]:
            service_utm = gpd.GeoDataFrame(geometry = [quilt_isochrone_polys[service]])
            service_utm.crs = crs#{'init':'epsg:'+str(epsg)}
            service_utm.geometry = service_utm.geometry.simplify(15) #maybe this should be after the population calculation
            service_utm = gpd.overlay(service_utm ,boundaries_utm, how='intersection')
            service_latlon = service_utm.to_crs(epsg=4326)
            service_latlon.to_file(folder_name+service+'latlon'+'.geojson', driver='GeoJSON')
            
            stats = rasterstats.zonal_stats(service_latlon, 'pop_dens.tif', stats=['sum'])           
            total_PNS = stats[0]['sum']
            print("\n")
            print('Total People Near Service for', service, ":", total_PNS)
            print(100*total_PNS/total_pop,"% of",total_pop)
            results[service] = total_PNS / total_pop
        else:
            print ('NO SERVICE FOR', service)
            results[service] = 0
    
    if 'carfree' in to_test:
        print("getting carfree")
        if citywide_carfree:
            carfree_latlon = gpd.GeoDataFrame(geometry = citywide_carfree)
            #just a latlon list of points
            carfree_latlon.crs = {'init':'epsg:4326'}
            carfree_utm = carfree_latlon.to_crs(crs)
            carfree_utm.geometry = carfree_utm.geometry.buffer(100)
            #this is the analysis, the 100m buffer
            carfree_utm = gpd.GeoDataFrame(geometry = [shapely.ops.cascaded_union(carfree_utm.geometry)])
            carfree_utm.geometry = carfree_utm.geometry.simplify(10)
            carfree_utm = gpd.overlay(carfree_utm ,boundaries_utm, how='intersection')
            carfree_utm.crs = crs
            carfree_latlon = carfree_utm.to_crs('epsg:4326')
            
            stats = rasterstats.zonal_stats(carfree_latlon, 'pop_dens.tif', stats=['sum'])
            total_carfree = stats[0]['sum']
            print("\n")
            print('Total People Near Service for carfree', ":", total_carfree)
            print(100*total_carfree/total_pop,"% of",total_pop)
            results['carfree'] = total_carfree / total_pop
            
            carfree_latlon = carfree_utm.to_crs('epsg:4326')
            carfree_latlon.to_file(folder_name+'carfreelatlon'+'.geojson', driver='GeoJSON')
        else:
            print ('NO SERVICE FOR carfree')
            results['carfree'] = 0
            
    
    if 'h+s' in to_test:
        if quilt_isochrone_polys['healthcare'] and quilt_isochrone_polys['schools']:
            service = 'h+s'
            intersect = quilt_isochrone_polys['healthcare'].intersection(quilt_isochrone_polys['schools'])
            if type(intersect) == shapely.geometry.collection.GeometryCollection:
                intersect = [obj for obj in intersect if type(obj) == shapely.geometry.polygon.Polygon]
                intersect = shapely.geometry.MultiPolygon(intersect)
            hs_utm = gpd.GeoDataFrame(geometry = [intersect])
            hs_utm.crs = crs#{'init':'epsg:'+str(epsg)} #maybe this should be after the population calculation
            if hs_utm.geometry.area.sum() != 0:
                hs_utm = gpd.overlay(hs_utm ,boundaries_utm, how='intersection')
                hs_utm.geometry = hs_utm.geometry.simplify(15)
                hs_latlon = hs_utm.to_crs(epsg=4326)
                hs_latlon.to_file(folder_name+service+'latlon'+'.geojson', driver='GeoJSON')
                stats = rasterstats.zonal_stats(hs_latlon, 'pop_dens.tif', stats=['sum'])
                
                total_PNS = stats[0]['sum']
                print("\n")
                print('Total People Near Service for', service, ":", total_PNS)
                print(100*total_PNS/total_pop,"% of",total_pop)
                results[service] = total_PNS / total_pop
            else:
                print ('NO SERVICE FOR h+s')
                results['h+s'] = 0
        else:
            print ('NO SERVICE FOR h+s')
            results['h+s'] = 0
    
    if 'density' in to_test:
        density = rasterstats.zonal_stats(boundaries, 
                                'pop_dens.tif', 
                                stats = [],
                                add_stats={'weighted': weighted_pop_density}
                                )[0]['weighted']
        results['density'] = density / 0.0625 #km^2 / pixel
        print('weighted pop density', results['density'])
        with rasterio.open('pop_dens.tif') as dataset:
            out_image, out_transform = rasterio.mask.mask(dataset, [boundaries], crop=True)
            out_meta = dataset.meta
            out_meta.update({"driver": "GTiff",
                 "height": out_image.shape[1],
                 "width": out_image.shape[2],
                 "transform": out_transform})
        with rasterio.open(folder_name+"pop_dens.tif", "w", **out_meta) as dest:
            dest.write(out_image)
            
    #garbage collection
    quilt_isochrone_polys = None
    isochrone_polys = None
    out_image = None
    dest = None
    dataset = None
    a = None
    b = None
    import gc 
    gc.collect()
    
    if 'blocks' in to_test:
        print("getting blocks")
        
        patches = make_patches(boundaries, patch_length=patch_length)
        print ("cut", len(list(patches)),"patches for block size in",name)
        
        outblocks = []
        block_counts = []
        for n, patch in enumerate(patches):
            print("patch"+str(n)+" of "+str(len(patches)) )
            unbuffered_patch = gpd.GeoSeries(patch, crs={'init':'epsg:4326'})
            unbuffered_patch = unbuffered_patch.to_crs(crs)[0]
            
            
            buffer_dist = 1
            
            patch = shapely.geometry.box(
                    patch.bounds[0] - (buffer_dist * longitude_factor),
                    patch.bounds[1] - (buffer_dist * latitude_factor),
                    patch.bounds[2] + (buffer_dist * longitude_factor),
                    patch.bounds[3] + (buffer_dist * latitude_factor)
                    )
            
            if overpass:
                G = ox.graph_from_polygon(patch, custom_filter=walk_filter, simplify=False, retain_all=True)
            else:
                boundingarg = '-b='
                boundingarg += str(patch.bounds[0])+','
                boundingarg += str(patch.bounds[1])+','
                boundingarg += str(patch.bounds[2])+','
                boundingarg += str(patch.bounds[3])
                subprocess.check_call(['osmconvert',
                                       folder_name+'/citywalk.o5m',
                                       boundingarg,
                                       #'--complete-ways',
                                       '--drop-broken-refs',
                                       '-o=patch.osm'])
                try:
                    G = ox.graph_from_xml('patch.osm', simplify=False, retain_all=True)
                except:
                    G = False
            
            if G:
                try:
                    G = ox.project_graph(G, to_crs=crs)
                    G = ox.simplify_graph(G)
                    
                    streets = ox.utils_graph.graph_to_gdfs(G, nodes = False)
                    
                    if not streets.empty:
                        streets = shapely.geometry.MultiLineString(list(streets.geometry))
                        merged = shapely.ops.linemerge(streets)
                        if merged:
                            borders = shapely.ops.unary_union(merged)
                            blocks = list(shapely.ops.polygonize(borders))
                            all_blocks = []
                            selected_areas = []
                            for block in blocks:
                                if 500 < block.area: #< 200000000:
                                    if block.interiors:
                                        block = shapely.geometry.Polygon(block.exterior)
                                    if block.centroid.within(unbuffered_patch):
                                        area = round(block.area, 3)
                                        perim = round(block.length, 3)
                                        lemgth = round((perim * perim) / area, 3)
                                        if blocks_simplification:
                                            block = block.simplify(blocks_simplification)
                                        all_blocks.append((block, area, perim, lemgth))
                                        if (lemgth < 50) and (1000 < area < 1000000):
                                            selected_areas.append(area)
                            outblocks += all_blocks
                            block_counts.append(len(all_blocks))
                        else:
                            block_counts.append(0)
                            print('not merged!')
                    else:
                        block_counts.append(0)
                except:
                    print('Hawassa Error')
                    block_counts.append(0)
            else:
                block_counts.append(0)
        
        #export            
        
        patch_densities = gpd.GeoDataFrame(geometry = list(patches))
        patch_densities['block_count'] = block_counts
        patch_densities.crs = {'init':'epsg:4326'}
        patch_densities_utm = patch_densities.to_crs(crs)
        patch_densities_utm['density'] = patch_densities_utm.block_count / (patch_densities_utm.area /1000000)
        patch_densities_latlon = patch_densities_utm.to_crs(epsg=4326)
        patch_densities_latlon.to_file(folder_name+'patch_densities'+'latlon'+'.geojson', driver='GeoJSON')
        
        a = gpd.GeoDataFrame(geometry=[block[0] for block in outblocks])
        a.crs = crs#{'init':'epsg:'+str(epsg)}
        a['area'] = [block[1] for block in outblocks]
        a['perim'] = [block[2] for block in outblocks]
        a['lemgth'] = [block[3] for block in outblocks]
        a['density'] = [1000000/block[1] for block in outblocks]
        b = a.to_crs(epsg=4326)
        b.to_file(folder_name+'blocks'+'latlon'+'.geojson', driver='GeoJSON')
        #b.to_file(folder_name+'blocks'+'latlon'+'.shp')
        filtered_blocks = []
        for block in outblocks:
            if block[3] < 50:
                if 1000 < block[1] < 1000000:
                    filtered_blocks.append(block)
        c = gpd.GeoDataFrame(geometry=[block[0] for block in outblocks])
        c.crs = crs#{'init':'epsg:'+str(epsg)}
        c['area'] = [block[1] for block in outblocks]
        c['perim'] = [block[2] for block in outblocks]
        c['lemgth'] = [block[3] for block in outblocks]
        try:
            blockmedian = statistics.median([block[1] for block in filtered_blocks])
            print('median block density')
            results['blockmedian_density'] = 1000000 / blockmedian
            print(results['blockmedian_density'])
        except:
            results['blockmedian_density'] = 0
            print('BLOCK MEDIAN COULD NOT BE CALCULATED')
        try:
            blockmean = statistics.mean([block[1] for block in filtered_blocks])
            print('mean block density')
            results['blockmean_density'] = 1000000 / blockmean
            print(results['blockmean_density'])
        except:
            results['blockmean_density'] = 0
            print('BLOCK MEAN COULD NOT BE CALCULATED')
        
    ft = datetime.datetime.now()
    print("total", str(ft-dt))
    results['calctime'] = str(ft-dt)
    results['total_pop'] = total_pop
    with open(folder_name+"results.json","w") as output:
        output.write(json.dumps(results))
    for file in ['city.o5m','cityhighways.o5m','citywalk.o5m']:
        if os.path.exists(folder_name+'/'+file):
            os.remove(folder_name+'/'+file)
    return results
import osmnx as ox

G = ox.graph_from_xml('extract.osm', bidirectional=True)
ox.save_graphml(G, 'out.graphml')
Exemple #11
0
    end = graph.nodes(data=True)[b]

    # compute forward and back azimuths, plus distance
    azf, azb, distance = g.inv(start['x'], start['y'], end['x'], end['y'])

    # return only the distance we are only interested in the distance
    return distance


# LOAD DATA IN XML (.osm) FORMAT AND CREATE GRAPH (from the xml dataset)
# downloaded as .osm file for greater manchester (GM)
# renamed .osm with .xml before loading data
# convert the graph into a DiGraph (to permit duplicate edges - EXPLAIN WHY)
# store the result in the variable 'graph'
# use relative file path to load the data into the graph
graph = DiGraph(graph_from_xml("mcr_osm.xml"))

# print to check that the .xml file loaded correctly (remove this later) - doesnt work
# # print(graph)
plt.plot(graph)
plt.show()

# CALCULATE A SPATIAL INDEX FROM THE DIGRAPH build a spatial index - needed to take locations and find the nearest
# node in the graph (one to define the start point and one to define the end point of the route)
idx = index.Index()
for _id, data in list(graph.nodes(data=True)):
    # convert the id to a number using int(id)
    idx.insert(int(_id), (data['x'], data['y'], data['x'], data['y']))

# CALCULATE NEAREST NODE TO THE START AND END POINT Just assign two random points for purposes of development
# calculate the 'from' and 'to' node as the nearest to the specified coordinates rtree spatial indexes think about