|
| 1 | +import overpy |
| 2 | +import numpy |
| 3 | +import pickle |
| 4 | +from matplotlib import pyplot |
| 5 | +from shapely.geometry import LineString |
| 6 | +from shapely.geometry import shape |
| 7 | +from shapely.geometry import box |
| 8 | +from shapely.ops import polygonize_full |
| 9 | +from descartes import PolygonPatch |
| 10 | +from fiona import collection |
| 11 | + |
| 12 | +features = collection("~/Desktop/gshhg-shp-2/GSHHS_shp/f/GSHHS_f_L1.shp") |
| 13 | +northAmericaPolygon = shape(features[3]['geometry']) |
| 14 | + |
| 15 | +api = overpy.Overpass() |
| 16 | + |
| 17 | +stateRelation = api.query("rel(165475);(._;>;);out;") |
| 18 | +fnwrRelation = api.query("rel(3947664);(._;>;);out;") |
| 19 | +motorwaysInBoundingBox = api.query("way(32.120,-125.222,42.212,-113.928)[highway=motorway];(._;>);out;") |
| 20 | +trunkInBoundingBox = api.query("way(32.120,-125.222,42.212,-113.928)[highway=trunk];(._;>);out;") |
| 21 | + |
| 22 | +stateLineStrings = [] |
| 23 | +for way in stateRelation.ways: |
| 24 | + lineString = [] |
| 25 | + for node in way.nodes: |
| 26 | + lineString.append((node.lon,node.lat)) |
| 27 | + stateLineStrings.append(LineString(lineString)) |
| 28 | + |
| 29 | +polygons, dangles, cuts, invalids = polygonize_full(stateLineStrings) |
| 30 | +statePolygonWithOcean = polygons.geoms[3] |
| 31 | + |
| 32 | +statePolygon = statePolygonWithOcean.intersection(northAmericaPolygon) |
| 33 | + |
| 34 | +fnwrLineStrings = [] |
| 35 | +for way in fnwrRelation.ways: |
| 36 | + lineString = [] |
| 37 | + for node in way.nodes: |
| 38 | + lineString.append((node.lon,node.lat)) |
| 39 | + fnwrLineStrings.append(LineString(lineString)) |
| 40 | + |
| 41 | +Fpolygons, dangles, cuts, invalids = polygonize_full(fnwrLineStrings) |
| 42 | + |
| 43 | +motorwayLineStrings = [] |
| 44 | +for way in motorwaysInBoundingBox.ways: |
| 45 | + line = [] |
| 46 | + for node in way.nodes: |
| 47 | + line.append((node.lon,node.lat)) |
| 48 | + wayLineString = LineString(line) |
| 49 | + if statePolygonWithOcean.contains(wayLineString): motorwayLineStrings.append(wayLineString) |
| 50 | + |
| 51 | +for way in trunkInBoundingBox.ways: |
| 52 | + line = [] |
| 53 | + for node in way.nodes: |
| 54 | + line.append((node.lon,node.lat)) |
| 55 | + wayLineString = LineString(line) |
| 56 | + if statePolygonWithOcean.contains(wayLineString): motorwayLineStrings.append(wayLineString) |
| 57 | + |
| 58 | +islandPolygons = [] |
| 59 | +for i in range(0,len(polygons.geoms)): |
| 60 | + if (i != 3) and (i != 4): |
| 61 | + islandPolygons.append(polygons.geoms[i]) |
| 62 | + |
| 63 | +for geom in Fpolygons.geoms: |
| 64 | + islandPolygons.append(geom) |
| 65 | + |
| 66 | +fig = pyplot.figure(figsize=(100,100)) |
| 67 | +ax = fig.add_subplot(111) |
| 68 | + |
| 69 | +for line in motorwayLineStrings: |
| 70 | + x, y = line.xy |
| 71 | + ax.plot(x, y, color='#000000', linewidth=1, zorder=2) |
| 72 | + |
| 73 | +patch = PolygonPatch(statePolygon, fc='#FFFFFF', ec='#000000', zorder=1) |
| 74 | +ax.add_patch(patch) |
| 75 | + |
| 76 | +for polygon in islandPolygons: |
| 77 | + patch = PolygonPatch(polygon, fc='#FFFFFF', ec='#000000', zorder=1) |
| 78 | + ax.add_patch(patch) |
| 79 | + |
| 80 | +fig.savefig('test2.png') |
| 81 | + |
| 82 | +##if we want more roads: |
| 83 | +hgvInBoundingBox = api.query("way(32.120,-125.222,42.212,-113.928)[hgv=designated];(._;>);out;") |
| 84 | +motorwayLineStrings = [] |
| 85 | +for way in hgvInBoundingBox.ways: |
| 86 | + line = [] |
| 87 | + for node in way.nodes: |
| 88 | + line.append((node.lon,node.lat)) |
| 89 | + wayLineString = LineString(line) |
| 90 | + if statePolygonWithOcean.contains(wayLineString): motorwayLineStrings.append(wayLineString) |
| 91 | + |
| 92 | + |
| 93 | +##pickle |
| 94 | +output = open('data.pkl', 'wb') |
| 95 | +pickle.dump(islandPolygons,output) |
| 96 | +pickle.dump(statePolygon,output) |
| 97 | +pickle.dump(motorwayLineStrings,output) |
| 98 | +output.close() |
| 99 | + |
| 100 | +pkl_file = open('data.pkl', 'rb') |
| 101 | +islandPolygons = pickle.load(pkl_file) |
| 102 | +statePolygon = pickle.load(pkl_file) |
| 103 | +motorwayLineStrings = pickle.load(pkl_file) |
| 104 | +pkl_file.close() |
| 105 | + |
| 106 | + |
| 107 | +##multiprocessing |
| 108 | +from multiprocessing import Pool |
| 109 | + |
| 110 | +def buildLineString(way): |
| 111 | + lineString = [] |
| 112 | + for node in way.nodes: |
| 113 | + lineString.append((node.lon,node.lat)) |
| 114 | + return lineString |
| 115 | + |
| 116 | +p = Pool(8) |
| 117 | + |
| 118 | +for way in stateRelation.ways: |
| 119 | + stateLineStrings = p.map(buildLineString,stateRelation.ways) |
| 120 | + |
| 121 | +features = collection("~/Desktop/gshhg-shp-2/GSHHS_shp/f/GSHHS_f_L1.shp") |
| 122 | +northAmericaPolygon = shape(features[3]['geometry']) |
| 123 | + |
| 124 | +api = overpy.Overpass() |
| 125 | + |
| 126 | +stateRelation = api.query("rel(165475);(._;>;);out;") |
| 127 | +fnwrRelation = api.query("rel(3947664);(._;>;);out;") |
| 128 | +motorwaysInBoundingBox = api.query("way(32.120,-125.222,42.212,-113.928)[highway=motorway];(._;>);out;") |
| 129 | + |
| 130 | +stateLineStrings = [] |
| 131 | +for way in stateRelation.ways: |
| 132 | + lineString = [] |
| 133 | + for node in way.nodes: |
| 134 | + lineString.append((node.lon,node.lat)) |
| 135 | + stateLineStrings.append(LineString(lineString)) |
| 136 | + |
| 137 | +## debug info |
| 138 | +from geopandas import GeoSeries |
| 139 | +from geopandas import GeoDataFrame |
| 140 | + |
| 141 | +for i in range(0,len(islandPolygons)): |
| 142 | + ax.text(islandPolygons[i].centroid.x,islandPolygons[i].centroid.y,i,color = 'k', weight = 'bold') |
| 143 | + |
| 144 | +ax.plot([-124.482003,-114.1307816],[32.5295236,42.009499]) |
| 145 | + |
| 146 | +from shapely.geometry import MultiLineString |
| 147 | +test = MultiLineString(highwayLineStrings) |
| 148 | +patch = PolygonPatch(countyPolygon, fc='#6699cc', ec='#6699cc', alpha=0.5, zorder=2) |
| 149 | +ax.add_patch(patch) |
| 150 | + |
| 151 | +object.__dict__ |
| 152 | +result.nodes |
| 153 | +result.nodes.[0].lat |
| 154 | +result.ways |
| 155 | +result.ways[0]._node_ids[0] |
| 156 | +result.relations |
| 157 | +result.relations[0].members[0].ref |
| 158 | + |
| 159 | +len(streetsInBB.ways[0].get_nodes(resolve_missing=True)) |
| 160 | + |
| 161 | +for way in aboveIslands: |
| 162 | + gray = gray + .025 |
| 163 | + for node in way.nodes: |
| 164 | + ax.scatter(node.lon, node.lat, color=[gray,gray,gray], s=100, zorder=1) |
| 165 | + |
| 166 | +states = [shapely.geometry.shape(f['geometry']) for f in features] |
| 167 | + |
| 168 | +http://overpass.osm.rambler.ru/cgi/interpreter?data=%5Bout:json%5D;relation(396479);out; |
| 169 | + |
| 170 | +Farallon National Wildlife Refuge (3947664) |
| 171 | +Santa Catalina Island (237602) |
| 172 | +San Clemente Island (237603) |
| 173 | +Anacapa Island (3635899) |
| 174 | +Santa Cruz Island (237600) |
| 175 | +Santa Rosa Island (237601) |
| 176 | +Way: Santa Barbara Island (40501085) |
| 177 | +Way: Sutil Island (40501070) |
| 178 | +Way: San Nicolas Island (40500976) |
| 179 | +Way: San Miguel Island (40500912) |
| 180 | + |
| 181 | +islandRelationNumbers = [3947664,237602,237603,3635899,237600,237601] |
| 182 | +islandWayNumbers = [40501085,40501070,40500976,40500912] |
| 183 | + |
| 184 | +islandRelations = [] |
| 185 | +for relation in islandRelationNumbers: |
| 186 | + response = api.query("rel(" + relation + ");(._;>;);out;") |
| 187 | + islandRelations.append(response) |
| 188 | + |
| 189 | +islandWays = [] |
| 190 | +for ways in islandWayNumbers: |
| 191 | + response = api.query("way(" + way + ");(._;>;);out;") |
| 192 | + islandWays.append(response) |
| 193 | + |
| 194 | +islandPolygons = [] |
| 195 | +for relation in islandRelations: |
| 196 | + islandLineStrings = [] |
| 197 | + for way in relation.ways: |
| 198 | + lineString = [] |
| 199 | + for node in way.nodes: |
| 200 | + lineString.append((node.lon,node.lat)) |
| 201 | + islandLineStrings.append(LineString(lineString)) |
| 202 | + polygons, dangles, cuts, invalids = polygonize_full(stateLineStrings) |
| 203 | + islandPolygons.append(polygons.geoms[0]) |
| 204 | + |
| 205 | +for way in islandWays: |
| 206 | + lineString = [] |
| 207 | + for node in way.nodes: |
| 208 | + lineString.append((node.lon,node.lat)) |
| 209 | + islandLineStrings.append(LineString(lineString)) |
| 210 | + polygons, dangles, cuts, invalids = polygonize_full(stateLineStrings) |
| 211 | + islandPolygons.append(polygons.geoms[0]) |
0 commit comments