How to add geometry column using pgAdmin - postgresql

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).

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

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;

QGIS Add geometry to attribute table

I am importing my data into QGIS from a local postgreSQL server. The dataset loaded into the database contains 11 columns including a X and Y column which are in the CRS (Coodinate Reference System): EPSG:21781, CH1903 / LV03. I am trying to plot these points as a layer in QGIS but when I import it using the “Add PostGIS layer” I have to click “Also list tables with no geometry” to find it. Once added it appears as an attribute table which I can go into the layer properties and select the correct CRS but it still doesn't appear correctly.
I am still new to QGIS and PostgreSQL, am I doing something wrong or do I need to define the coordinates in the database before I import them to QGIS?
You need to create the geometry on the table. As of now, you are just displaying a table that does not contain any point so the coordinate system does not even apply.
So, first you would create the geometry column via PostGIS AddGeometryColumn
SELECT AddGeometryColumn ('myschema','mytable','geom',21781,'POINT',2);
Then you would update this new column with existing values.
UPDATE mytable SET geom = ST_SETSRID(ST_MakePoint(X, Y), 21781);

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!

Postgres spatial SQL queries

I'm trying to wrap my head about how postgres spatial stuff works from a SQL point of view. My goal is to be able to insert polygon geometry references as a column onto a table that also includes other information -- geometry name, et cetera.
I've started out by importing shapefiles into a geometry enabled postgres database. The polygon tables have been created off on a different schema; we'll use polygonGeometry as a example. So, public.geometry_columns has references off to polygonGeometry.(table) for each of my inserted polygons.
I then want to create another table, that has an id (serial primary key), a name (character variable), and a reference to the geometry (either polygon, or a refernece to a different key) that I inserted. How do I go about setting this schema up?
I then have another table with a id (serial primary key), lat (real) and long (real). What SQL query would I run to select geometries from the first table by primary key id, combine them with ST_Union, and return points from the second table created with ST_GeomFromText with the lat and long columns for each row that are within the unioned polygon?
Additionally, does anyone know any good references for getting up to speed with the spatial stuff from a schema, design, and usage standpoint assuming comfortable familiarity with SQL?
I think if you take a look at https://gis.stackexchange.com/ you will find answers to most (if not all) of your questions asked. Search under the postgis tag.
For resources, I really liked the book "PostGIS in Action".
You can get links and learn more at How do I get started with PostGis? and Spatial databases learning resources for newbies.