Exemplo n.º 1
0
def lines_to_shp(links,
                 gis_path,
                 group_id='trip_id',
                 color_id=None,
                 colors=['green'],
                 width=1,
                 epsg=None,
                 projection_file=None):
    if epsg is None and projection_file is None:
        print('No projection defined --> considered as EPSG:4326')
        projection_file = epsg4326

    if False:
        to_concat = []
        for line in set(links[group_id]):
            sample = links[links[group_id] == line]
            color = sample[color_id].iloc[0] if color_id else None
            l = list(sample['geometry'])

            to_concat.append((line, color, ops.linemerge(l)))

        df = pd.DataFrame(to_concat, columns=[group_id, 'color', 'geometry'])

    df = build_lines(links, [color_id] if color_id else None)

    if not color_id:
        df['color'] = pd.Series(colors * 10000)

    df['width'] = width
    pandasshp.write_shp(gis_path + 'lines.shp',
                        df,
                        epsg=epsg,
                        projection_file=projection_file,
                        style_file=bordered_line_style)
Exemplo n.º 2
0
def shares_to_shp(aggregated_shares,
                  zones,
                  gis_path,
                  epsg=None,
                  projection_file=None):
    if epsg is None and projection_file is None:
        print('No projection defined --> considered as EPSG:4326')
        projection_file = epsg4326

    as_attraction = aggregated_shares.copy()
    as_attraction['color'] = visual.color_series(
        as_attraction['pt_share_attraction'],
        colors=RdYlGn,
        method='linear',
        reversed_colors=False)
    as_emission = aggregated_shares.copy()
    as_emission['color'] = visual.color_series(
        as_emission['pt_share_emission'],
        colors=RdYlGn,
        method='linear',
        reversed_colors=False)

    pandasshp.write_shp(gis_path + 'share_attraction.shp',
                        as_attraction,
                        epsg=epsg,
                        projection_file=projection_file,
                        style_file=polygon_style)

    pandasshp.write_shp(gis_path + 'share_emission.shp',
                        as_emission,
                        epsg=epsg,
                        projection_file=projection_file,
                        style_file=polygon_style)
Exemplo n.º 3
0
def assigned_links_nodes_to_shp(links,
                                nodes,
                                gis_path,
                                epsg=None,
                                projection_file=None,
                                link_name='loaded_links.shp',
                                node_name='loaded_nodes.shp'):
    if epsg is None and projection_file is None:
        print('No projection defined --> considered as EPSG:4326')
        projection_file = epsg4326

    links = links.copy()
    nodes = nodes.copy()

    try:
        links['color'] = links['line_color']
    except KeyError:
        links['color'] = links['line_color'] = 'gray'

    links['width'] = 3
    nodes['color'] = 'gray'
    nodes['width'] = 3

    pandasshp.write_shp(gis_path + 'nodes_linedraft.shp',
                        nodes,
                        epsg=epsg,
                        projection_file=projection_file,
                        style_file=point_style)

    pandasshp.write_shp(gis_path + 'links_linedraft.shp',
                        links,
                        epsg=epsg,
                        projection_file=projection_file,
                        style_file=bordered_line_style)

    links['width'] = visual.width_series(links['load'], outer_average_width=20)
    links['color'] = links['line_color']
    pandasshp.write_shp(gis_path + 'loaded_links_linedraft_color.shp',
                        links,
                        epsg=epsg,
                        style_file=line_style,
                        projection_file=projection_file)

    links['color'] = visual.color_series(links['load'].fillna(0))
    pandasshp.write_shp(gis_path + link_name,
                        links,
                        style_file=line_style,
                        epsg=epsg,
                        projection_file=projection_file)

    nodes['width'] = visual.width_series(nodes['load'], outer_average_width=10)
    nodes['color'] = visual.color_series(nodes['load'].fillna(0))
    pandasshp.write_shp(gis_path + node_name,
                        nodes,
                        style_file=point_style,
                        epsg=epsg,
                        projection_file=projection_file)
Exemplo n.º 4
0
 def export(self,
            volume_column,
            file=None,
            affected=False,
            outer_average_width=15,
            max_value=None,
            projection_string=None,
            style_file=line_style,
            epsg=None,
            color=None):
     if projection_string == None and epsg == None:
         print('No projection defined --> considered as EPSG:4326')
         projection_string = epsg4326_string
     if affected:
         volume_column += '_transit'
     else:
         if not self.od_geometry:
             print(
                 'can only map affected volumes with arg : od_geometry=False'
             )
     to_shape = self.volume.sort_values(by=volume_column).copy()
     to_shape = to_shape[to_shape[volume_column] > 0]
     to_shape['label'] = to_shape[volume_column]
     if color is not None:
         to_shape['color'] = color
     else:
         to_shape['color'] = data_visualization.color_series(
             to_shape[volume_column], max_value=max_value)
     to_shape['width'] = data_visualization.width_series(
         to_shape[volume_column],
         outer_average_width=outer_average_width,
         max_value=max_value)
     if not file:
         return to_shape
     else:
         extension = file.split('.')[-1]
         if extension == 'shp':
             pandasshp.write_shp(file,
                                 to_shape,
                                 projection_string=projection_string,
                                 style_file=style_file,
                                 epsg=epsg)
         elif extension == 'geojson':
             gpd.GeoDataFrame(to_shape).to_file(file, driver='GeoJSON')
             if epsg:
                 with open(file, 'r') as infile:
                     data = json.load(infile)
                     infile.close()
                 with open(file, 'w') as outfile:
                     data['crs'] = {
                         "type": "name",
                         "properties": {
                             "name": "urn:ogc:def:crs:EPSG::{}".format(epsg)
                         }
                     }
                     json.dump(data, outfile)
Exemplo n.º 5
0
def loaded_links_to_shp(
    loaded_links,
    gis_path,
    load_column='load',
    max_value=None,
    outer_average_width=20,
    color=True,
    epsg=None,
    projection_file=None,
    name='loaded_links.shp',
    create_legend=False,
    *args,
    **kwargs
):
    if epsg is None and projection_file is None:
        print('No projection defined --> considered as EPSG:4326')
        projection_file = epsg4326

    loaded_links = loaded_links.copy()

    # Add colors
    if color:
        loaded_links['color'] = visual.color_series(
            loaded_links[load_column].fillna(0),
            max_value=max_value
        )

    # Add width
    loaded_links['width'] = visual.width_series(
        loaded_links[load_column].fillna(0),
        max_value=max_value,
        outer_average_width=outer_average_width
    )

    # Export
    pandasshp.write_shp(
        gis_path + name,
        loaded_links,
        style_file=line_style,
        epsg=epsg,
        projection_file=projection_file
    )

    # Legend
    if create_legend==True:
        create_load_legend(
            legend_file_path=gis_path + name.split('.shp')[0] + '_legend.shp',
            outer_average_width=outer_average_width,
            epsg=epsg,
            projection_file=projection_file,
            max_value=max_value,
            legend_type='LineString',
            *args,
            **kwargs
            )
Exemplo n.º 6
0
def write_shp_by_folder(layers, shapefile_folder, **kwargs) :
            
        for folder in tqdm(set(layers['folder'])):
            
            try:
                layer = layers.loc[layers['folder'] == folder]
                pandasshp.write_shp(
                    shapefile_folder +'//'+ folder + '.shp',
                    layer, 
                    **kwargs
                )
            except KeyError:
                print(folder)
Exemplo n.º 7
0
    def build_net_from_links_shape(
        self, links, output_network, first_node=0, length=False, debug=False,
                                   add_symmetric=False, write_shp=False, shp_kwargs={}):

        name = output_network.replace('.net', '').replace('.NET', '')
        links_to_shp = name + '_links.shp'
        nodes_to_shp = name + '_nodes.shp'

        links['coordinates_a'] = links['geometry'].apply(lambda c: c.coords[-1])
        links['coordinates_b'] = links['geometry'].apply(lambda c: c.coords[0])

        coordinate_list = list(set(list(links['coordinates_a'])).union(list(links['coordinates_b'])))
        coordinate_dict = {first_node + i :coordinate_list[i] for i in range(len(coordinate_list))}

        nodes = pd.DataFrame(pd.Series(coordinate_dict)).reset_index()
        nodes.columns = ['n', 'coordinates']


        links = pd.merge(links, nodes.rename(columns={'coordinates': 'coordinates_a'}), on='coordinates_a', how='left')
        links = pd.merge(links, nodes.rename(columns={'coordinates': 'coordinates_b'}), on='coordinates_b', how='left',
                         suffixes=['_a', '_b'])

        links.drop(['a', 'b', 'A', 'B', 'coordinates_a', 'coordinates_b'], axis=1, errors='ignore', inplace=True)
        links.rename(columns={'n_a': 'a', 'n_b': 'b'}, inplace=True)
        links = links.groupby(['a', 'b'], as_index=False).first()

        links = pandasdbf.convert_stringy_things_to_string(links)

        if length:
            links[length] = links['geometry'].apply(lambda g : g.length)

        if add_symmetric:
            sym = links.copy()
            sym['a'], sym['b'] = links['b'], links['a']
            sym = sym[sym['a'] != sym['b']]
            links =pd.concat([links, sym])

        nodes['geometry'] = nodes['coordinates'].apply(shapely.geometry.point.Point)
        if write_shp:
            pandasshp.write_shp(nodes_to_shp, nodes, **shp_kwargs)
            pandasshp.write_shp(links_to_shp, links, **shp_kwargs)

        links.drop(['geometry'], axis=1, errors='ignore', inplace=True)


        self.build_net(nodes[['n', 'geometry']], links.fillna(0), output_network, debug=debug)
Exemplo n.º 8
0
    def to_shape(self, stop_file=None, link_file=None, all_nodes=True):
        """
        Saves the geometry of the Lin as a .shp that contains the points (stops) and the polylines (links)

        :param node_file: name of the file that contains the points
        :param link_file: name ot the file that contains the polylines
        """
        if bool(link_file):
            pandasshp.write_shp(link_file,
                                self.geo_dataframe(all_nodes=True),
                                projection_string=self.prj)
        if bool(stop_file):
            stops = pd.merge(self.nodes,
                             self.line_count,
                             left_index=True,
                             right_index=True).reset_index()
            pandasshp.write_shp(stop_file, stops, projection_string=self.prj)
Exemplo n.º 9
0
def assigned_nodes_to_shp(
    nodes,
    gis_path,
    load_column='load',
    max_value=None,
    outer_average_width=10,
    method='surface',
    projection_file=None,
    epsg=None,
    color=True,
    nodes_name='loaded_nodes.shp'
):
    if epsg is None and projection_file is None:
        print('No projection defined --> considered as EPSG:4326')
        projection_file = epsg4326

    nodes = nodes.copy()

    # Add width
    nodes['width'] = visual.width_series(
        nodes[load_column],
        max_value=max_value,
        outer_average_width=outer_average_width,
        method=method,
    )

    # Add color
    if color:
        nodes['color'] = visual.color_series(
            nodes[load_column].fillna(0),
            max_value=max_value,
        )

    # Export
    pandasshp.write_shp(
        gis_path + nodes_name,
        nodes,
        style_file=point_style,
        epsg=epsg,
        projection_file=projection_file
    )
Exemplo n.º 10
0
def ntlegs_centroids_to_shp(
    ntlegs,
    centroids,
    gis_path,
    epsg=None,
    projection_file=None,
    weighted=True
):
    if epsg is None and projection_file is None:
        print('No projection defined --> considered as EPSG:4326')
        projection_file = epsg4326

    ntlegs['width'] = 1
    ntlegs['color'] = 'gray'
    pandasshp.write_shp(
        gis_path + 'ntlegs.shp',
        ntlegs,
        epsg=epsg,
        projection_file=projection_file,
        style_file=line_style)

    try:
        centroids['color'] = visual.color_series(
            centroids['emission_rate'].fillna(0))
    except:
        pass
    pandasshp.write_shp(
        gis_path + 'centroids.shp',
        centroids,
        epsg=epsg,
        projection_file=projection_file
    )

    if weighted:
        centroids['width'] = visual.width_series(
            np.sqrt(centroids['weight']), outer_average_width=15)
        pandasshp.write_shp(
            gis_path + 'weighted_centroids.shp',
            centroids,
            style_file=point_style,
            epsg=epsg,
            projection_file=projection_file
        )
Exemplo n.º 11
0
def create_load_legend(legend_file_path, coordinates, legend_type, values, max_value=None,  style_file=line_style,
                       categories=None, outer_average_width=20, colors=True, delta=[1000, 1500], method='linear',
                       epsg=None, projection_file=None):
    """
    Create a georeferenced legend in shp format for loaded links or points.
    Args:
        legend_file_path: name and path of the legend shp file
        style_file: path to the style file to use
        coordinates: coordinates of the georeferenced legend
        type: Point or LineString
        values: list of values to include in the legend
        max_value: max value to calibrate the legend
        categories: categories associated with the values
        outer_average_width: width
        delta: legend spacing (one value for Point legend, two values for Linestring)        
    """
    x = coordinates[0]
    y = coordinates[1]

    if legend_type == 'Point':
        delta_y = delta[0] if isinstance(delta, list) else delta
        legend_geometries = [geometry.Point([(x, y)])]
        for i in range(len(values) - 1):
            legend_geometries.append(
                geometry.Point([(x, y + (i + 1) * delta_y)]))
    elif legend_type == 'LineString':
        delta_x = delta[0]
        delta_y = delta[1]
        legend_geometries = [geometry.LineString(
            [(x, y), (x + delta_x, y)])]
        for i in range(len(values) - 1):
            legend_geometries.append(
                geometry.LineString(
                    [
                        (x + (i +1) * delta_x, y ),
                        (x + (i + 2) * delta_x, y )
                    ]
                )
            )
    else:
        raise Exception('Not implemented')

    # Create legend df
    legend_scale = pd.DataFrame({
        'value': values,
        'category': categories if categories else values,
        'geometry': legend_geometries

    }
    )
    if colors:
        legend_scale['color'] = visual.color_series(
            legend_scale['value'], max_value=max_value)

    legend_scale['width'] = visual.width_series(
        legend_scale['value'],
        outer_average_width=outer_average_width,
        max_value=max_value,
        method=method
    )
    legend_scale['label'] = legend_scale['value']

    # Write shp legend file
    pandasshp.write_shp(
        legend_file_path,
        legend_scale,
        style_file=style_file,
        epsg=epsg,
        projection_file=projection_file
        )
Exemplo n.º 12
0
def three_level_aggregation_summary_to_shp(
    micro_zones,
    meso_zones,
    macro_zones,
    micro_meso_cluster_series,
    meso_macro_cluster_series,
    gis_path,
    epsg=None,
    projection_file=None,
):
    if epsg is None and projection_file is None:
        print('No projection defined --> considered as EPSG:4326')
        projection_file = epsg4326

    micro_centroids, meso_centroids, centroid_links = aggregation_summary(
        micro_zones, meso_zones, micro_meso_cluster_series)

    pandasshp.write_shp(
        gis_path + 'micro_centroids.shp',
        micro_centroids,
        epsg=epsg,
        projection_file=projection_file,
        style_file=point_style)
    pandasshp.write_shp(
        gis_path + 'meso_centroids.shp',
        meso_centroids,
        epsg=epsg,
        projection_file=projection_file,
        style_file=point_style)
    pandasshp.write_shp(
        gis_path + 'centroid_links.shp',
        centroid_links,
        epsg=epsg,
        projection_file=projection_file,
        style_file=line_style)

    meso_centroids, macro_centroids, macro_centroid_links = aggregation_summary(
        meso_zones, macro_zones, meso_macro_cluster_series, size=1.5)
    pandasshp.write_shp(
        gis_path + 'meso_centroids.shp',
        meso_centroids,
        epsg=epsg,
        projection_file=projection_file,
        style_file=point_style)
    pandasshp.write_shp(
        gis_path + 'macro_centroids.shp',
        macro_centroids,
        epsg=epsg,
        projection_file=projection_file,
        style_file=point_style)
    pandasshp.write_shp(
        gis_path + 'macro_centroid_links.shp',
        macro_centroid_links,
        epsg=epsg,
        projection_file=projection_file,
        style_file=line_style)

    pandasshp.write_shp(
        gis_path + 'micro_zones.shp',
        micro_zones,
        epsg=epsg,
        projection_file=projection_file,
        style_file=polygon_style)
    pandasshp.write_shp(
        gis_path + 'meso_zones.shp',
        meso_zones,
        epsg=epsg,
        projection_file=projection_file,
        style_file=polygon_style)
    pandasshp.write_shp(
        gis_path + 'macro_zones.shp',
        macro_zones,
        epsg=epsg,
        projection_file=projection_file,
        style_file=polygon_style)
Exemplo n.º 13
0
    def to_net(
        self,
        folder,
        zone_index_to_int=int,
        node_index_to_int=int,
        keep_link_columns=[],
        keep_node_columns=[],
        keep_zone_columns=[],
        ntleg_type=10,
        footpath_type=10,
        separate=True,
        road_network=True,
    ):
        """
        dump all the files that you will need to build a network in the folder
        """
        self.check_link_references()  # no orphan node
        road_links = self.road_links.copy() if road_network else pd.DataFrame()
        road_nodes = self.road_nodes.copy() if road_network else pd.DataFrame()
        pt_links = self.links.copy()
        road_links['type'] = 0
        pt_links['type'] = pt_links['route_type']
        links = pd.concat([road_links, pt_links])

        zones = spatial.add_geometry_coordinates(self.zones,
                                                 columns=['x', 'y'])

        zones[['x', 'y']] = np.round(zones[['x', 'y']], 6)
        nodes = pd.concat([road_nodes, self.nodes.copy()])

        nodes = spatial.add_geometry_coordinates(nodes, columns=['x', 'y'])

        nodes[['x', 'y']] = np.round(nodes[['x', 'y']], 6)

        zones['n'] = zones.index
        nodes['n'] = nodes.index

        zones['n'] = zones['n'].apply(zone_index_to_int)
        nodes['n'] = nodes['n'].apply(node_index_to_int)
        links['a'] = links['a'].apply(node_index_to_int)
        links['b'] = links['b'].apply(node_index_to_int)

        nodes = nodes.drop_duplicates(subset=['n'])

        nodes = nodes[['x', 'y', 'n', 'geometry'] + keep_node_columns]
        zones = zones[['x', 'y', 'n', 'geometry'] + keep_zone_columns]

        ntlegs = self.zone_to_road
        access = ntlegs.loc[ntlegs['direction'] == 'access'].copy()
        eggress = ntlegs.loc[ntlegs['direction'] == 'eggress'].copy()

        access['a'] = access['a'].apply(zone_index_to_int)
        eggress['b'] = eggress['b'].apply(zone_index_to_int)

        access['b'] = access['b'].apply(node_index_to_int)
        eggress['a'] = eggress['a'].apply(node_index_to_int)

        ntlegs = pd.concat([access, eggress])

        # footpaths
        footpaths = self.footpaths.copy()
        footpaths['type'] = footpath_type
        footpaths['a'] = footpaths['a'].apply(node_index_to_int)
        footpaths['b'] = footpaths['b'].apply(node_index_to_int)

        ntlegs['distance'] = np.round(ntlegs['length'])
        links['distance'] = np.round(links['length'])
        footpaths['distance'] = np.round(footpaths['length'])

        ntlegs['type'] = ntleg_type
        ntlegs = ntlegs[['a', 'b', 'distance', 'geometry', 'type'] +
                        keep_link_columns]
        links = links[['a', 'b', 'distance', 'geometry', 'type'] +
                      keep_link_columns]
        footpaths = footpaths[['a', 'b', 'distance', 'geometry', 'type'] +
                              keep_link_columns]

        links.drop_duplicates(subset=['a', 'b', 'type'], inplace=True)

        road_links = links[links['type'] == 0]
        pt_links = links[links['type'] > 0].copy()

        if road_network:
            pandasshp.write_shp(folder + 'road_links.shp',
                                road_links,
                                re_write=True)

        pandasshp.write_shp(folder + 'nodes.shp', nodes, re_write=True)
        pandasshp.write_shp(folder + 'zones.shp', zones, re_write=True)
        pandasshp.write_shp(folder + 'ntlegs.shp', ntlegs, re_write=True)
        pandasshp.write_shp(folder + 'footpaths.shp', footpaths, re_write=True)

        pandasshp.write_shp(folder + 'pt_links.shp', pt_links, re_write=True)
        if separate:
            for pt_type in set(pt_links['type']):
                type_links = pt_links.loc[pt_links['type'] == pt_type].copy()
                pandasshp.write_shp(
                    folder + 'pt_links_route_type_%s.shp' % str(pt_type),
                    type_links,
                    re_write=True)