Can I do one query to achieve vertex and all of his edges (including the other vertexes)?
now I'm doing multiple queries to achieve it:
For getting the vertex:
select from V where entity_name = 'SomeEntity'
Then going over each edge and select it:
select from #EDGE#rid // Multiple in the number of edges for this node
And at the end going over the edges and getting all other vertexes (except the original)
select from #VERTEX#rid // Multiple in the number of vertexes in the edges
SELECT *,in(),out() FROM V WHERE entity_name = 'SomeEntity'
I used the following to get all Vertexes and Edges in the required depth from my Vertex:
TRAVERSE inE(), outE(), inV(), outV()
FROM (select from V where entity_name = 'SomeEntity')
WHILE $depth <= 2
An observation; the answer by Dor Cohen works if the structure is a tree. However, if it is a cyclical graph, the last edge on the longest path is missed, due to the node visit strategy of not visiting the node which has been visited. The solution that worked for me is as below:
TRAVERSE out(), outE() FROM #22:1 WHILE $depth <= 4
The query traverses all the nodes with are deemed progressively out of the vertex and then the edges which are progressively out from all the vertices.
The output looks like:
The structure of the graph:
On the other hand if the above style of query is used (though I am not looking for in vertex or edge), the output is:
The edge from #22:4 to #23:1 is missed (as a record), as it falls on the longer path. The reference of the missed edge (#28:2) is available on both the above records though.
Related
I try to figure out how to come from a single given coordinate (lat/lon) to the nearest bounds which enclose this coordinate on a map e.g. streets or sea.
Here two examples to give you a better understanding of what I mean:
What i tried already or thought about:
Setting up a Nominatim server and search for the given coordinate via the reverse-function to get the bbox and/or the geojson polygon of this coordinate. -> this only works when the given coordinate is within a POI or for example directly on a street.
Writing an algorithm to walk in all 4 or 8 directions (n/e/s/w) and 'stop' when the map layer/surface changes (change = stop for this direction and mark a bounding-point)
Building up an image-recognition system using TensorFlow to detect the different colors and 'draw' the polygon. Worked with TensorFlow a couple of times but this seems to be the most tricky solution to implement (but at my current understanding the most precise one)
Does someone of you have any other ideas to get a solution for this problem? Would appreciate any kind of approaches
Cheers!
If I got your question right, you might wanna first select all polygons in which the given point is inside of using ST_Contains, and then compute the distance to this point using ST_Distance. If you ORDER BY distance and LIMIT to 1 result you'll get the nearest polygon, e.g.
Data Sample
CREATE TABLE t (gid int, geom geometry);
INSERT INTO t VALUES
(1,'POLYGON((-4.47 54.26,-4.44 54.28,-4.41 54.24,-4.46 54.23,-4.47 54.26))'),
(2,'POLYGON((-4.48 54.25,-4.40 54.25,-4.41 54.23,-4.48 54.23,-4.48 54.25))'),
(3,'POLYGON((-4.53 54.23,-4.44 54.29,-4.38 54.22,-4.53 54.23))');
Query
SELECT gid,ST_AsText(geom) FROM t
WHERE ST_Contains(geom,ST_MakePoint(-4.45, 54.25))
ORDER BY ST_Distance(geom,ST_MakePoint(-4.45, 54.25))
LIMIT 1;
gid | st_astext
-----+------------------------------------------------------------------------
1 | POLYGON((-4.47 54.26,-4.44 54.28,-4.41 54.24,-4.46 54.23,-4.47 54.26))
(1 Zeile)
I have 02 questions:
1- From the graph2 below, I want to create several regions in a networkx based on a criterion (first zone starts at node X up to a given set of nodes). For example, region 1 starts from node 1 to nodes (4 and 16). Is there a way to do that?
create regions on the graph
2- I also want to determine the depth of each node in the attached graph
node_depth of a graph
Thank you for any hint that could help.
What do you mean by "regions"? Are you asking how to create subgraphs (https://en.wikipedia.org/wiki/Glossary_of_graph_theory_terms#subgraph)? That is, do you want to create separate, smaller graphs which contain only the nodes and edges existing in the regions you have drawn in your picture?
If so, you can do that using the subgraph() function of networkx.
In your example, to create the subgraph of region 1 you would do:
region1 = graph2.subgraph([1,2,3,4,11,12,13,14,15,16])
and an equivalent statement for regions 2 and 3.
Then for node depth, I am not sure what you mean but I think you might mean distance from some root node.
Let's assume your root node is 1.
Then to find the "depth" of each node, you want to find the distance(number of edges) between that node and 1.
To do that you could do something like:
for n in myGraph.nodes():
print n, networkx.shortest_path(graph, 1, n)
I'm not an expert in networkx so sorry for not using the right terms. Coming back to my 02 points: A region can effectively be considered as a subgraph. To note that the main criterion behind the construction of the region is to start from a reference node (node 1 for region 1) and include all the nodes and edges until a specific condition is met on a specific edge (in my case it is the presence of a electric device on an edge).
how to build a region
I am trying to fetch edge property value between two vertices. E.g. A-->B
A and B are two vertices and it has edge with property(name).
My code looks like:
graph.V().hasLabel(A).outE().value("name").headOption()
It gives me the property value for name.
In a given two vertices, i am getting None as output
graph.traversal().V().hasLabel(A).outE("test").outV().hasLabel(B).properties("name").headOption()
'test' - Edge Label
'name' - Edge property
Any idea what is wrong with my query.
Apologies for not being able to answer this in your comments on the previous question you asked. I think what you are looking for is:
graph.traversal().V()
.hasLabel("A").outE("test").as("x").otherV()
.hasLabel("B").select("x").properties("name");
If you just want the values of the properties on the edge you can do the following:
graph.traversal().V()
.hasLabel("A").outE("test").as("x").otherV()
.hasLabel("B").select("x").values("name");
Side Note (Why is your original traversal wrong): Your original traversal:
graph.traversal().V().hasLabel(A).outE("test").outV().hasLabel(B).properties("name").headOption()
is doing the following:
Get all vertices with the label "A"
From those vertices follow the outward going edges which have the label "test" to vertices which have the label "B"
Then get the property "name" from those vertices
You are actually asking for the properties on the vertex.
I'm using Tinkerpop 3.0.1 (Titan 1.0.0), and I try to list all edges for a vertex, sorted by degree of the node on the other end of the edge.
I tried:
g.V(1482896).bothE().order().by(otherV().bothE().count(), decr)
I get the following error from Titan:
The path history of the traverser does not contain a previous vertex: [e[1d2m8u-1d70ts-b2t-vs7k][82628848-DIRECTED->1482896]]
The strange thing is, there is a previous vertex in the path (namely vertex #1482896. I'm confused on how to solve this one.
Answering my own question, after playing around with this, I found a workaround:
g.V(123).bothE().as('edges')
.otherV().order().by(bothE().count(), decr)
.select('edges')
This will effectively sort edges adjacent to vertex #123 by highest degree of the node on the other end of the edge.
I would like to find all highway way member nodes in a certain radius. I cannot see how to do this without using intersection, however, that is not in the API. For example I have this:
[out:json];
way(around:25, 50.61193,-4.68711)["highway"];>->.a;
(node(around:25, 50.61193,-4.68711) - .a);
out;
Result set .a contains the nodes I want but also nodes outside the radius - potentially a large number if the ways are long. I can find all the nodes inside the radius I don't need, as returned by the complete query above. Now I can always perform a second around query and do the intersection of the two result sets outside of Overpass. Or I can do another difference:
[out:json];
way(around:25, 50.61193,-4.68711)["highway"];>->.a;
(node(around:25, 50.61193,-4.68711) - .a)->.b;
(node(around:25, 50.61193,-4.68711) - .b);
out;
This gives the result I want but can it be simplified? I'm certain I'm missing something here.
Indeed, your query can be simplified to an extent that we don't need any difference operator at all. I would recommend the following approach:
We first query for all nodes around a certain lat/lon position and a given radius.
Based on this set of nodes we determine all ways, which contain some of the previously found nodes (-> Hint: that's why we don't need any kind of intersection or difference!).
Using our set of highway ways we now look again for all nodes of those ways within a certain radius of our lat/lon position.
In Overpass QL this reads like:
[out:json];
node(around:25, 50.61193,-4.68711);
way(bn)[highway];
node(w)(around:25, 50.61193,-4.68711);
out;
Try it on Overpass Turbo