PostGis Erro invalid geometry - postgresql

I'm using POSTGIS="2.4" and Postgresql 9.6 and facing following error
While trying to insert polygon data
INSERT INTO aalis.mv_l1_parcelownership_aalis (geometry) VALUES
(st_Polygonfromtext ('polygon(482449.20552234241,
999758.79058533313,.....)',20137));

You're close :-)
The geometry you provided in your insert statement is invalid. Make sure that your POLYGONS are really correct and try one of these statements (using ST_GeomFromText or ST_PolygonFromText):
INSERT INTO aalis.mv_l1_parcelownership_aalis
VALUES (ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',20137));
or
INSERT INTO aalis.mv_l1_parcelownership_aalis
VALUES (ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',20137));
To check if your geometries are correct you can use ST_IsValid:
SELECT ST_IsValid(ST_GeomFromText('POLYGON((0 0, 1 1, 1 2, 1 1, 0 0))'));
HINWEIS: Self-intersection at or near point 0 0
st_isvalid
------------
f
(1 Zeile)
SELECT ST_IsValid(ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'));
st_isvalid
------------
t
(1 Zeile)
Keep in mind also that the WKT standard sort of expects double parenthesis (( for polygons with 0 interior rings, and yours has only one: 'polygon(482449.20552234241, 999758.79058533313,.....). Also, the x and y axes are separated by space, not by comma. Commas separate coordinate pairs instead.
Example:
SELECT ST_IsValid('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))');
st_isvalid
------------
t
(1 Zeile)
SELECT ST_IsValid('POLYGON(30 10, 40 40, 20 40, 10 20, 30 10)');
FEHLER: parse error - invalid geometry
ZEILE 1: SELECT ST_IsValid('POLYGON(30 10, 40 40, 20 40, 10 20, 30 10...
^
TIP: "POLYGON(30 " <-- parse error at position 11 within geometry

Your polygon text is way off, and includes the characters ....., which are not valid:
polygon(482449.20552234241, 999758.79058533313,.....)
Not sure what your coordinates are, but the polygon text is generally in the form:
polygon((1.000 1.000, 2.000 1.500, 3.000 2.000, 1.000 1.000))
Note that the x-y pairs are in the form x y, and there are commas between the pairs.

Related

How to find all the rows that share the same value on two columns?

Dataset example:
sex favourite_meal favourite_color age weight(kg)
Tom M pizza red 18 90
Jess F lasagna blue 20 43
Mark M pizza red 30 68
David M hamburger purple 25 70
Lucy F sushi green 18 47
How can I compare each row with the others and find which one share for example the same (sex,favourite_meal) couple. The idea is to check on a large dataset which rows share the same values on two attributes (columns). In this example would be Tom and Mark which share (M, pizza); how to do the same on a large dataset where you can't check by eye?
One awk option is process the source data twice. First get the count of uniq values in columns 2 and 3 into an array. Then use those counts to filter the data:
awk 'NR==FNR {p[$2" "$3]++} FNR<NR {for(n in p) if (p[n]>1 && $2" "$3==n) { print}}' m.dat m.dat
Tom M pizza red 18 90
Mark M pizza red 30 68
you can use pandas to do this
import pandas as pd
# Initialize data to Dicts of series.
d = {'Name': pd.Series(['Tom', 'Jess', 'Mark', 'David', 'Lucy']),
'sex': pd.Series(['M', 'F', 'M', 'M', 'F']),
'favorite_meal': pd.Series(['pizza', 'lasanga', 'pizza', 'hamburger', 'sushi']),
'favorite_color': pd.Series(['red', 'blue', 'red', 'purple', 'green']),
'age': pd.Series([18, 20, 30, 20, 18]),
' weights(kg)': pd.Series([90, 43, 68, 70, 47])
}
df = pd.DataFrame(d)
for y, x in df.groupby(['favorite_meal', 'sex']):
print("....................")
print(x.to_string(index=0, header=0))
In each iteration, the for loop is operating on a set of similar rows.

How to use textscan to reading this txt file matlab/octave

How to use textscan to reading this txt file matlab/octave
Time:
11:00
Day:
2019-11-05
Company:
Hyperdrones
Drones:
Jupiter, alvalade, 20, 2000, 500.0, 20.0, 2019-11-05, 10:15
Terra, ameixoeira, 15, 1500, 400.0, 20.0, 2019-11-05, 10:20
V125, ameixoeira, 20, 2000, 350.0, 20.0, 2019-11-05, 10:20
Saturno, lumiar, 10, 1000, 600.0, 20.0, 2019-11-05, 10:30
Neptuno, lumiar, 15, 1500, 600.0, 15.0, 2019-11-05, 10:30
Mercurio, alvalade, 25, 2500, 200.0, 20.0, 2019-11-05, 10:40
Marte, campogrande, 10, 1500, 100.0, 10.0, 2019-11-05, 10:50
Regarding the Octave version specifically, I would recommend something like this:
pkg load io % required for `csv2cell` function
tmp = fileread('data'); % read file as string
tmp = strsplit( tmp, '\n' ); % split on line endings to create rows
tmp = tmp(1:6); % keep only rows 1 to 6
Headers = struct( tmp{:} ); % create struct from comma-separated-list
Headers.Drones = csv2cell('data', 7) % use csv2cell to read csv starting from row 7
Result:
Headers =
scalar structure containing the fields:
Time:: 1x5 sq_string
Day:: 1x10 sq_string
Company:: 1x11 sq_string
Drones: 8x8 cell
Matlab does not come with an equivalent csv2cell function, but there are similar ones on matlab fileexchange; here's one that seems to have similar syntax as the octave version.
In general I'd use csv2cell for non-numeric csv data of this nature; it's much easier to work with than textscan. I'd only use textscan as a last resort for non-csv files with lines that are unusual but otherwise consistent in some way...
PS. If your csv file ends with an empty line, csv2cell might result in an extra 'empty' cell row. Remove this manually if you don't want it.
Assuming you want to read the matrix portion:
C = textscan(fileID, '%s %s %d %d %f %f %{yyyy-dd-MM}D %{HH:mm}D','HeaderLines',7,'Delimiter,',')
The 7 assumes there aren't really blank lines in your file. If you have blank lines, adjust that to 14 or whatever is appropriate).

How to find the row where a value is higher than a number and plot its coordinate?

If I have this matrix M:
M =[0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90
5 0.284491788 0.312419395 0.327207952 0.334946026 0.339755602 0.342395525 0.344233852 0.345327586 0.346104243 0.34657095 0.346777584 0.346927215 0.347005593 0.347080409 0.347137411 0.347158787 0.347180163 0.347190851
10 0.300057002 0.3301507 0.34627525 0.354729417 0.359959386 0.362862945 0.364882967 0.366055079 0.366878051 0.367362571 0.367583455 0.367754462 0.367839966 0.367925469 0.367986034 0.36800741 0.368028786 0.368039474
15 0.315522463 0.348113577 0.365474367 0.374605437 0.380323489 0.383497809 0.385653212 0.386942891 0.387840678 0.388360825 0.388606648 0.388816844 0.388920161 0.38901279 0.389076918 0.389098293 0.389119669 0.389130357
20 0.331119741 0.366108518 0.384972746 0.39493035 0.401193487 0.404656382 0.406986355 0.408386476 0.409366205 0.409914853 0.410192739 0.410417186 0.410527628 0.410627383 0.41069151 0.410716449 0.410737825 0.410748513
25 0.347347608 0.384858741 0.405034023 0.415832413 0.422576508 0.42631729 0.428864584 0.430410773 0.431440379 0.432053155 0.432352417 0.432580427 0.432708682 0.432811999 0.432883252 0.432908191 0.432929566 0.432940254
30 0.362410488 0.40227297 0.423944565 0.435523175 0.442766041 0.44679896 0.449577826 0.451245146 0.452324629 0.452990844 0.453318608 0.453553743 0.453681998 0.453796003 0.453870818 0.453895757 0.453924258 0.453934946
35 0.376946097 0.419277495 0.442399088 0.454836296 0.462656311 0.466949304 0.469924116 0.47170544 0.472877552 0.473575831 0.473928533 0.474181481 0.474316862 0.474430867 0.474509245 0.474537746 0.474566247 0.474576935
40 0.391574335 0.436360398 0.460981866 0.47432755 0.482721151 0.487341907 0.490491289 0.492386619 0.493615733 0.494395953 0.494762906 0.495019416 0.495179736 0.495304428 0.495389932 0.495418433 0.495446934 0.495464748
45 0.406220385 0.453646371 0.479924472 0.494110941 0.503035377 0.507941145 0.511293598 0.513320745 0.514656739 0.515483273 0.515885853 0.516167302 0.516334747 0.516459439 0.516552068 0.516580569 0.516609071 0.516626884
50 0.420232285 0.470177064 0.497937226 0.513060672 0.522508818 0.527735224 0.531251559 0.533396273 0.534810645 0.53569062 0.536107449 0.536399587 0.53659197 0.53672735 0.536819979 0.536848481 0.536876982 0.536894795
55 0.434824896 0.487441662 0.516933272 0.533000819 0.542951299 0.548448466 0.552196373 0.554465781 0.555937155 0.556867006 0.557319463 0.557640101 0.557850297 0.557992803 0.558085432 0.558113934 0.558142435 0.558160248
60 0.448729916 0.504029356 0.535195411 0.552171435 0.562738252 0.568559621 0.572517724 0.57491895 0.576468702 0.577441305 0.5779187 0.578253589 0.578470911 0.578620542 0.578713171 0.578741672 0.578770174 0.578787987
65 0.462649186 0.520620613 0.553350672 0.571245858 0.582375574 0.588524707 0.592693007 0.595272365 0.59689337 0.597926538 0.598435997 0.598788699 0.599013146 0.59916634 0.599262532 0.599291033 0.599319534 0.599337347
70 0.475371406 0.535943568 0.57038726 0.589212298 0.600933414 0.607378246 0.611746054 0.614475044 0.61617799 0.617257473 0.61779187 0.618158823 0.618390395 0.618550714 0.618650468 0.61869322 0.618721721 0.618739535
75 0.488100752 0.551262959 0.587185151 0.606875913 0.619220492 0.625964587 0.630610282 0.633467526 0.635277352 0.636410275 0.636973173 0.637343689 0.637585949 0.637756956 0.637860273 0.63791015 0.637938651 0.637956464
80 0.499832555 0.565477929 0.602782429 0.623342477 0.636278457 0.643282625 0.648145641 0.651113328 0.653005095 0.654216395 0.654804232 0.655188999 0.655452635 0.65563433 0.655741209 0.655794649 0.65582315 0.655844526
85 0.511222345 0.579325947 0.618169511 0.639670099 0.653136913 0.660465282 0.665520681 0.668634437 0.670593894 0.671844383 0.67247141 0.672877552 0.673148313 0.673340696 0.673451138 0.673504578 0.673533079 0.673554455
90 0.522344936 0.592785635 0.632936692 0.655249564 0.669208023 0.676910471 0.682186754 0.685414514 0.687487976 0.688788343 0.689450996 0.689864263 0.69014215 0.690348783 0.690459225 0.690512665 0.690541166 0.690566105];
we are going to plot only [,1] and [1,] values:
the idea is to find the rows of the columns from 2 to 19 (i,2:19) where the value is higher than 0.4 and plot the the rows which match the condition agains its respective column.
The values to select the rows would be like:
The results would be like:
rows = [45 30 25 25 20 20 20 20 20 20 20 20 20 20 20 20 20 20];
col = [5:5:90];
plot (rows, col);
Assuming that each column is in ascending sorted order that you see in your matrix, you can threshold the matrix beyond 0.4, then find the first index in each column that surpasses this threshold. You can use the max function in MATLAB to help you do that. You can thus locate the row location that surpasses this threshold for each column, skipping the first column and we also skip the first row. You can then use this to index into the first row and finally plot your data. Note that you'll have to offset your indices by 1 because we skipped the first row, first column:
[~,ind] = max(M(2:end,2:end) > 0.4, [], 1);
rows = M(1, ind + 1);
With your example, we thus get:
>> format compact
>> rows
ans =
45 30 25 25 20 20 20 20 20 20 20 20 20 20 20 20 20 20
This agrees with your expected result.

Difference between "enqueue" and "dequeue"

Can somebody please explain the main differences? I don't have a clear knowledge about these functions in programming for any language.
Some of the basic data structures in programming languages such as C and C++ are stacks and queues.
The stack data structure follows the "First In Last Out" policy (FILO) where the first element inserted or "pushed" into a stack is the last element that is removed or "popped" from the stack.
Similarly, a queue data structure follows a "First In First Out" policy (as in the case of a normal queue when we stand in line at the counter), where the first element is pushed into the queue or "Enqueued" and the same element when it has to be removed from the queue is "Dequeued".
This is quite similar to push and pop in a stack, but the terms enqueue and dequeue avoid confusion as to whether the data structure in use is a stack or a queue.
Class coders has a simple program to demonstrate the enqueue and dequeue process. You could check it out for reference.
http://classcoders.blogspot.in/2012/01/enque-and-deque-in-c.html
Enqueue and Dequeue tend to be operations on a queue, a data structure that does exactly what it sounds like it does.
You enqueue items at one end and dequeue at the other, just like a line of people queuing up for tickets to the latest Taylor Swift concert (I was originally going to say Billy Joel but that would date me severely).
There are variations of queues such as double-ended ones where you can enqueue and dequeue at either end but the vast majority would be the simpler form:
+---+---+---+
enqueue -> | 3 | 2 | 1 | -> dequeue
+---+---+---+
That diagram shows a queue where you've enqueued the numbers 1, 2 and 3 in that order, without yet dequeuing any.
By way of example, here's some Python code that shows a simplistic queue in action, with enqueue and dequeue functions. Were it more serious code, it would be implemented as a class but it should be enough to illustrate the workings:
import random
def enqueue(lst, itm):
lst.append(itm) # Just add item to end of list.
return lst # And return list (for consistency with dequeue).
def dequeue(lst):
itm = lst[0] # Grab the first item in list.
lst = lst[1:] # Change list to remove first item.
return (itm, lst) # Then return item and new list.
# Test harness. Start with empty queue.
myList = []
# Enqueue or dequeue a bit, with latter having probability of 10%.
for _ in range(15):
if random.randint(0, 9) == 0 and len(myList) > 0:
(itm, myList) = dequeue(myList)
print(f"Dequeued {itm} to give {myList}")
else:
itm = 10 * random.randint(1, 9)
myList = enqueue(myList, itm)
print(f"Enqueued {itm} to give {myList}")
# Now dequeue remainder of list.
print("========")
while len(myList) > 0:
(itm, myList) = dequeue(myList)
print(f"Dequeued {itm} to give {myList}")
A sample run of that shows it in operation:
Enqueued 70 to give [70]
Enqueued 20 to give [70, 20]
Enqueued 40 to give [70, 20, 40]
Enqueued 50 to give [70, 20, 40, 50]
Dequeued 70 to give [20, 40, 50]
Enqueued 20 to give [20, 40, 50, 20]
Enqueued 30 to give [20, 40, 50, 20, 30]
Enqueued 20 to give [20, 40, 50, 20, 30, 20]
Enqueued 70 to give [20, 40, 50, 20, 30, 20, 70]
Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20]
Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20, 20]
Dequeued 20 to give [40, 50, 20, 30, 20, 70, 20, 20]
Enqueued 80 to give [40, 50, 20, 30, 20, 70, 20, 20, 80]
Dequeued 40 to give [50, 20, 30, 20, 70, 20, 20, 80]
Enqueued 90 to give [50, 20, 30, 20, 70, 20, 20, 80, 90]
========
Dequeued 50 to give [20, 30, 20, 70, 20, 20, 80, 90]
Dequeued 20 to give [30, 20, 70, 20, 20, 80, 90]
Dequeued 30 to give [20, 70, 20, 20, 80, 90]
Dequeued 20 to give [70, 20, 20, 80, 90]
Dequeued 70 to give [20, 20, 80, 90]
Dequeued 20 to give [20, 80, 90]
Dequeued 20 to give [80, 90]
Dequeued 80 to give [90]
Dequeued 90 to give []
These are terms usually used when describing a "FIFO" queue, that is "first in, first out". This works like a line. You decide to go to the movies. There is a long line to buy tickets, you decide to get into the queue to buy tickets, that is "Enqueue". at some point you are at the front of the line, and you get to buy a ticket, at which point you leave the line, that is "Dequeue".
A queue is a certain 2-sided data structure. You can add new elements on one side, and remove elements from the other side (as opposed to a stack that has only one side). Enqueue means to add an element, dequeue to remove an element. Please have a look here.
Enqueue means to add an element, dequeue to remove an element.
var stackInput= []; // First stack
var stackOutput= []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
return stackInput.push(item);
}
function dequeue(stackInput, stackOutput) {
// Reverse the stack such that the first element of the output stack is the
// last element of the input stack. After that, pop the top of the output to
// get the first element that was ever pushed into the input stack
if (stackOutput.length <= 0) {
while(stackInput.length > 0) {
var elementToOutput = stackInput.pop();
stackOutput.push(elementToOutput);
}
}
return stackOutput.pop();
}
In my opinion one of the worst chosen word's to describe the process, as it is not related to anything in real-life or similar. In general the word "queue" is very bad as if pronounced, it sounds like the English character "q". See the inefficiency here?
enqueue: to place something into a queue; to add an element to the tail of a queue;
dequeue to take something out of a queue; to remove the first available element from the head of a queue
source: https://www.thefreedictionary.com

How to calculate a Mod b in Casio fx-991ES calculator

Does anyone know how to calculate a Mod b in Casio fx-991ES Calculator. Thanks
This calculator does not have any modulo function. However there is quite simple way how to compute modulo using display mode ab/c (instead of traditional d/c).
How to switch display mode to ab/c:
Go to settings (Shift + Mode).
Press arrow down (to view more settings).
Select ab/c (number 1).
Now do your calculation (in comp mode), like 50 / 3 and you will see 16 2/3, thus, mod is 2. Or try 54 / 7 which is 7 5/7 (mod is 5).
If you don't see any fraction then the mod is 0 like 50 / 5 = 10 (mod is 0).
The remainder fraction is shown in reduced form, so 60 / 8 will result in 7 1/2. Remainder is 1/2 which is 4/8 so mod is 4.
EDIT:
As #lawal correctly pointed out, this method is a little bit tricky for negative numbers because the sign of the result would be negative.
For example -121 / 26 = -4 17/26, thus, mod is -17 which is +9 in mod 26. Alternatively you can add the modulo base to the computation for negative numbers: -121 / 26 + 26 = 21 9/26 (mod is 9).
EDIT2: As #simpatico pointed out, this method will not work for numbers that are out of calculator's precision. If you want to compute say 200^5 mod 391 then some tricks from algebra are needed. For example, using rule
(A * B) mod C = ((A mod C) * B) mod C we can write:
200^5 mod 391 = (200^3 * 200^2) mod 391 = ((200^3 mod 391) * 200^2) mod 391 = 98
As far as I know, that calculator does not offer mod functions.
You can however computer it by hand in a fairly straightforward manner.
Ex.
(1)50 mod 3
(2)50/3 = 16.66666667
(3)16.66666667 - 16 = 0.66666667
(4)0.66666667 * 3 = 2
Therefore 50 mod 3 = 2
Things to Note:
On line 3, we got the "minus 16" by looking at the result from line (2) and ignoring everything after the decimal. The 3 in line (4) is the same 3 from line (1).
Hope that Helped.
Edit
As a result of some trials you may get x.99991 which you will then round up to the number x+1.
You need 10 ÷R 3 = 1
This will display both the reminder and the quoitent
÷R
There is a switch a^b/c
If you want to calculate
491 mod 12
then enter 491 press a^b/c then enter 12. Then you will get 40, 11, 12. Here the middle one will be the answer that is 11.
Similarly if you want to calculate 41 mod 12 then find 41 a^b/c 12. You will get 3, 5, 12 and the answer is 5 (the middle one). The mod is always the middle value.
You can calculate A mod B (for positive numbers) using this:
Pol( -Rec( 1/2πr , 2πr × A/B ) , Y ) ( πr - Y ) B
Then press [CALC], and enter your values for A and B, and any value for Y.
/ indicates using the fraction key, and r means radians ( [SHIFT] [Ans] [2] )
type normal division first and then type shift + S->d
Here's how I usually do it. For example, to calculate 1717 mod 2:
Take 1717 / 2. The answer is 858.5
Now take 858 and multiply it by the mod (2) to get 1716
Finally, subtract the original number (1717) minus the number you got from the previous step (1716) -- 1717-1716=1.
So 1717 mod 2 is 1.
To sum this up all you have to do is multiply the numbers before the decimal point with the mod then subtract it from the original number.
Note: Math error means a mod m = 0
It all falls back to the definition of modulus: It is the remainder, for example, 7 mod 3 = 1.
This because 7 = 3(2) + 1, in which 1 is the remainder.
To do this process on a simple calculator do the following:
Take the dividend (7) and divide by the divisor (3), note the answer and discard all the decimals -> example 7/3 = 2.3333333, only worry about the 2. Now multiply this number by the divisor (3) and subtract the resulting number from the original dividend.
so 2*3 = 6, and 7 - 6 = 1, thus 1 is 7mod3
Calculate x/y (your actual numbers here), and press a b/c key, which is 3rd one below Shift key.
Simply just divide the numbers, it gives yuh the decimal format and even the numerical format. using S<->D
For example: 11/3 gives you 3.666667 and 3 2/3 (Swap using S<->D).
Here the '2' from 2/3 is your mod value.
Similarly 18/6 gives you 14.833333 and 14 5/6 (Swap using S<->D).
Here the '5' from 5/6 is your mod value.