PostgreSQL - Geography shape becomes invalid - postgresql

I have a geography that looks like this in geojson.io:
I have a geography column in DB, which stores the above geography that looks like this in pgAdmin:
As you can see in the pgAdmin image, it has created invalid geography by connecting the lines in between.
Is there any way to fix this issue or I will need to recreate the geography to somehow it do not create such an invalid geography shape?

Related

How to change geometry type from LINESTRING to MULTILINESTRING in a Postgresql database layer in QGIS

I merged lines in a shapefile layer and need to copy them to a postgreSQL database layer. If I paste them to the pg db layer, it does not work. If I merge them, after I pasted to QGIS, it crashes. I figured out that it works, if I export the pg db layer to a geopackage or shapefile layer and set the geometry from linestring to multilinestring. The problem is that I need it to bring to the data base layer. Anyone can give me a hint how I configure the db layer in the right way?
Thank you!
I would change the geometry type in PostgreSQL not in the geopackage or shapefile. The problem seems to be in the geometry type column. You could use this to change it:
ALTER TABLE [table_name]
ALTER COLUMN [geometry_column] TYPE geometry(MULTILINESTRING) USING ST_Multi([geometry_column]);
OR
You could import in PostgreSQL the lines without merging them and to that in with postgis while altering the geometry type of the column:
ALTER TABLE [table_name]
ALTER COLUMN [geometry_column] TYPE geometry(MULTILINESTRING) USING ST_Union([geometry_column]);
Also, this might help you:
https://gis.stackexchange.com/questions/68071/how-to-create-a-multilinestring-feature-with-a-postgis-layer-in-qgis

Why do I get different geometry value when using st_transform vs changing the table SRID?

I have a table "people" with SRID set to 4326 for the geometry column in the "geometry_columns" postgis view.
The value of the geometry column for my record X is:
"0101000020E6100000000000000080404000000000008040C0"
I know I can change the SRID (e.g. from 4326 to 3003) of that geometry column by updating it in the mentioned view, as follows:
ALTER TABLE people
ALTER COLUMN geometry_column TYPE geometry(POINT, 3003)
USING ST_SetSRID(geometry_column,3003);
The value of the geometry column for my record X is now:
0101000020BB0B0000000000000080404000000000008040C0
Why do I get a different value in the geometry column by doing so compared to extracting (with TS_TRANSFORM) the transformed geometry like in the query below while keeping SRID to 4326?
SELECT St_transform(geometry_column,3003) FROM people;
The result is the following and it differs from the previous one while I expected it identical:
0101000020BB0B0000328D4934B2BF4C413783531F94E74DC1
In the ALTER TABLE statement you have to transform the geometry, not just set another SRS. In your code you're simply "changing" the SRID of the coordinate, which means that the coordinate pair itself remains unchanged. The only way to transform coordinates from one SRS to another is to use ST_Transform:
ALTER TABLE people
ALTER COLUMN geometry_column TYPE geometry(POINT, 3003)
USING ST_Transform(geometry_column,3003);
Demo: db<>fiddle
CREATE TABLE people (geometry_column geometry(point,4326));
INSERT INTO people VALUES ('SRID=4326;POINT(33 -33)');
ALTER TABLE people
ALTER COLUMN geometry_column TYPE geometry(POINT, 3003)
USING ST_Transform(geometry_column,3003);
SELECT ST_AsEWKT(geometry_column) FROM people;
st_asewkt
--------------------------------------------------------
SRID=3003;POINT(3768164.4084946155 -3919656.244736101)

How can I add SRID 4326 (Spatial Types) to Workbench when adding columns?

When I add a column with type POINT in the EER Diagram, is there anything I can do with that diagram so when I generate automatically the scripts, SRID 4326 is attached to CREATE TABLE script? If I don't setup that number, then by default is zero (flat), but I do need 4326 (sphere).
If not possible, does that mean I cannot synchronise my model with my server automatically and I have to add these changes manually all the time?
I couldn't figure this out either. I believe adding a SRID to a column is currently not supported by MySQL Workbench.
To check that it is indeed not supported, I did the following:
Added a SRID to a column of an existing DB
Reversed engineered a script of this DB (using Workbench)
Checked the script if it included the set SRID for the column
Was disappointed that it didn't...
The "good" news is though, that as it is not supported, MySQL Workbench won't pick up on a missing SRID on a column when synchronizing sources.
This means that once you set the SRID on a column yourself, it won't cause any problems when synchronizing in the futures.
Note that in order to set a SRID on a column, there can't be a (spatial) index on that column. Therefore, you must remove the index, set the SRID and then add the index back.
Below is a short and simple script, which I used to do this. Don't forget to update it to your use-case:
DROP INDEX `my_idx` ON my_table;
ALTER TABLE my_table MODIFY COLUMN my_column POINT NOT NULL SRID 4326;
ALTER TABLE my_table ADD SPATIAL INDEX `my_idx` (`my_column`) VISIBLE;

How to add geometry column using pgAdmin

I'm using a database created in the PostgreSQL. In its schema there are two tables and in one of them I want to add a geometry column.
The problem is that I created the postgis Extension (CREATE EXTENSION postgis;) for the database, but I'm not able to add this data type (geometry) column using pgAdmin.
To do this with pgAdmin's "New Column..." dialog, if you can't find geometry, then you might be able to find public.geometry instead (if PostGIS was installed there, which is normal).
However, I advise against using pgAdmin for creating geometry columns, as it does not understand typmods used to define the geometry type and SRID.
The best way is using DDL to directly manipulate the table, e.g.:
ALTER TABLE locations ADD COLUMN geom geometry(PointZ,4326);
to add a geom column of XYZ points (long, lat, alt).

Converting lat-long to PostGIS geometry without querying the database

I have a table in postgresql with a PostGIS geometry(point, 4326) column (location, using SRID 4326) and I have a Python application that using SQL Alchemy updates the table (the rest of the columns) without any problem.
Now, I need to update the location column and I know I can use the proper text representation of a given location to update the column using SQL Alchemy without the need to use GEOAlchemy, for instance I can update the column with the value: '0101000020E6100000AEAC7EB61F835DC0241CC418A2F74040'
which corresponds to lat:33.9346343 long:-118.0488106
The question is: is there a way to compute in Python this '0101000020E6100000AEAC7EB61F835DC0241CC418A2F74040' having this (33.9346343 ,-118.0488106) as an input without querying the database? or any way to update the column using a proper text input?
I know I can use SQLAlchemy to execute this query:
select st_setsrid(st_makepoint(-118.0488106, 33.9346343),4326)
and obtain the value to update the column, but I want to avoid that.
Thanks in advance!
The solution to this problem is rather easier than it seems. To update the field using text and the input lat-long all I needed to do was defining the SRID in the text assign:
location = 'SRID=4326;POINT(-118.0488106 33.9346343)'
This will update the geometry(point,4326) column properly and when you do a select in the table the value of the column is the expected one:
"0101000020E6100000AEAC7EB61F835DC0241CC418A2F74040"
Thanks guys!