I am making project using data from map. I have 2 structures. "ways" and "nodes". Nodes contain ID, Latitude and Longitude. Ways contain information about "which nodes (IDs) represent this road (they are points marking street)" and data I want to retrieve. Structures are following:
nodes = version: 0.6000 generator: 'Overpass API' osm3s: [1x1 struct] elements: {37952x1 cell}
In elements there are cells containing 1×1 struct like that:
nodes.elements{1,1} = type: 'node' id: 30788202 lat: 54.4719 lon: 18.4729
———————
Ways are similar:
ways = version: 0.6000 generator: 'Overpass API' osm3s: [1x1 struct] elements: [247x1 struct]
Inside:
ways.elements = 247x1 struct array with fields: type id nodes tags
For example:
ways.elements(1) = type: 'way' id: 4941052 nodes: [6x1 double] tags: [1x1 struct]
—————————
Now. I have GPS positions from my ride (a lot of them) – lat and lon. I create small box around each of my positions and then search all the nodes to check if any of them is inside my box. If I find that I am near the node, I search all the ways to find what road is it (I check ways.elements(i).nodes if they contain the node near me). When found I take necessary data from tags.
————————-
Code looks like that:
diff = 0.1e-4; %to create small box around my position
%initialization
speedlimit = zeros(length(GPSinterpol),1); previousNode = 0; %just to make sure they are different
nodeID = -1; %just to make sure they are different for k=1:length(GPSinterpol) %for all positions I have been at
position = GPSinterpol(k,:); positionplus = position + diff; %create upper right corner of the box
positionminus = position - diff; %create lower left corner of the box
for i = 1:length(nodes.elements) %search all nodes
%check if any node is inside my box
if all([nodes.elements{i,1}.lat nodes.elements{i,1}.lon] > positionminus) && all([nodes.elements{i,1}.lat nodes.elements{i,1}.lon] < positionplus) nodeID = nodes.elements{i,1}.id; if nodeID ~= previousNode %find way corresponding our node
for j = 1:length(ways.elements) %search all ways
if any(ways.elements(j,1).nodes == nodeID) %way found
speedlimit(k) = str2double(ways.elements(j,1).tags.maxspeed); %get speedlimit from tags
break end end end break end end if nodeID == previousNode speedlimit(k) = speedlimit(k-1); end previousNode = nodeID; end
My question is how to do this better ;). Using loops is extremely slow for that amount of data and I want to use it on even bigger. I hope there are better ways to search structures
Best Answer