I am trying to build a graph where nodes would be the cities of the Friuli-Venezia Giulia region and the edges the roads connecting them (edge's weight being the road length).
I therefore decided to use data from openstreetmaps, I found a way to download an extract containing all I need (https://download.openstreetmap.fr/extracts/europe/italy/). Then I filtered the data using osmfilter to retain only what I needed
osmfilter file.osm --keep-nodes="place=* and population>1000 " --keep-ways="highway=primary =secondary =tertiary" --drop-relations --keep= -o=new.osm
So far everything went well (I guess). It is when I am trying to use osmnx to create the graph that an error occurs
import osmnx as ox
G = ox.graph.graph_from_xml('new.osm', simplify=True)
The error is the following
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'float' object has no attribute 'deg2rad'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-2-75f455d1bcf4> in <module>
----> 1 G = ox.graph.graph_from_xml('new.osm', simplify=True)
~/.local/lib/python3.8/site-packages/osmnx/graph.py in graph_from_xml(filepath, bidirectional, simplify, retain_all)
516
517 # create graph using this response JSON
--> 518 G = _create_graph(response_jsons, bidirectional=bidirectional, retain_all=retain_all)
519
520 # simplify the graph topology as the last step
~/.local/lib/python3.8/site-packages/osmnx/graph.py in _create_graph(response_jsons, retain_all, bidirectional)
585 # add length (great-circle distance between nodes) attribute to each edge
586 if len(G.edges) > 0:
--> 587 G = distance.add_edge_lengths(G)
588
589 return G
~/.local/lib/python3.8/site-packages/osmnx/distance.py in add_edge_lengths(G, precision, edges)
152
153 # calculate great circle distances, round, and fill nulls with zeros
--> 154 dists = great_circle_vec(c[:, 0], c[:, 1], c[:, 2], c[:, 3]).round(precision)
155 dists[np.isnan(dists)] = 0
156 nx.set_edge_attributes(G, values=dict(zip(uvk, dists)), name="length")
~/.local/lib/python3.8/site-packages/osmnx/distance.py in great_circle_vec(lat1, lng1, lat2, lng2, earth_radius)
58 earth_radius
59 """
---> 60 y1 = np.deg2rad(lat1)
61 y2 = np.deg2rad(lat2)
62 dy = y2 - y1
TypeError: loop of ufunc does not support argument 0 of type float which has no callable deg2rad method
Any idea of what is wrong? I would also be glad if anyone knows a better way to create such graph.
Thank you very much
Related
I am following the sample source code on this link: Simplified detection of urban types
. And after updating GeoPandas (0.11.0) and GDAL (3.5.0) packages, I started getting this error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 profile = momepy.StreetProfile(streets, buildings)
File ~/opt/anaconda3/envs/PythonEnv/lib/python3.9/site-packages/momepy/dimension.py:590, in StreetProfile.__init__(self, left, right, heights, distance, tick_length, verbose)
587 ticks.append([line_end_1, pt])
588 ticks.append([line_end_2, pt])
--> 590 ticks = pygeos.linestrings(ticks)
592 inp, res = right.sindex.query_bulk(ticks, predicate="intersects")
593 intersections = pygeos.intersection(ticks[inp], right.geometry.values.data[res])
File ~/opt/anaconda3/envs/PythonEnv/lib/python3.9/site-packages/pygeos/decorators.py:80, in multithreading_enabled.<locals>.wrapped(*args, **kwargs)
78 for arr in array_args:
79 arr.flags.writeable = False
---> 80 return func(*args, **kwargs)
81 finally:
82 for arr, old_flag in zip(array_args, old_flags):
File ~/opt/anaconda3/envs/PythonEnv/lib/python3.9/site-packages/pygeos/creation.py:119, in linestrings(coords, y, z, indices, out, **kwargs)
117 coords = _xyz_to_coords(coords, y, z)
118 if indices is None:
--> 119 return lib.linestrings(coords, out=out, **kwargs)
120 else:
121 return simple_geometries_1d(coords, indices, GeometryType.LINESTRING, out=out)
ValueError: linestrings: Input operand 0 does not have enough dimensions (has 1, gufunc core with signature (i, d)->() requires 2)
the minimum source code that can regenerate the error:
import geopandas
import libpysal
import momepy
import osmnx
import pandas
place = 'Znojmo, Czechia'
buildings = osmnx.geometries.geometries_from_place(place, tags={'building':True})
osm_graph = osmnx.graph_from_place(place, network_type='drive')
#osm_graph = osmnx.projection.project_graph(osm_graph, to_crs=local_crs)
streets = osmnx.graph_to_gdfs(
osm_graph,
nodes=False,
edges=True,
node_geometry=False,
fill_edge_geometry=True
)
profile = momepy.StreetProfile(streets, buildings)
Does anyone aware of changes in the new packages that would result in this error?
Dear people of stackoverflow,
I made a routeplanner based on length and elevation. With both costfactors I get only results with an undirected graph. So the code is good for an undirect graph but doesn't work with a MultiDiGraph as input, which I want because it is necessary with elevation.
The errors I get:
number 1:
~\anaconda3\envs\environmentwithhighestversion\lib\site-
packages\osmnx\plot.py in
plot_graph_route(G, route, route_color, route_linewidth, route_alpha,
orig_dest_size, ax,
**pg_kwargs)
292
293 # scatterplot origin and destination points (first/last nodes in
route)
--> 294 x = (G.nodes[route[0]]["x"], G.nodes[route[-1]]["x"])
295 y = (G.nodes[route[0]]["y"], G.nodes[route[-1]]["y"])
296 ax.scatter(x, y, s=orig_dest_size, c=route_color,
alpha=route_alpha, edgecolor="none")
TypeError: 'NoneType' object is not subscriptable
Number 2:
~\anaconda3\envs\environmentwithhighestversion\lib\site-
packages\osmnx\utils_graph.py in get_route_edge_attributes(G, route,
attribute, minimize_key, retrieve_default)
263 """
264 attribute_values = []
--> 265 for u, v in zip(route[:-1], route[1:]):
266 # if there are parallel edges between two nodes, select the
one with the
267 # lowest value of minimize_key
TypeError: 'NoneType' object is not subscriptable
The code is partly altered, the original code is of G. Boeing (2021):
https://github.com/gboeing/osmnx-examples/blob/main/notebooks/12-node-elevations-edge-grades.ipynb
Here is also a reproducible example possible.
The code for the first error:
TypeError Traceback (most recent call
last)
<ipython-input-3-86e03594e5bd> in <module>
7 route_by_impedance = ox.shortest_path(G7, source, target,
weight="impedance")
8 print('Route Impedance:')
----> 9 fig, ax = ox.plot_graph_route(G7, route_by_impedance,
node_size=3)
10 def print_route_stats(route):
11 route_grades = ox.utils_graph.get_route_edge_attributes(G7,
route, "grade_abs")
The code for the second error:
ipython-input-4-4eb87f73e021> in print_route_stats(route)
9 #fig, ax = ox.plot_graph_route(G7, route_by_impedance, node_size=3)
10 def print_route_stats(route):
---> 11 route_grades = ox.utils_graph.get_route_edge_attributes(G7,
route, "grade_abs")
12 msg = "The average grade is {:.1f}% and the max is {:.1f}%"
13 print(msg.format(np.mean(route_grades) * 100,
np.max(route_grades) * 100))
Does anybody know why I get this problem?
Kind regards,
Damiaan
I'm trying to use a binary integer linear program to assign members of my staff to different shift. I have a 16x9 matrix of preferences for my staff in a csv (16 staff members, 9 slots to fill) and I used the following code to try and assign them:
weights = pd.read_csv("holiday_green day.csv", index_col= 0)
weights = weights.to_numpy().astype(float)
selection = cvx.Variable((9,16), boolean = True)
row_sum_vector = np.ones((16,1)).astype(float)
result_constraint = np.ones((9,1)).astype(float) * 2
objective = cvx.Minimize(cvx.trace(weights # assignments))
prob = cvx.Problem(objective, [assignments # row_sum_vector == result_constraint])
prob.solve()
When I try running this, I get the error TypeError: G must be a 'd' matrix and I don't know where to start debugging. I looked at this post, but it wasn't helpful. Can someone help me figure out what G is and what it means by 'd' matrix? Its my first time actually using CVXPY and I'm very lost.
Full Stack Trace:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-d07ad22cbc25> in <module>()
6 objective = cvx.Minimize(cvx.atoms.affine.trace.trace(weights # assignments))
7 prob = cvx.Problem(objective, [assignments # row_sum_vector == result_constraint])
----> 8 prob.solve()
3 frames
/usr/local/lib/python3.7/dist-packages/cvxpy/problems/problem.py in solve(self, *args, **kwargs)
288 else:
289 solve_func = Problem._solve
--> 290 return solve_func(self, *args, **kwargs)
291
292 #classmethod
/usr/local/lib/python3.7/dist-packages/cvxpy/problems/problem.py in _solve(self, solver, warm_start, verbose, parallel, gp, qcp, **kwargs)
570 self._intermediate_problem)
571 solution = self._solving_chain.solve_via_data(
--> 572 self, data, warm_start, verbose, kwargs)
573 full_chain = self._solving_chain.prepend(self._intermediate_chain)
574 inverse_data = self._intermediate_inverse_data + solving_inverse_data
/usr/local/lib/python3.7/dist-packages/cvxpy/reductions/solvers/solving_chain.py in solve_via_data(self, problem, data, warm_start, verbose, solver_opts)
194 """
195 return self.solver.solve_via_data(data, warm_start, verbose,
--> 196 solver_opts, problem._solver_cache)
/usr/local/lib/python3.7/dist-packages/cvxpy/reductions/solvers/conic_solvers/glpk_mi_conif.py in solve_via_data(self, data, warm_start, verbose, solver_opts, solver_cache)
73 data[s.B],
74 set(int(i) for i in data[s.INT_IDX]),
---> 75 set(int(i) for i in data[s.BOOL_IDX]))
76 results_dict = {}
77 results_dict["status"] = results_tup[0]
TypeError: G must be a 'd' matrix
Edit: Tried casting all numpy arrays as float like they suggested in a different post. It didn't work.
My previous script using OSMNX from Geoff boeing is not working anymore since I did Conda update. It used to run before. This is the bare part of the script that gives the error message
import osmnx as ox
import pandas as pd
import geopandas as gpd
import networkx as nx
import numpy as np
# Set place and language; It must return a POLYGON/POLYLINE, not a POINT, so you might have to play with it a little, or set which_result below accordingly
place='ALmere, Netherlands'
# note the which_result parameter, as per comment above. Default which_result=1. For places like Utrecht changing it gives a different result
G = ox.graph_from_place(place, network_type='all', which_result=1)
# For the colouring, we take the attributes from each edge found extract the road name, and use the function above to create the colour array
edge_attributes = ox.graph_to_gdfs(G, nodes=False)
Gives error message:
TypeError: unhashable type: 'dict'
I seem to have the last version of Osmnx (conda list shows 0.16.1). I did find this question, but can't translate that to my code: TypeError: unhashable type: 'dict' in Networkx random walk code that was previously working
And this one: https://github.com/gboeing/osmnx/issues/372. My Python version is 3.8.5
Traceback below:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-6a868604da3b> in <module>
10
11 # note the which_result parameter, as per comment above. Default which_result=1. For places like Utrecht changing it gives a different result
---> 12 G = ox.graph_from_place(place, network_type='all', which_result=1)
13
14 # For the colouring, we take the attributes from each edge found extract the road name, and use the function above to create the colour array
~\Anaconda3\lib\site-packages\osmnx\core.py in graph_from_place(query, network_type, simplify, retain_all, truncate_by_edge, name, which_result, buffer_dist, timeout, memory, max_query_area_size, clean_periphery, infrastructure, custom_filter)
1443 max_query_area_size=max_query_area_size,
1444 clean_periphery=clean_periphery, infrastructure=infrastructure,
-> 1445 custom_filter=custom_filter)
1446
1447 log('graph_from_place() returning graph with {:,} nodes and {:,} edges'.format(len(list(G.nodes())), len(list(G.edges()))))
~\Anaconda3\lib\site-packages\osmnx\core.py in graph_from_polygon(polygon, network_type, simplify, retain_all, truncate_by_edge, name, timeout, memory, max_query_area_size, clean_periphery, infrastructure, custom_filter)
1319 G_buffered = create_graph(response_jsons, name=name, retain_all=True,
1320 bidirectional=network_type in settings.bidirectional_network_types)
-> 1321 G_buffered = truncate_graph_polygon(G_buffered, polygon_buffered, retain_all=True, truncate_by_edge=truncate_by_edge)
1322
1323 # simplify the graph topology
~\Anaconda3\lib\site-packages\osmnx\core.py in truncate_graph_polygon(G, polygon, retain_all, truncate_by_edge, quadrat_width, min_num, buffer_amount)
731
732 # find all the nodes in the graph that lie outside the polygon
--> 733 points_within_geometry = intersect_index_quadrats(gdf_nodes, polygon, quadrat_width=quadrat_width, min_num=min_num, buffer_amount=buffer_amount)
734 nodes_outside_polygon = gdf_nodes[~gdf_nodes.index.isin(points_within_geometry.index)]
735
~\Anaconda3\lib\site-packages\osmnx\core.py in intersect_index_quadrats(gdf, geometry, quadrat_width, min_num, buffer_amount)
678 # drop duplicate points, if buffered poly caused an overlap on point(s)
679 # that lay directly on a quadrat line
--> 680 points_within_geometry = points_within_geometry.drop_duplicates(subset='node')
681 else:
682 # after simplifying the graph, and given the requested network type,
~\Anaconda3\lib\site-packages\pandas\core\frame.py in drop_duplicates(self, subset, keep, inplace, ignore_index)
5106
5107 inplace = validate_bool_kwarg(inplace, "inplace")
-> 5108 duplicated = self.duplicated(subset, keep=keep)
5109
5110 result = self[-duplicated]
~\Anaconda3\lib\site-packages\pandas\core\frame.py in duplicated(self, subset, keep)
5245
5246 vals = (col.values for name, col in self.items() if name in subset)
-> 5247 labels, shape = map(list, zip(*map(f, vals)))
5248
5249 ids = get_group_index(labels, shape, sort=False, xnull=False)
~\Anaconda3\lib\site-packages\pandas\core\frame.py in f(vals)
5220 def f(vals):
5221 labels, shape = algorithms.factorize(
-> 5222 vals, size_hint=min(len(self), _SIZE_HINT_LIMIT)
5223 )
5224 return labels.astype("i8", copy=False), len(shape)
~\Anaconda3\lib\site-packages\pandas\core\algorithms.py in factorize(values, sort, na_sentinel, size_hint)
676
677 codes, uniques = _factorize_array(
--> 678 values, na_sentinel=na_sentinel, size_hint=size_hint, na_value=na_value
679 )
680
~\Anaconda3\lib\site-packages\pandas\core\algorithms.py in _factorize_array(values, na_sentinel, size_hint, na_value, mask)
499 table = hash_klass(size_hint or len(values))
500 uniques, codes = table.factorize(
--> 501 values, na_sentinel=na_sentinel, na_value=na_value, mask=mask
502 )
503
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.factorize()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable._unique()
TypeError: unhashable type: 'dict'
I know there's a bunch of questions on here regarding the use of the rolling_mean function in pandas - but I can't get it to work.
I'm new to Python, and the packages numpy,pandas etc.
I have a list of DateTime objects and I have a list of simple integers. Plotting them works without a problem. But I can't plot a moving average line to the graph!
I'm trying to use the Pandas docs to understand it but I still can't get it to work. This is what I have tried:
# code before this simply reads a file and converts dates into datetime objects and values into a list
x1 = date_object #list of datetime objects
y1 = values1 #list of integer values
plot(x1,y1) # works fine
t = pd.date_range(date_object[0].strftime('%m/%d/%Y'), date_object[len(date_object)-1].strftime('%m/%d/%Y'), freq='W')
ts = pd.Series(y1, t)
ts_movavg = pd.rolling_mean(ts,10)
plot(ts_movavg)
..I get the following error:
ValueError: setting an array element with a sequence.
As you can probably quickly tell, I'm very confused. I think I'm missing the point of the Series object.
EDIT: (full traceback)
ValueError Traceback (most recent call last)
<ipython-input-228-2247062d3126> in <module>()
33 ts = pd.Series(y1, x1)
34
---> 35 ts_movavg = PD.rolling_mean(ts,10)
36
37 ts_movavg.head()
C:\Users\****\Anaconda\lib\site-packages\pandas\stats\moments.py in f(arg, window, min_periods, freq, center, time_rule, **kwargs)
507 return _rolling_moment(arg, window, call_cython, min_periods,
508 freq=freq, center=center,
--> 509 time_rule=time_rule, **kwargs)
510
511 return f
C:\Users\****\Anaconda\lib\site-packages\pandas\stats\moments.py in _rolling_moment(arg, window, func, minp, axis, freq, center, time_rule, **kwargs)
278 arg = _conv_timerule(arg, freq, time_rule)
279 calc = lambda x: func(x, window, minp=minp, **kwargs)
--> 280 return_hook, values = _process_data_structure(arg)
281 # actually calculate the moment. Faster way to do this?
282 if values.ndim > 1:
C:\Users\****\Anaconda\lib\site-packages\pandas\stats\moments.py in _process_data_structure(arg, kill_inf)
326
327 if not issubclass(values.dtype.type, float):
--> 328 values = values.astype(float)
329
330 if kill_inf:
ValueError: setting an array element with a sequence.
Could someone show me how to plot a moving average line using the rolling_mean function?