Skip to content

Commit

Permalink
refactor notebook to use helper funcs outside
Browse files Browse the repository at this point in the history
  • Loading branch information
jpacilo committed Aug 29, 2023
1 parent c4b5bdd commit 8bf2291
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 55 deletions.
68 changes: 14 additions & 54 deletions notebooks/1-Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -95,67 +95,27 @@
"pd.options.display.max_colwidth=200\n",
"pd.options.display.max_columns=100\n",
"from itertools import combinations\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# custom lib for the lecture\n",
"# from sds4gdsp import loader, processor"
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a20b68c5-d574-45c9-b777-bde3ff96f559",
"metadata": {
"tags": []
},
"id": "6f580e2b",
"metadata": {},
"outputs": [],
"source": [
"def make_points(num_points, lng_min, lng_max, lat_min, lat_max):\n",
" coords = []\n",
" for _ in range(num_points):\n",
" lng = random.uniform(lng_min, lng_max)\n",
" lat = random.uniform(lat_min, lat_max)\n",
" coords.append((lng, lat))\n",
" return coords\n",
"\n",
"def make_lines(points):\n",
" random.shuffle(points)\n",
" lines = []\n",
" for orig, dest in zip(points, points[1:]):\n",
" if random.choices([True, False], [30, 70], k=1)[0]:\n",
" lines.append(LineString([orig, dest]))\n",
" return lines\n",
"\n",
"def make_polygon(points):\n",
" return Polygon(points)\n",
"\n",
"def make_spatial_data(n):\n",
" coords = np.random.random((n, 2))\n",
" points = list(map(lambda z: Point(z), coords))\n",
" lines = LineString(points)\n",
" lines = list(map(lambda x: LineString(x), list(zip(points, points[1:]))))\n",
" polygon = Polygon(points)\n",
" return points, lines, polygon\n",
"\n",
"def make_graph(\n",
" origin, network_type, dist=500, dist_type=\"bbox\", retain_all=False, simplify=True\n",
"):\n",
" # query the road network using OSMNx\n",
" G = ox.graph_from_point(\n",
" center_point=origin, # origin point of query\n",
" dist=dist, # radius in meters from the origin\n",
" dist_type=dist_type, # examples is `bbox`\n",
" retain_all=retain_all, # filter connected components?\n",
" simplify=simplify, # simplify network topology\n",
" network_type=network_type # filter to <insert type from OSM> roads\n",
" )\n",
" return G\n",
"\n",
"def get_coord_sequence(G, route):\n",
" route_coords = []\n",
" for node in route:\n",
" route_coords.append((G.nodes[node][\"x\"], G.nodes[node][\"y\"]))\n",
" return route_coords"
"import sys\n",
"sys.path.insert(0, 'sds4gdsp')\n",
"\n",
"from sds4gdsp.loader import (\n",
" make_points,\n",
" make_lines,\n",
" make_polygon,\n",
" make_spatial_data,\n",
" make_graph,\n",
" get_coord_sequence\n",
")"
]
},
{
Expand Down
3 changes: 2 additions & 1 deletion sds4gdsp/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def make_lines(points):
def make_polygon(points):
return Polygon(points)

def make_geoms(n):
def make_spatial_data(n):
coords = np.random.random((n, 2))
points = list(map(lambda z: Point(z), coords))
lines = LineString(points)
Expand All @@ -35,6 +35,7 @@ def make_geoms(n):
def make_graph(
origin, network_type, dist=500, dist_type="bbox", retain_all=False, simplify=True
):
# query the road network using OSMNx
G = ox.graph_from_point(
center_point=origin, # origin point of query
dist=dist, # radius in meters from the origin
Expand Down

0 comments on commit 8bf2291

Please sign in to comment.