ShpStreetGraph is a Python tool designed to convert shapefiles into graph representations. This tool uses the geopandas
library for handling shapefiles, networkx
for graph representation, and pyyaml
for flexibility in configuration and user interactions. It allows for various graph export formats such as CSV, GraphML, and Pajek.
You can set up this program in two different ways:
- Download the .deb package from the releases section:
wget https://github.com/PabloVrs/ShpStreetGraph/raw/main/shpstreetgraph_1.0.0.deb
- Install the package using dpkg:
sudo dpkg -i shpstreetgraph_1.0.0.deb
- The program will be ready to use.
- Install
stdeb
package:
pip install stdeb
- Download the setup.py and the shpstreetgraph.py files.
wget https://github.com/PabloVrs/ShpStreetGraph/raw/main/setup.py
wget https://github.com/PabloVrs/ShpStreetGraph/raw/main/shpstreetgraph.py
- Package the files.
python3 setup.py --command-packages=stdeb.command bdist_deb
- Install the package using dpkg:
sudo dpkg -i deb_dist/python3-shpstreetgraph_1.0.0-1_all.deb
The main script can be run from the command line. It requires a path to a shapefile and a configuration file.
-s
,--shapefile
: Path to the shapefile (required).-p
,--print_head
: Print the head of the GeoDataFrame and exit.-e
,--EPSG
: Show the CRS (Coordinate Reference System) of your shapefile.-h
,--help
: Show the list of options.
ShpStreetGraph requires a YAML configuration file to specify various parameters for processing the shapefile. Below is an example configuration file (config.yaml
):
street: nodes #nodes or edges
spatial_operations: intersection #intersection or distance
distance_range: 100 #distance used for buffering streets (depends on the EPSG)
output_format_adjlist: True
output_format_graphml: True
output_format_pajek: True
street_representation: id #id or street_name
merge_streets: True #The streets will be merged by the 'street_identifer_field'
street_identifier_field:
- Field1
- Field2
- Field3
EPSG: False #Set EPSG or use False to default
-
street
: Whether streets are represented as nodes or edges. -
spatial_operations
: Type of spatial operation (intersection or distance). -
distance_range
: distance used for buffering streets in spatial operations (depends on the EPSG). -
output_format_adjlist
: Whether to export the adjacency list in CSV format. -
output_format_graphml
: Whether to export the graph in GraphML format. -
output_format_pajek
: Whether to export the graph in Pajek format. -
street_representation
: How streets are represented in the graph (street_name or id). -
merge_streets
: Whether the streets will be unified by identifier. -
street_identifier_field
: List of fields or unique field to identify streets.If street_identifier_field is a list: | If is a unique field: street_identifier_field: | street_identifier_field: Field - Field1 | - Field2 | - Fieldn |
-
EPSG
: The EPSG code for the coordinate reference system to use.
When you are not certain about the street names, you can print the head of the geodataframe:
shpstreetgraph -s path/to/your/shapefile.shp -p
- Create a configuration file
config.yaml
with your desired settings. - Run the script with your shapefile.
shpstreetgraph -s path/to/your/shapefile.shp
- The script will process the shapefile and output the results in the specified formats.
spatial_operations: intersection |
spatial_operations: distance |
---|---|
CSV Output 1 | CSV Output 2 |
id | from | to | id | from | to |
---|---|---|---|---|---|
7 | East Ninth North | North 100 East | 7 | East Ninth North | North 100 East |
8 | East Ninth North | North East First Street | 8 | East Ninth North | North East First Street |
14 | Hillcrest Drive | North 100 East | 14 | Hillcrest Drive | North 100 East |
18 | North 100 East | Covecrest Street | 18 | North 100 East | North East First Street |
19 | North 100 East | Covecrest Street |
Note that using distance
, with the specific radius, the streets "North 100 East" and "North East First Street" intersect!
The example files are available at examples.
This project is licensed under the GNU General Public License version 3 (GPLv3) - see the LICENSE file for details.