So this one is a very difficult one I think, so I'll try to make it as clear as possible.
So basically, I have geographic datas :
Nodes (#ID,lat,lng)
WayNodes (#ID,#node_id,#way_id, sequence)
Ways(#id,name)
And what I want is to get the intersection of two GROUPS of ways.
For example I need to find intersection(s) between the ways called "name1" : {way1, way2, way3} and the ways called "name2" : {way4, way5, way6}
So what I need is to do an equivalent to this :
float x;
float y;
float A1 = Y2-Y1;
float B1 = X1-X2;
float C1 = A1*X1+B1*Y1;
float A2 = Y4-Y3;
float B2 = X3-X4;
float C2 = A2*X3+B2*Y3;
float det = A1*B2 - A2*B1;
if(det == 0){
//Lines are parallel
x = 0.0;
y = 0.0;
}else{
x = (B2*C1 - B1*C2)/det;
y = (A1*C2 - A2*C1)/det;
}
BOOL intersection = (x<MAX(X1,X2) && x<MAX(X3,X4) && x>MIN(X1,X2) && x>MIN(X3,X4));
But in SQL !
I kind of think it is possible, my request looks like that :
(F1, F2 and F3 replace two very long functions, which compute X, Y and det, they should be correct.)
SELECT F1(n1.lat,n1.lng,n2.lat,n2.lng,n3.lat,n3.lng,n4.lat,n4.lng) AS x,
F2(n1.lat,n1.lng,n2.lat,n2.lng,n3.lat,n3.lng,n4.lat,n4.lng) AS y,
F3(n1.lat,n1.lng,n2.lat,n2.lng,n3.lat,n3.lng,n4.lat,n4.lng) AS det,
FROM Nodes n1, Nodes n2, Nodes n3, Nodes n4
JOIN WayNodes wn1 ON n1.id = wn1.node_id
JOIN WayNodes wn2 ON n2.id = wn1.node_id
JOIN WayNodes wn3 ON n3.id = wn1.node_id
JOIN WayNodes wn4 ON n4.id = wn1.node_id
JOIN Way w1 ON wn1.way_id = w1.id AND wn2..way_id = w1.id
JOIN Way w2 ON wn3..way_id = w2.id AND wn4..way_id = w2.id
WHERE det != 0 AND
x < MAX(n1.lng, n2.lng)
AND x > MIN(n1.lng, n2.lng)
AND x < MAX(n3.lng, n4.lng)
AND x > MIN(n3.lng, n4.lng)
AND wn1.sequence=wn2.sequence - 1
AND wn3.sequence=wn4.sequence - 1
AND w1.name = "name1"
AND w2.name = "name2"
Apparently something doesn't work in the junction... any idea ?
Is it that you need...
JOIN WayNodes wn1 ON n1.id = wn1.node_id
JOIN WayNodes wn2 ON n2.id = wn2.node_id
JOIN WayNodes wn3 ON n3.id = wn3.node_id
JOIN WayNodes wn4 ON n4.id = wn4.node_id
Related
I want to calculate the distances between buildings. First I get graph_from_place, after I get geometries_from_place. For all geometries I eather take the point or the polygon.centroid as center. To the center I want to get_nearest_node. Unfortunately, all nearest nodes are far away and the same node. What am I doing wrong or how can I calculate the distance between buildings? I tried some different smaller towns and it always leads to the same problem. Thank you very much!
import osmnx as ox
from IPython.display import IFrame
%matplotlib inline
ox.config(log_console=True, use_cache=True)
ox.\__version__
place = "Neuenburg am Rhein, Landkreis Breisgau-Hochschwarzwald, Baden-Württemberg, 79395, Germany"
G = ox.graph_from_place(place)
tags = {'building': True, 'amenity': True, 'addr:housenumber': True}
buildings = ox.geometries_from_place(place, tags)
buildings['center'] = buildings['geometry']
count_buildings_point = 0
count_buildings_poly = 0
for i in range(len(buildings)):
if(buildings.loc[i, 'geometry'].type == 'Polygon'):
buildings.loc[i, 'center'] = buildings.loc[i, 'geometry'].centroid
count_buildings_poly += 1
else:
buildings.loc[i, 'center'] = buildings.loc[i, 'geometry']
count_buildings_point += 1
print('#polygon', count_buildings_poly)
print('#point ', count_buildings_point)
buildings['nearestnode'] = buildings['geometry']
p = buildings.loc[0, 'center'].x, buildings.loc[0, 'center'].y
p
>(7.5589873, 47.8144135)
nn_node1 = ox.get_nearest_node(G, p)
nn_node1
>256970665
p2 = buildings.loc[1, 'center'].x, buildings.loc[1, 'center'].y
p2
>(7.565093, 47.814843)
nn_node2 = ox.get_nearest_node(G, p2)
nn_node2
>256970665
Lol, could figure it out. I had to change lon and lat:
p = buildings.loc[0, 'center'].x, buildings.loc[0, 'center'].y
p
>(7.5589873, 47.8144135)
nn_node1 = ox.get_nearest_node(G, p)
nn_node1
>256970665
p2 = buildings.loc[1, 'center'].x, buildings.loc[1, 'center'].y
p2
>(7.565093, 47.814843)
nn_node2 = ox.get_nearest_node(G, p2)
nn_node2
>256970665
to:
p1 = [buildings.loc[0, 'center'].y, buildings.loc[0, 'center'].x]
p1
>[47.873246, 7.5688247]
nn1 = ox.get_nearest_node(G, p1)
nn1
>90875045
p2 = [buildings.loc[1, 'center'].y, buildings.loc[1, 'center'].x]
p2
>[47.7893012, 7.5377884]
nn2 = ox.get_nearest_node(G, p2)
nn2
>456275
(select A from 'TableB' where C = c and G = g)
intersect
(select A from 'TableB' where C = d and G = h)
First of all, because Mysql does not provide an intersect operator, I changed the query statement written above as follows.
select A
from 'TableB'
where C = c and G = g and A in(
select A
from 'TableB'
where C = d and G = h)
I want to use MongoDB to get the same result as above.
Is there any other way??
let mongoQuery = {
$and:[
{C: c},
{D: d},
{G: g},
{G: h}
]
};
const result = await TableB.find(mongoQuery, {A: 1});
This query will return only elements from 'A' that matches C=c, D=d, G=h, G=g
Hope it helps
I am trying to find three parameters (a, b, c) to fit my experimental data using ODE solver and optimization by least squares using Scilab in-built functions.
However, I keep having the message "submatrix incorrectly defined" at line "y_exp(:,1) = [0.135 ..."
When I try another series of data (t, yexp) such as the one used in the original template I get no error messages. The template I use was found here: https://wiki.scilab.org/Non%20linear%20optimization%20for%20parameter%20fitting%20example
function dy = myModel ( t , y , a , b, c )
// The right-hand side of the Ordinary Differential Equation.
dy(1) = -a*y(1) - b*y(1)*y(2)
dy(2) = a*y(1) - b*y(1)*y(2) - c*y(2)
endfunction
function f = myDifferences ( k )
// Returns the difference between the simulated differential
// equation and the experimental data.
global MYDATA
t = MYDATA.t
y_exp = MYDATA.y_exp
a = k(1)
b = k(2)
c = k(3)
y0 = y_exp(1,:)
t0 = 0
y_calc=ode(y0',t0,t,list(myModel,a,b,c))
diffmat = y_calc' - y_exp
// Make a column vector
f = diffmat(:)
MYDATA.funeval = MYDATA.funeval+ 1
endfunction
// Experimental data
t = [0,20,30,45,75,105,135,180,240]';
y_exp(:,1) =
[0.135,0.0924,0.067,0.0527,0.0363,0.02445,0.01668,0.012,0.009]';
y_exp(:,2) =
[0,0.00918,0.0132,0.01835,0.0261,0.03215,0.0366,0.0393,0.0401]';
// Store data for future use
global MYDATA;
MYDATA.t = t;
MYDATA.y_exp = y_exp;
MYDATA.funeval = 0;
function val = L_Squares ( k )
// Computes the sum of squares of the differences.
f = myDifferences ( k )
val = sum(f.^2)
endfunction
// Initial guess
a = 0;
b = 0;
c = 0;
x0 = [a;b;c];
[fopt ,xopt]=leastsq(myDifferences, x0)
Does anyone know how to approach this problem?
Just rewrite lines 28,29 as
y_exp = [0.135,0.0924,0.067,0.0527,0.0363,0.02445,0.01668,0.012,0.009
0,0.00918,0.0132,0.01835,0.0261,0.03215,0.0366,0.0393,0.0401]';
or insert a clear at line 1 (you may have defined y_exp before with a different size).
I am trying to construct a double count left join query in Slick 3.1 similar to this:
SELECT
COUNT(p.id) AS replies,
COUNT(f.id) AS images
FROM posts AS p
LEFT JOIN files AS f
ON p.id = f.post_id
WHERE thread = :thread_id
Join part of the query is quite simple and looks like this:
val joinQ = (threadId: Rep[Long]) =>
(postDAO.posts joinLeft fileRecordDAO.fileRecords on (_.id === _.postId))
.filter(_._1.thread === threadId)
Using joinQ(1L).map(_._1.id).length generates COUNT(1) which counts all rows - not the result I want to obtain. Using joinQ(1L).map(_._1.id).countDistinct generates COUNT(DISTINCT p.id) which is somewhat what I'm looking for, but trying to do two of these generates this monstrosity:
select x2.x3, x4.x5
from (select count(distinct x6.`id`) as x3
from `posts` x6
left outer join `files` x7 on x6.`id` = x7.`post_id`
where x6.`thread` = 1) x2,
(select
count(distinct (case when (x8.`id` is null) then null else 1 end)) as x5
from `posts` x9
left outer join `files` x8 on x9.`id` = x8.`post_id`
where x9.`thread` = 1) x4
Here's the double countDistinct code:
val q = (threadId: Rep[Long]) => {
val join = joinQ(threadId)
val q1 = join.map(_._1.id).countDistinct // map arg type is
val q2 = join.map(_._2).countDistinct // (Post, Rep[Option[FileRecord]])
(q1, q2)
}
Any ideas? :)
Count is aggregation function, so you also need grouping by some field (e.x. id). Try next query:
def joinQ(threadId: Long) = (postDAO.posts joinLeft fileRecordDAO.fileRecords on (_.id === _.postId))
.filter(_._1.thread === threadId)
val q = (threadId: Long) => {
joinQ(threadId).groupBy(_._1.id).map {
case (id, qry) => (id, qry.map(_._1.id).countDistinct, qry.map(_._2.map(_.id)).countDistinct)
}
}
It generates next sql:
SELECT x2.`id`,
Count(DISTINCT x2.`id`),
Count(DISTINCT x3.`id`)
FROM `posts` x2
LEFT OUTER JOIN `files` x3
ON x2.`id` = x3.`post_id`
WHERE x2.`thread` = 10
GROUP BY x2.`id`
I'm trying to derive a CATransform3D that will map a quad with 4 corner points to another quad with 4 new corner points. I've spent a little bit of time researching this and it seems the steps involve converting the original Quad to a Square, and then converting that Square to the new Quad. My methods look like this (code borrowed from here):
- (CATransform3D)quadFromSquare_x0:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {
float dx1 = x1 - x2, dy1 = y1 - y2;
float dx2 = x3 - x2, dy2 = y3 - y2;
float sx = x0 - x1 + x2 - x3;
float sy = y0 - y1 + y2 - y3;
float g = (sx * dy2 - dx2 * sy) / (dx1 * dy2 - dx2 * dy1);
float h = (dx1 * sy - sx * dy1) / (dx1 * dy2 - dx2 * dy1);
float a = x1 - x0 + g * x1;
float b = x3 - x0 + h * x3;
float c = x0;
float d = y1 - y0 + g * y1;
float e = y3 - y0 + h * y3;
float f = y0;
CATransform3D mat;
mat.m11 = a;
mat.m12 = b;
mat.m13 = 0;
mat.m14 = c;
mat.m21 = d;
mat.m22 = e;
mat.m23 = 0;
mat.m24 = f;
mat.m31 = 0;
mat.m32 = 0;
mat.m33 = 1;
mat.m34 = 0;
mat.m41 = g;
mat.m42 = h;
mat.m43 = 0;
mat.m44 = 1;
return mat;
}
- (CATransform3D)squareFromQuad_x0:(float)x0 y0:(float)y0 x1:(float)x1 y1:(float)y1 x2:(float)x2 y2:(float)y2 x3:(float)x3 y3:(float)y3 {
CATransform3D mat = [self quadFromSquare_x0:x0 y0:y0 x1:x1 y1:y1 x2:x2 y2:y2 x3:x3 y3:y3];
// invert through adjoint
float a = mat.m11, d = mat.m21, /* ignore */ g = mat.m41;
float b = mat.m12, e = mat.m22, /* 3rd col*/ h = mat.m42;
/* ignore 3rd row */
float c = mat.m14, f = mat.m24;
float A = e - f * h;
float B = c * h - b;
float C = b * f - c * e;
float D = f * g - d;
float E = a - c * g;
float F = c * d - a * f;
float G = d * h - e * g;
float H = b * g - a * h;
float I = a * e - b * d;
// Probably unnecessary since 'I' is also scaled by the determinant,
// and 'I' scales the homogeneous coordinate, which, in turn,
// scales the X,Y coordinates.
// Determinant = a * (e - f * h) + b * (f * g - d) + c * (d * h - e * g);
float idet = 1.0f / (a * A + b * D + c * G);
mat.m11 = A * idet; mat.m21 = D * idet; mat.m31 = 0; mat.m41 = G * idet;
mat.m12 = B * idet; mat.m22 = E * idet; mat.m32 = 0; mat.m42 = H * idet;
mat.m13 = 0 ; mat.m23 = 0 ; mat.m33 = 1; mat.m43 = 0 ;
mat.m14 = C * idet; mat.m24 = F * idet; mat.m34 = 0; mat.m44 = I * idet;
return mat;
}
After calculating both matrices, multiplying them together, and assigning to the view in question, I end up with a transformed view, but it is wildly incorrect. In fact, it seems to be sheared like a parallelogram no matter what I do. What am I missing?
UPDATE 2/1/12
It seems the reason I'm running into issues may be that I need to accommodate for FOV and focal length into the model view matrix (which is the only matrix I can alter directly in Quartz.) I'm not having any luck finding documentation online on how to calculate the proper matrix, though.
I was able to achieve this by porting and combining the quad warping and homography code from these two URLs:
http://forum.openframeworks.cc/index.php/topic,509.30.html
http://forum.openframeworks.cc/index.php?topic=3121.15
UPDATE: I've open sourced a small class that does this: https://github.com/dominikhofmann/DHWarpView