Merge multiple geometry lines into a single geometry line - postgresql

Now I have 3 lines (LINESTRING) with 3 different geometry values.
Line#1 : "11AABB"
Line#2 : "22CCDD"
Line#3 : "33EEFF"
How to merge these lines into a single line via PostGIS function ?
I use to know that It use ST_LineMerge or ST_Union But I'm not sure how to use it with geometry values.

could you be more specific?
with the examples you provided you could try this:
SELECT ST_Union('11AABB'::geometry , ST_Union('22CCDD'::geometry,'33EEFF'::geometry ) );
your geometry values are invalid, but this is the correct sintax of the query you want. You should get this error with the example you provided: "ERROR: parse error - invalid geometry"

Related

Sum columns of a dataset

I'm working in PySpark and I have a dataset like this :
I want to create a new df like this with the corresponding sums :
So I tried this code :
df = df.withColumnRenamed("month_actual_january", "monthjanuary")
fin=df.groupBy(["column1","column2"]).sum()
The problem is that I get the following error :
Attribute sum(column3) contains an invalid character among ,;{}()\n\t=. Please use an alias to rename it
Do you know how to fix this error ? Thanks !
Lets try use least squares to pass a wild card alias as follows
df.groupBy(["column1","column2"]).agg(*[sum(x).alias(f"sum_{x}") for x in df.drop("column1","column2").columns]).show()

Postgis: function st_contains does not exist

I am struggling to get a simple Postgresql/Postgis statement to work, I need all points within a polygon (in this case a rectangle)
SELECT * FROM points_table WHERE ST_Contains( ST_GEOMFROMTEXT('POLYGON((51.8121, 0.13712199999997665, 51.9078, 0.21444399999995767))'), points_table.geom)
The error reads
ERROR: function st_contains(geometry, geography) does not exist
LINE 1: SELECT * FROM points_table WHERE ST_Contains( ST_GEOMFRO...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function st_contains(geometry, geography) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 38
The answer from this question amongst others suggests my statement is correct.
Seems you are comparing GEOMETRY and GEOGRAPHY
As in your message error st_contains(geometry, geography)
be sure that your column points_table.geom is a valid GEOMETRY data type and not a GEOGRAPHY data type .. for this chek also for the SR you are using and eventually convert you geomtext as a valid SR for geography
eg assuming you using as SR 4326
SELECT *
FROM points_table
WHERE ST_Contains(
ST_Transform(
ST_GEOMFROMTEXT('POLYGON((51.8121, 0.13712199999997665, 51.9078, 0.21444399999995767))')
,4326)
, points_table.geom)
Although I accepted an answer that solved my statement, I feel it important to share an alternative to the explicit scenario (points within a boundary) as this is something I have struggled to find a solution to elsewhere.
SELECT * FROM points WHERE points.geom && ST_MakeEnvelope(0.13712199999997665, 51.8121, 0.26340800000002673, 51.9135, 4326)
This one is far simpler and yields the results exactly as required, same as the accepted answer.
I solved this problem by joining on public.ST_CONTAINS(geom, coords) instead of ST_CONTAINS(geom, coords)
I don't really understand enough about Postgresql to know why this worked though. I just skimmed https://gis.stackexchange.com/questions/132103/postgis-st-within-does-not-exist and hoped for the best.

Postgres LIKE %% syntax errors

I have been trying to do a LIKE comparison in postgres but repeatedly receive an error telling me that the column "%#firstname%" doesn't exist.
I should clarify, this query is executed in a function, "#firstname" is the parameter passed into the function.
The relevant section of the query is below:
WHERE u."firstname" LIKE "%#firstname%"
I do not want an exact comparison which is why I am trying to add the %% to the query. It works just fine without them for exact queries. Whenever, I add the % then it assumes that they are part of the variable name and subsequently can't find it.
I have also tried the following:
'%"#firstname"%' which results in an empty array being returned even though it should have matched
"%'#firstname'%" which results in error: column "%'#filter'%" does not exist
%"#firstname"% which results in error: column "%'#filter'%" does not exist
If "#firstname" is a parameter you need something like:
WHERE u.firstname LIKE concat('%', "#firstname", '%');

Use PostGIS to retrieve the nearby points

I've tried to use PostGIS to retrieve the nearby points, however,
I've gotten an error message from the pgAdmin3.
Can you help me to debug the SQL queries (Postgresql+PostGIS) below?
Thank you for your kindness help.
I've used the 3826 geometry.
"ERROR: parse error - invalid geometry
HINT: "...79721.29349234176 2759680.13418412))" <-- parse error at position 44 within geometry" in the
SELECT * FROM pointslight
WHERE ST_DWithin(
ST_Transform(ST_GeomFromText('POINT(279721.29349234176 2759680.13418412))',3826),26986),
ST_Transform(location,26986), 50)
ORDER BY ST_Distance(ST_GeomFromText('POINT(279721.29349234176 2759680.13418412))',3826), location);
In the string defining the point, you have one opening parenthesis an two closing one. Remove one of the latter!

search a name in dataset error :Undefined function 'eq' for input arguments of type 'cell'

I load a file which has some columns with data.The first line contains ,CITY,YEAR2000 .
The first column has name of cities.The other columns contain number data.
I am trying to search for a specific city using:
data(data.CITY=='Athens',3:end)
where
data = dataset('File','cities.txt','Delimiter',',')
but I receive an error
Undefined function 'eq' for input arguments of type 'cell'.
--------UPDATE-----------------------------
Ok, use :
data(find(strncmp(data.CITY,'Athens',length('Athens'))),3:end)
Have you tried with using strncmp tangled with find?
I would use it this way
find(strncmp(data.CITY,'ATHENS',length('ATHENS')))
EDIT
Other opportunities to exploit would encompass strfind
strfind(data.CITY,'ATHENS')
EDIT 2
You could also try with
data(ismember(data.CITY,'ATHENS'),3:end)
This should lead you to the results you expect (at least I guess so).
EDIT 3
Given your last request I would go for this solution:
inp = input('Name of the CITY: ','s')
Name of the City: ATHENS
data(find(strncmp(data.CITY,inp,length(inp))),3:end)