Exemple #1
0
def get_shp_with_offset(
    df,
    volume_column,
    outer_average_width=15,
    max_value=None,
    color=None,
    offset_col=None
):
    to_shape = df.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 offset_col is not None:
        to_shape['offset'] = data_visualization.width_series(
            to_shape[offset_col],
            outer_average_width=outer_average_width,
            max_value=max_value)

    return to_shape
Exemple #2
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)
Exemple #3
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)
Exemple #4
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
            )
Exemple #5
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
        )
Exemple #6
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
    )
Exemple #7
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
        )