Distance between buildings - osmnx

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

Related

Error with continuous time simulation. 'Index exceeds the number of array elements (2).'

Im attempting to model a the following continuous time simulation but do not understand why there is an indexing error.
clear all
global Bsm Bsf Jm Jg1 Jf Gc GR Ke Kf Kg Kr Ks Kt L lf mf R g Va Tf
%Model Paramaters
Bsm=0.01; Bsf=1.5; Jm=0.002; Jg1=0.001; Jf=0.0204; Gc=9.95; GR=1.7; R=4; g=9.81;
Ke=0.35; Kf=0.5; Kg=2.0; Kr=0.9; Ks=0.9; Kt=0.35; L=0.1; lf=0.35; mf=0.5; Va=1;
% Define parameters for the simulation
stepsize = 0.1;
comminterval = 0.1;
EndTime = 20;
i = 0;
% Initial conditions
u = 0;
x = [0,0]';
xdot = [0,0]';
for time = 0:stepsize:EndTime
if rem(time,comminterval)==0
i = i+1;
tout(i) = time;
xout(i,:) = x;
xdout(i,:) = xdot;
end
xdot = derivitive(x,u);
x = eulerint(xdot, stepsize, x);
end
figure(1)
clf % clear figure
plot(tout,xout(:,1),'bo-')
xlabel('time [s]')
ylabel('states')
hold on
grid on
plot(tout,xout(:,2),'ro-')
hold off
Index exceeds the number of array elements (2).
Error in derivitive (line 5)
xdot(1,1) = -R/L*x(1)-Ke/L*x(3)+Va/L;`
The function 'derivative' for my dynamic equations I'm attempting to call is as follows:
function xdot = derivitive(x,u);
global Bsm Bsf Jm Jg1 Jf Gc GR Ke Kf Kg Kr Ks Kt L lf mf R g Va Tf
xdot(1,1) = -(R/L)*x(1)-(Ke/L)*x(3)+(Va/L);
xdot(2,1) = x(3);
xdot(3,1) = -Bsm/Jm*x(3)+Bsm/Jm*x(5)+Kt/Jm*x(1);
xdot(4,1) = x(5);
xdot(5,1) = Bsm/Jg1*x(3)-Bsm/Jg1*x(5);
xdot(6,1) = x(7);
xdot(7,1) = -Bsf/Jf*x(7)-mf*lf*g/(2*Jf)*x(4)-mf*lf*g/(2*Jf)*x(6)+Tf/Jf``
Do not understand why there is an indexing error, any help appreciated.

how to optimize max position limit in quantstrat for bollinger bands strategy

The code below is my Bollinger bands strategy for commodity. I add a position limit and want to optimize this strategy through parameter maxpos. For the function add.distribution, it asks for a component.label, but the function addPosLimit does not have a variable called "label". I wonder how to optimize maxpos at this case.
symbol <- 'C1'
currency("USD")
#stock(symbol, currency="USD", multiplier=1)
portfName <- 'RSI_Strategy'
acctName <- portfName
suppressWarnings(rm.strat(stratName))
initPortf(name = portfName, symbols = symbol, initDate = initDate,
currency = 'USD')
initAcct(name = acctName, portfolios = portfName,
initDate=initDate, initEq=initEq)
initOrders(portfolio = portfName, initDate = initDate)
stratName <- portfName
strategy(name = stratName, store=TRUE)
SD = 2
N = 20
add.indicator(strategy = stratName, name = "BBands",
arguments = list(HLC = quote(HLC(mktdata)), maType='SMA',
n=N, sd=SD),
label='BBands')
add.signal(strategy = stratName, name="sigCrossover",
arguments=list(columns=c("Close","up"),relationship="gt"),
label="Cl.gt.UpperBand")
add.signal(strategy = stratName, name="sigCrossover",
arguments=list(columns=c("Close","dn"),relationship="lt"),
label="Cl.lt.LowerBand")
add.signal(strategy = stratName, name="sigCrossover",
arguments=list(columns=c("High","mavg"),relationship="gt"),
label="Hi.Cross.Mid")
add.signal(strategy = stratName, name="sigCrossover",
arguments=list(columns=c("Low","mavg"),relationship="lt"),
label="Lo.Cross.Mid")
add.rule(strategy = stratName, name='ruleSignal',
arguments=list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-nShs,
ordertype='market', orderside=NULL, osFUN=osMaxPos
),type='enter',
label = "Enter.Short")
add.rule(strategy = stratName, name='ruleSignal',
arguments=list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty=nShs,
ordertype='market', orderside=NULL, osFUN=osMaxPos
),type='enter',
label = "Enter.Long")
add.rule(strategy = stratName, name='ruleSignal',
arguments=list(sigcol="Hi.Cross.Mid",sigval=TRUE, orderqty= 'all',
ordertype='market', orderside=NULL),type='exit',
label = "Exit.All")
add.rule(strategy = stratName, name='ruleSignal',
arguments=list(sigcol="Lo.Cross.Mid",sigval=TRUE, orderqty= 'all',
ordertype='market', orderside=NULL),type='exit',
label = "Exit.All")
addPosLimit(portfName, symbol, timestamp=initDate, maxpos=maxpos, minpos=0)
.maxpos = seq(3000,8000,1000)
add.distribution(stratName,
paramset.label = 'PosOpt',
component.type = 'order',
component.label = 'addPosLimit',
variable = list(maxpos = .maxpos),
label = 'MaxPos')

Error in FUN(X[[i]], ...) : object 'Year' not found when plotting ordination in ggplot

I am having an issue with the ggplot code line where R doesn't like the "group = Year".
Here is what my data looks like:
> head(data.scores.pa)
NMDS1 NMDS2 NMDS3 Site Year Elevation Fire history
1 -0.737547 0.73473457 0.7575643 BF 2004 1710 Burnt
......
> head(spp.scrs2)
species MDS1 MDS2 pval
1 Acrothamnus.montanus 0.8383 -0.02382347 1e-04
........
> head(vec.sp.df.pa)
MDS1 MDS2 species pvals
Elevation 0.834847 0.747474 Elevation 0.005
Here is the code I am using:
>xy <- ggplot(data.scores.pa, aes(x = NMDS1, y = NMDS2, group = Year)) +
geom_point(size = 3, aes(shape = Fire history, colour = Year))+
stat_ellipse(mapping = NULL, data = NULL, geom = "path", position = "identity", type = "t", level = 0.95, segments = 51, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) +
geom_segment(data=vec.sp.df.pa, aes(x=0,xend=MDS1,y=0,yend=MDS2),
arrow = arrow(length = unit(0.5,"cm")),colour="grey")+
geom_text_repel(data=vec.sp.df.pa,aes(x=MDS1,y=MDS2,label=species),size=2)+
geom_segment(data=spp.scrs2,aes(x=0,xend=MDS1,y=0,yend=MDS2),
arrow = arrow(length = unit(0.5, "cm")),colour="black")+
geom_text_repel(data=spp.scrs2, aes(x=MDS1,y=MDS2,label=species),size=2)+
annotate("text", x = -1.6, y = 1, label = paste0("3D stress: ", format(ord.pa$stress, digits = 4)), hjust = 0) +
theme_cowplot() + scale_color_brewer(palette = "BrBG", direction = 1) +
theme(panel.border = element_rect(colour = "black"))+
ggtitle("All Sites - distance data using Bray-Curtis")+
labs(x = "NMDS1", y = "NMDS2")
> Error in FUN(X[[i]], ...) : object 'Year' not found
However, when I remove the geom_segment and geom_text_repel code lines it fixes the problem and I am able to plot the graph...
Is anyone able to provide some insight into this issue?
Thank you!

I'm trying to project 4D lines to 3D to 2D and clip them at z=p and w=p, but I'm not sure if it's rendering correctly

I've been trying to write a program that can render 4D lines, the specific function doing this gets the lines already rotated, and the function attempts to clip the lines at planes z = p and w = p if needed, and then draw the line to the screen.
I think that I am doing at least most of this properly, however I am unsure, and not having much experience viewing the fourth dimension I cannot tell what might be a visual bug, or what is actually how it should be rendered.
The function first loads a line into two variables, each is one of the two endpoints of the line. If both points are beyond clippl (the clipping plane variable) for z = clippl and w = clippl, it then applies perspective transformation to them, and subsequently renders a line on the screen correspondingly.
If certain logic is met for the points, the function goes through a process of clipping them, and then continues the same as it would outside the clipping planes.
The location of the camera is held in the variables Ox, Oy, Oz, Ow at the beginning of the full program.
I can't tell if I've done this properly, can anyone tell me if this works right as a 4D perspective projection from a first person camera?
EDIT: I've added points to the rendering list that are at the corners of the cube I'm rendering, and it seems to show that there is in fact some problem with the line clipping, as I am fairly certain that the points are rendering properly, and there is not always a line showing up at it. Could the problem have to do with the w = p clip?
Here's the function, the program uses p5.js:
function drawPLines(P){
var lA,lB;
for(var i=0;i<P.length;i++){
lA = [P[i][0],P[i][1],P[i][2],P[i][3]];
lB = [P[i][4],P[i][5],P[i][6],P[i][7]];
//X: ( x*VS+(width*0.5)+(ox*VS) )
//Y: ( y*VS+(height*0.5)+(oy*VS) )
//x: (XV[0]*P[i][0])+(YV[0]*P[i][1])+(ZV[0]*P[i][2])+(WV[0]*P[i][3])
//y: (XV[1]*P[i][0])+(YV[1]*P[i][1])+(ZV[1]*P[i][2])+(WV[1]*P[i][3])
var x0,y0,x1,y1;
//x0 = (XV[0]*lA[0])+(YV[0]*lA[1])+(ZV[0]*lA[2])+(WV[0]*lA[3]);
//y0 = (XV[1]*lA[0])+(YV[1]*lA[1])+(ZV[1]*lA[2])+(WV[1]*lA[3]);
//new rendering pipeline
//old rendering pipeline
if(lA[2]>clippl&&lB[2]>clippl&&lA[3]>clippl&&lB[3]>clippl){
x0 = XV[0]*lA[0];
y0 = YV[1]*lA[1];
x0 = (x0/lA[3])/(lA[2]/lA[3]);
y0 = (y0/lA[3])/(lA[2]/lA[3]);
//console.log(y);
x0 = ( x0*VS+(width*0.5)+(ox*VS) );
y0 = ( y0*VS+(height*0.5)+(oy*VS) );
//x1 = (XV[0]*lB[0])+(YV[0]*lB[1])+(ZV[0]*lB[2])+(WV[0]*lB[3]);
//y1 = (XV[1]*lB[0])+(YV[1]*lB[1])+(ZV[1]*lB[2])+(WV[1]*lB[3]);
x1 = XV[0]*lB[0];
y1 = YV[1]*lB[1];
x1 = (x1/lB[3])/(lB[2]/lB[3]);
y1 = (y1/lB[3])/(lB[2]/lB[3]);
//console.log(y);
x1 = ( x1*VS+(width*0.5)+(ox*VS) );
y1 = ( y1*VS+(height*0.5)+(oy*VS) );
stroke([P[i][8],P[i][9],P[i][10],P[i][11]]);
line(x0,y0,x1,y1);
}else if((lA[2]>clippl||lA[3]>clippl||lB[2]>clippl||lB[3]>clippl)){
var V = 0;
var zV = 0;
var wV = 0;
//var oV = 0;
if(lA[2]>clippl&&lA[3]>clippl){
V++;
}else if(lA[2]>clippl&&lA[3]<=clippl){
zV++;
}else if(lA[2]<=clippl&&lA[3]>clippl){
wV++;
}/*else{
oV++;
}*/
if(lB[2]>clippl&&lB[3]>clippl){
V++;
}else if(lB[2]>clippl&&lB[3]<=clippl){
zV++;
}else if(lB[2]<=clippl&&lB[3]>clippl){
wV++;
}/*else{
oV++;
}*/
if((V==1)||(wV==1&&(V==1||zV==1))||(zV==1&&(V==1||wV==1))){
var lin = lB;
var out = lA;
if(lA[2]<=clippl){
out = lB;
lin = lA;
}
if(lin[2]<=clippl){
lin = [((((lA[0]-lB[0])*clippl)-((lA[0]-lB[0])*lB[2]))/(lA[2]-lB[2]))+lB[0],((((lA[1]-lB[1])*clippl)-((lA[1]-lB[1])*lB[2]))/(lA[2]-lB[2]))+lB[1],clippl,((((lA[3]-lB[3])*clippl)-((lA[3]-lB[3])*lB[2]))/(lA[2]-lB[2]))+lB[3]];
}
if((lA[2]-lB[2])!==0){
lA = lin;
lB = out;
}
lin = lA;
out = lB;
if(lB[3]<=clippl){
out = lA;
lin = lB;
}
if(lin[3]<=clippl){
lin = [((((lA[0]-lB[0])*clippl)-((lA[0]-lB[0])*lB[3]))/(lA[3]-lB[3]))+lB[0],((((lA[1]-lB[1])*clippl)-((lA[1]-lB[1])*lB[3]))/(lA[3]-lB[3]))+lB[1],((((lA[2]-lB[2])*clippl)-((lA[2]-lB[2])*lB[3]))/(lA[3]-lB[3]))+lB[2],clippl];
//alert(lin);
//alert(out);
}
if((lA[3]-lB[3])!==0){
lA = lin;
lB = out;
}
if(lA[2]>clippl||lB[2]>clippl||lA[3]>clippl||lB[3]>clippl){
x0 = XV[0]*lA[0];
y0 = YV[1]*lA[1];
x0 = (x0/lA[3])/(lA[2]/lA[3]);
y0 = (y0/lA[3])/(lA[2]/lA[3]);
//console.log(y);
x0 = ( x0*VS+(width*0.5)+(ox*VS) );
y0 = ( y0*VS+(height*0.5)+(oy*VS) );
//x1 = (XV[0]*lB[0])+(YV[0]*lB[1])+(ZV[0]*lB[2])+(WV[0]*lB[3]);
//y1 = (XV[1]*lB[0])+(YV[1]*lB[1])+(ZV[1]*lB[2])+(WV[1]*lB[3]);
x1 = XV[0]*lB[0];
y1 = YV[1]*lB[1];
x1 = (x1/lB[3])/(lB[2]/lB[3]);
y1 = (y1/lB[3])/(lB[2]/lB[3]);
//console.log(y);
x1 = ( x1*VS+(width*0.5)+(ox*VS) );
y1 = ( y1*VS+(height*0.5)+(oy*VS) );
stroke([P[i][8],P[i][9],P[i][10],P[i][11]]);
line(x0,y0,x1,y1);
}
}
}
}
}
You can see the full program at https://editor.p5js.org/hpestock/sketches/Yfagz4Bz3

Vpython greyscreen crash

I have found many times a solution for my problems from here, but this time I am totally baffled. I don't know what's wrong at my code.
I made a code to create a box with charged particles inside with Vpython. As I launch the program, I get only a grey screen and the program crash. No error message, nothing.
from visual import *
from random import *
def electronizer(num):
list = []
electron_charge = -1.60217662e-19
electron_mass = 9.10938356e-31
for i in range(num):
another_list = []
e = sphere(pos=(random(), random(),random()), radius=2.818e-15,
color=color.cyan)
e.v = vector(random(), random(), random())
another_list.append(e)
another_list.append(e.v)
another_list.append(electron_charge)
another_list.append(electron_mass)
list.append(another_list)
return list
def protonizer(num):
list = []
proton_charge = 1.60217662e-19
proton_mass = 1.6726219e-27
for i in range(num):
another_list = []
p = sphere(pos=(random(), random(),random()), radius=0.8408739e-15, color=color.red)
p.v = vector(random(), random(), random())
another_list.append(p)
another_list.append(p.v)
another_list.append(proton_charge)
another_list.append(proton_mass)
list.append(another_list)
return list
def cross(a, b):
c = vector(a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0])
return c
def positioner(work_list):
k = 8.9875517873681764e3 #Nm2/C2
G = 6.674e-11 # Nm2/kg2
vac_perm = 1.2566370614e-6 # H/m
pi = 3.14159265
dt = 0.1e-3
constant = 1
force = vector(0,0,0)
for i in range(len(work_list)):
for j in range(len(work_list)):
if i != j:
r = work_list[i][0].pos - work_list[j][0].pos
r_mag = mag(r)
r_norm = norm(r)
F = k * ((work_list[i][2] * work_list[j][2]) / (r_mag**2)) * r_norm
force += F
B = constant*(vac_perm / 4*pi) * (cross(work_list[j][2] * work_list[j][1], norm(r)))/r_mag**2
F = cross(work_list[i][2] * work_list[i][1], B)
force += F
F = -(G * work_list[i][3] * work_list[j][3]) / r_mag**2 * r_norm
force += F
acceleration = force / work_list[i][3]
difference_in_velocity = acceleration * dt
work_list[i][1] += difference_in_velocity
difference_in_position = work_list[i][1] * dt
work_list[i][0].pos += difference_in_position
if abs(work_list[i][0].pos[0]) > 2.5e-6:
work_list[i][1][0] = -work_list[i][1][0]
elif abs(work_list[i][0][1]) > 2.5e-6:
work_list[i][1][1] = -work_list[i][1][1]
elif abs(work_list[i][0][2]) > 2.5e-6:
work_list[i][1][2] = -work_list[i][1][2]
return work_list
box = box(pos=(0, 0, 0), length = 5e-6, width = 5e-6, height = 5e-6, opacity = 0.5)
protons_num = raw_input("number of protons: ")
electrons_num = raw_input("number of electrons: ")
list_of_electrons = electronizer(int(electrons_num))
list_of_protons = protonizer(int(protons_num))
work_list = list_of_electrons + list_of_protons
while True:
work_list = positioner(work_list)
You should ask your question on the VPython.org forum where the VPython experts hang out and will be able to answer your question. You should mention which operating system you are using and which version of python you are using. From your code I see that you are using classic VPython. There is a newer version of VPython 7 that just came out but the VPython syntax has changed.