{"id":17234822,"url":"https://github.com/caesar0301/s2g","last_synced_at":"2025-08-11T00:05:01.370Z","repository":{"id":57463385,"uuid":"75212032","full_name":"caesar0301/s2g","owner":"caesar0301","description":"(S)hapefile to(2) (G)raph/network converter in Python","archived":false,"fork":false,"pushed_at":"2018-12-14T17:18:52.000Z","size":87,"stargazers_count":24,"open_issues_count":4,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-11T00:03:39.386Z","etag":null,"topics":["conversion","fiona","graph","network-converter","networkx","python","s2g","shapefile"],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/s2g","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/caesar0301.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2016-11-30T17:47:57.000Z","updated_at":"2024-08-15T17:07:05.000Z","dependencies_parsed_at":"2022-09-13T10:50:29.262Z","dependency_job_id":null,"html_url":"https://github.com/caesar0301/s2g","commit_stats":null,"previous_names":["caesar0301/python-s2g"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/caesar0301/s2g","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caesar0301%2Fs2g","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caesar0301%2Fs2g/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caesar0301%2Fs2g/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caesar0301%2Fs2g/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/caesar0301","download_url":"https://codeload.github.com/caesar0301/s2g/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/caesar0301%2Fs2g/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269809468,"owners_count":24478542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conversion","fiona","graph","network-converter","networkx","python","s2g","shapefile"],"created_at":"2024-10-15T05:30:45.173Z","updated_at":"2025-08-11T00:05:01.339Z","avatar_url":"https://github.com/caesar0301.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# python-s2g\n\n(S)hapefile (2) Graph/network converter in Python\n\n[![Build Status](https://travis-ci.org/caesar0301/s2g.svg?branch=master)](https://travis-ci.org/caesar0301/s2g)\n\nWhen we process GIS data, a non-trivial problem is the conversion from shape lines to graph or network data structure.\nThe latter may benefit from these out-of-box graphical libraries such as [networkx](http://networkx.github.io/)\nand [igraph](http://igraph.org/python/). But the conversion is a headache to components open communities.\nThis mostly urges me to finish this tiny but useful library.\n\n# Install\n\nRequirements: Python 2.7+ or Python 3.3+\n\n```\nsudo apt-get install python python-pip libgeos-dev\n```\n\nInstall `s2g`,\n\n```$xslt\nsudo pip install s2g\n```\n\nExtra utilities to run unittests,\n\n```\nsudo apt-get install python-tk\nsudo pip install matplotlib\n```\n\n# Usage\n\nYou have two alternative ways to construct the graph. One is reading from a raw shapefiles with `LineString` objects.\n(Under the hood, I involve [fiona](https://pypi.python.org/pypi/Fiona/) to read geometries and\n[shapely](https://pypi.python.org/pypi/Shapely) to analyze the data.).\nCurrently, this tool only supports conversion to *undirected graph*.\n\n```python\nfrom s2g import ShapeGraph\nimport networkx as nx\n\nsg = ShapeGraph(shapefile='path/to/roads.shp', to_graph=True)\nassert isinstance(sg.graph, nx.Graph)\n```\n\nThe other way is designed for programmable usage or time-consuming process where intermediate data could be sniffed or\nsaved. Here is an example to read lines with [fiona]:\n\n```python\nfrom s2g import ShapeGraph\nimport fiona\nfrom shapely.geometry import shape, LineString\n\nshp = 'path/to/shapefile.shp'\n\nwith fiona.open(shp) as source:\n    geoms = []\n    for r in source:\n        s = shape(r['geometry'])\n        if isinstance(s, LineString):\n            geoms.append(s)\n\n# create ShapeGraph object from a list of lines\nsg = ShapeGraph(geoms, to_graph=False)\n\n# detect major components\nmc = sg.gen_major_components()\n# major components are mc[2]\n\n# convert the largest component to networkx Graph\ngraph = sg.to_networkx()  # equivalently sg.graph\n```\n\nDive into [source doc](https://github.com/caesar0301/python-s2g/blob/master/s2g/shapegraph.py) to discover other functionalities.\n\n## QA\n\n* Why not NetworkX's `read_shp` function? ([Issue](https://github.com/caesar0301/s2g/issues/4))\n\nI endeavored to avoid reinventing the wheel at the beginning. It has several limitations to meet common road network processing:\n1. It is not able to detect the major components when the shapefile has disconneted parts\n2. It has no buffer mechsnisms to determine the connectivities of line-line, line-point or point-point pairs\n3. It does not support parameter controlled sampling of road lines when we convert geometry lines into edges\n4. It has no pesudo edges to fix the disconnectivity of geometry elements\n\n## References\n\n* [shp2graph](https://cran.r-project.org/web/packages/shp2graph/index.html) in R by Binbin Lu, as well as [his talk](http://web.warwick.ac.uk/statsdept/user2011/TalkSlides/Contributed/17Aug_1600_FocusIV_2-Geospatial_1-Lu.pdf) on useR! 2011\n* [A Tutorial on Topology Correction of Shapefiles Using GRASS](http://xiaming.me/posts/2015/08/29/a-tutorial-on-topology-correction-of-shapefiles-using-grass/)\n* [Why do I make s2g](http://xiaming.me/posts/2016/12/18/process-gis-shapefile-with-graph-tools/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaesar0301%2Fs2g","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaesar0301%2Fs2g","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaesar0301%2Fs2g/lists"}