Get coordinates of a list of shapely points - coordinates

I made a list of shapely Point objects
in:
alle_Punkte
out:
[[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>],
[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>],
[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>],
[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>],
[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>],
[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>],
[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>],
[<shapely.geometry.point.Point at 0x218a8447c08>,
<shapely.geometry.point.Point at 0x218a843fcc8>,
<shapely.geometry.point.Point at 0x218a848a108>]]
How can I derive the coordinates of those points?
I tried to get them using alle_Punkte.coords or alle_Punkte.x but this won't work because it's a list.
I need the coordinates to proceed my work in QGIS, so it would be even better to create a shapefile out of this list, but its the sampe problem here.

Related

Matlab matrix minimum value

I have a a matrix Number which contains
0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728
0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304
0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008
0.1900 0.1233 0.0567 0.0100 0.0567 0.1233 0.1900
0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008
0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304
0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728
I am trying to find the minimum value (or values if there are equal mins).
I have tried
[min_val,idx]=min(number);
[row,col]=ind2sub(size(number),idx);
I am getting row 4 which is right but col 1 which clearly not the min, min is in the center.
When I print min(number) I am give the whole of row 4 so I also tried
[min_val,idx]=min(min(number));
[row,col]=ind2sub(size(number),idx);
But i's giving the same result. I'm not really sure what's going on here. Any help would be appreciated!
Code used to get the positions of multiple minimums.
[min_val, idx] = min(number(:));%finds minimum value of n
mins = number==min_val;%logical array that gives 1 where number is at
its minimum
ind1 = zeros();
ind2= zeros();
for i = 1:length(x)
for j = 1:length(y)
if min_val(i,j) == 1
ind1 = [ind1;i];% indcies where mins = 1
ind2 = [ind2;j];
end
end
end
ind1 = ind1(ind1~=0);
Looks like you can use min and ind2sub to achieve your expected output:
matlab:1> number = [0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728; 0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304; 0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008; 0.1900 0.1233 0.0567 0.0100 0.0567 0.1233 0.1900; 0.2008 0.1391 0.0843 0.0567 0.0843 0.1391 0.2008; 0.2304 0.1786 0.1391 0.1233 0.1391 0.1786 0.2304; 0.2728 0.2304 0.2008 0.1900 0.2008 0.2304 0.2728]
number =
0.272800 0.230400 0.200800 0.190000 0.200800 0.230400 0.272800
0.230400 0.178600 0.139100 0.123300 0.139100 0.178600 0.230400
0.200800 0.139100 0.084300 0.056700 0.084300 0.139100 0.200800
0.190000 0.123300 0.056700 0.010000 0.056700 0.123300 0.190000
0.200800 0.139100 0.084300 0.056700 0.084300 0.139100 0.200800
0.230400 0.178600 0.139100 0.123300 0.139100 0.178600 0.230400
0.272800 0.230400 0.200800 0.190000 0.200800 0.230400 0.272800
matlab:2> [min_val, idx] = min(number(:))
min_val = 0.010000
idx = 25
matlab:3> [row, col] = ind2sub(size(number), idx)
row = 4
col = 4

Reshaping 3D array to 2D

Check out following toy example:
m = 3;
n = 3;
Y = rand(m,n,2);
for example gives me
y(:,:,1) =
0.8314 0.3993 0.6569
0.8034 0.5269 0.6280
0.0605 0.4168 0.2920
y(:,:,2) =
0.4317 0.1672 0.1981
0.0155 0.1062 0.4897
0.9841 0.3724 0.3395
now when I reshape it using
reshape(Y,m*n,2)
it disturbs the order and gives me,
0.8314 0.4317
0.8034 0.0155
0.0605 0.9841
0.3993 0.1672
0.5269 0.1062
0.4168 0.3724
0.6569 0.1981
0.6280 0.4897
0.2920 0.3395
because here 2nd row should be
0.3993 0.1672
this can be crosschecked before reshaping by
Y(1,1,:)
Y(1,2,:)
etc.
The order changes.
PS : I have huge data to be fed in Neural network and this affects the way my weights are being multiplied.
Add in permute there and then reshape, like so -
reshape(permute(y,[2,1,3]),[],size(y,3))
Sample run -
>> y
y(:,:,1) =
0.8314 0.3993 0.6569
0.8034 0.5269 0.628
0.0605 0.4168 0.292
y(:,:,2) =
0.4317 0.1672 0.1981
0.0155 0.1062 0.4897
0.9841 0.3724 0.3395
>> reshape(permute(y,[2,1,3]),[],size(y,3))
ans =
0.8314 0.4317
0.3993 0.1672
0.6569 0.1981
0.8034 0.0155
0.5269 0.1062
0.628 0.4897
0.0605 0.9841
0.4168 0.3724
0.292 0.3395

Plotting Peaks function in Octave doesn't produce correct graph

I am trying to use octave to do a data approximation project, but I am running into some trouble testing some basic things. I'm not very experienced with octave, so I was hoping someone could point me in the right direction.
I'm attempting to generate a list of x,y points using the Halton sequence. I want to plug those x,y points into the peaks function to get the z peaks values for those coordinates. Next, I want to plot those x,y,z points to be sure that my plot resembles the peak plot, but my plot looks wrong.
Here is the code:
%13 for this example to compare to meshgrid
N = 13;
%creates a halton sequence for N and scales it between -3 to 3
seq = haltonseq(N, 2) * 6 – 3;
%save the x,y coordinate from the halton sequence
X = seq(:,1);
Y = seq(:,2);
%convert the x,y vector in matrixes
X = repmat(X, 1, N)';
Y = repmat(Y,1,N);
%get the z values for x,y
Z = peaks(X, Y);
%plot my x,y,z values
surf(X,Y,Z);
%plot meshgrid to compare to
[mX mY] = meshgrid(-3:0.5:3);
mZ = peaks(mX, mY);
surf(mX, mY, mZ);
The Halton sequence is correct, but I will include it for verification. This is after it has been scaled to be between -3 and 3:
seq =
0.00000 -1.00000
-1.50000 1.00000
1.50000 -2.33333
-2.25000 -0.33333
0.75000 1.66667
-0.75000 -1.66667
2.25000 0.33333
-2.62500 2.33333
0.37500 -2.77778
-1.12500 -0.77778
1.87500 1.22222
-1.87500 -2.11111
1.12500 -0.11111
Mesh Graph:
Halton Graph N = 13:
Halton Graph N = 300:
Now, to the best of my understanding of this code, the Halton sequence plot should not look like it does, and as I increase N, it should resemble the meshgrid graph more, but it keeps its same deformed shape. I'm not sure where I am going wrong, can someone point me in the right direction?
Note: I am using this code with Octave now, but I will also be using it with Matlab later, so I tagged it as both.
A quick look at your X and Y reveals the problem:
octave:33> X
X =
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
0.00000 -1.50000 1.50000 -2.25000 0.75000 -0.75000 2.25000 -2.62500 0.37500 -1.12500 1.87500 -1.87500 1.12500
octave:34> Y
Y =
-1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000 -1.00000
1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000 1.00000
-2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333 -2.33333
-0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333 -0.33333
1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667 1.66667
-1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667 -1.66667
0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333 0.33333
2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333 2.33333
-2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778 -2.77778
-0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778 -0.77778
1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222 1.22222
-2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111 -2.11111
-0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111 -0.11111
As you can see, they are not sorted properly.
When plotting, order (usually) matters; see below for an explanation.
Adding the following two lines after the repmat fixes it:
X = sort(X,2);
Y = sort(Y,1);
Here's the result:
Explanation
The problem is that, when plotting graphs where lines have to be drawn between points, ordering matters.
To illustrate, consider this small example:
x1 = [1 2 3 4];
x2 = [3 1 4 2];
plot(x1,x1.^2);
plot(x2,x2.^2);
These two will plot the exact same points, but in a different order.
The lines joining them will hence look different:
Plot for x1:
Plot for x2:

What to do with the results of getaddrinfo?

Using getaddrinfo to query a host, I get a number of results:
struct addrinfo hints;
hints.ai_flags = 0
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0
hints.ai_addrlen = 0;
hints.ai_addr = NULL;
hints.ai_canonname= NULL;
hints.ai_next = NULL;
struct addrinfo *res = NULL;
getaddrinfo("google.com", "http", &hints, &res);
res gets filled in with a list of 11 possible addresses I could use to connect to google.com with the given parameters:
1 family=2, addr.port=80, addr.address=74.125.226.98, protocol=6, socktype=1, flags=0
2 family=2, addr.port=80, addr.address=74.125.226.104, protocol=6, socktype=1, flags=0
3 family=2, addr.port=80, addr.address=74.125.226.100, protocol=6, socktype=1, flags=0
4 family=2, addr.port=80, addr.address=74.125.226.99, protocol=6, socktype=1, flags=0
5 family=2, addr.port=80, addr.address=74.125.226.96, protocol=6, socktype=1, flags=0
6 family=2, addr.port=80, addr.address=74.125.226.102, protocol=6, socktype=1, flags=0
7 family=2, addr.port=80, addr.address=74.125.226.110, protocol=6, socktype=1, flags=0
8 family=2, addr.port=80, addr.address=74.125.226.97, protocol=6, socktype=1, flags=0
9 family=2, addr.port=80, addr.address=74.125.226.105, protocol=6, socktype=1, flags=0
10 family=2, addr.port=80, addr.address=74.125.226.103, protocol=6, socktype=1, flags=0
11 family=2, addr.port=80, addr.address=74.125.226.101, protocol=6, socktype=1, flags=0
My question is, which do I use? Should I just always take the first one, or pick one at random? Or should I try each one in order until connect() succeeds?
There is not an officially defined order, but unless you have a good reason not to: try them in order, skipping any that don't match your requirements (eg, wrong protocol). Usually DNS servers will round-robin or somehow permute the results. (You can probably see that if your run your test multiple times in a row or running nslookup or dig a few times on google.com.)
https://en.wikipedia.org/wiki/Round-robin_DNS
Traditionally the first entry is used.
DNS servers make use of this to balance the load between multiple servers, that is they return a different order each time. So you should pick the first one and continue from there until you get a successful connection.
You should loop through them, one at a time in order, trying to connect() to each one until either one succeeds or you exhaust the list.

Matlab and then c implementation: Smooth data

I have some data that is slightly noisy. I want to clean it as well as the derivative. A very simple "filter" such as Y(n)=0.2*X(n)+0.8*Y(n-1) suits me well: in other words, the output is smooth enough. Here is an example with both data and its derivative:
Nevertheless, as it's real-time, I cannot accept a big lag in data. For the derivative, the lag is around 10 iterations: 5 iterations (to get the filtered data) and then another 5 iterations (to get the filtered derivative data).
How can I implement an algorithm in Matlab - in fine, I need a c implementation - that produces a similar result but with very little lag, such as a maximum of 3 iterations EVEN and ESPECIALLY for the derivative.
Here is the Matlab code:
%Here is data input;
data = [5554 5767 5737 5828 6011 6011 6103 6225 6408 6286 6347 6439 6591 6683 6530 6652 6805 7019 7080 7019 6988 6988 6958 6958 6927 6896 6866 6805 6835 6713 6591 6713 6713 6652 6713 6713 6835 6896 6744 6622 6683 6561 6591 6591 6622 6561 6500 6652 6774 6805 6958 7049 7171 7232 7293 7293 7415 7476 7690 8087 8056 7965 8178 8270 8148 8178 8392 8239 8056 8026 7873 7873 7476 7446 7476 7293 7171 7141 7263 7019 7141 6774 6652 6530 6530 6469 6561 6622 6652 6713 6866 6958 6958 7110 6958 7049 6805 6866 6866 6744 7049 6805 6652 6530 6622 6744 6408 6652 6683 6713 6713 6622 6744 6805 6713 6683 6805 6835 6927 6866 6805 6744 7049 7232 7171 7141 7202 7354 7324 7415 7415 7415 7965 8178 8178 8178 8178 7995 7995 7965 7843 7904 7904 7812 7812 7720 7690 7843 7873 7904 8056 7904 7843 7934 7873 7873 7781 7781 7873 7873 7873 7690 7659 7690 7720 7751 7659 7598 7629 7659 7568 7537 7629 7537 7659 7568 7568 7476 7568 7598 7354 7324 7476 7354 7385 7202 7293 7293 6958 6866 6866 6683 6927 6927 6927 7019 7019 7049 7141 7202 7293 7385 7568 7446 7446 7415 7354 7263 7293 7202 7202 7202 7232 7202 7263 7141 7171 7019 7141 7232 7080 7232 7141 7263 6958 7019 7019 6866 6896 6744 6774 6805 6835 6866 6774 6744 6774 6744 6744 6866 6958 6958 7019 6988 6988 6896 6958 7080 7080 7171 7232 7202 7354 7385 7385 7476 7720 7690 7598 7659 7659 7690 7720 7720 7690 7690 7781 7751 7659 7812 7873 7659 7629 7446 7446 7629 7354 7415 7476 7568 7415 7385 7385 7324 7202 7324 7110 6958 7019 7019 7049 6774 7202 6561 6408 6195 5920 5920 5920 5859 5615 5645 5584 5676 5432 5401 5126 5096 5004 5004 4791 4852 4791 4791 4730 4760 4730 4730 4699 4760 4669 4730 4760 4760 4791 4882 4882 4882 4974 4943 4974 5065 5096 5157 5218 5279 5279]';
%Define smooth coefficient
c=0.2;
%Filter data
data_filtered=zeros(size(data),1);
for n=drange(2:size(data))
data_filtered(n,1)=c*data(n)+(1-c)*data_filtered(n-1,1);
end
%Graph both data and filtered data
subplot(2,1,1);
plot([1:size(data)], data, [1:size(data)], data_filtered);
%Calculate derivative data. Delta time is constant.
for n=drange(2:size(data_filtered))
data_derivative(n,1)=data(n)-data(n-1,1);
end
%Calculate derivative based on filtered data. Delta time is constant.
for n=drange(2:size(data_filtered))
data_filtered_derivative(n,1)=data_filtered(n)-data_filtered(n-1,1);
end
%Filter derivative (which is based on filtered data)
data_filtered_derivative_filtered=data_filtered_derivative;
for n=drange(2:size(data_filtered_derivative))
data_filtered_derivative_filtered(n,1)=c*data_filtered_derivative(n)+(1-c)*data_filtered_derivative_filtered(n-1,1);
end
%Graph both derivative data and filtered derivative data
subplot(2,1,2);
plot([1:size(data_derivative)], data_derivative, [1:size(data_derivative)], data_filtered_derivative_filtered);
If you want an excellent, low-lag filter it seems something like Jurik JMA might be for you. It is probably pretty expensive (and definitely not free) but the paper might give you ideas on other (possibly free) algorithms.
I have not tested that algorithm, I have just found it and liked the pictures.