Created
October 1, 2012 22:52
-
-
Save aheadley/3814996 to your computer and use it in GitHub Desktop.
Python script to find unconnected regions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import networkx | |
import glob | |
import os.path | |
def get_subgraphs(regionset_path): | |
graph = networkx.Graph() | |
graph.add_nodes_from(get_nodes(regionset_path)) | |
add_node_edges(graph) | |
return networkx.connected_component_subgraphs(graph) | |
def add_node_edges(graph): | |
offsets = (-1, 1) | |
nodes = graph.nodes() | |
for node in nodes: | |
for offset in offsets: | |
graph.add_edges_from((node,offset_node) for offset_node in \ | |
[(node[0] + offset, node[1]), (node[0], node[1] + offset), (node[0] + offset, node[1] + offset)] \ | |
if offset_node in nodes) | |
return graph | |
def get_nodes(regionset_path): | |
return [(int(p[1]), int(p[2])) \ | |
for p in (f.split('.') \ | |
for f in glob.glob(os.path.join(regionset_path, 'r.*.*.mca')))] | |
if __name__ == '__main__': | |
import sys | |
subgraphs = sorted(get_subgraphs(sys.argv[1])) | |
print 'Total regions: %d' % sum(len(g) for g in subgraphs) | |
print 'Found %d discrete sections' % len(subgraphs) | |
print '-> %s' % ', '.join(str(len(sg)) for sg in subgraphs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment