QGIS Add geometry to attribute table - postgresql

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

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;

Can't connect to PostGIS database

I have a myriad of issues with a seemingly simple process, starting with the tsk described below. But first some background:
Windows 10
QGIS 2.18.5
PgAdmin 4 (v 2.0)
Postgresql 10 installed
-Postgresql database (hosted on Amazon AWS cloud running PostgreSQL 9.6.5)
I'm very new to postgresql and postgis but after following all the basic instructions, I can't seem to be able to view any sql tables in QGIS. Here's what I have set up:
I'm in pgAdmin 4, logged in as admin (I'm the only user anyway), connected to my Amazon AWS server and connected to the new database I've created.
I've enabled the postgis extension (CREATE EXTENSION postgis;) and two others I read I might need...
All good so far?
I have a shapefile called test_poly.shp (created in ArcCatalog, with one simple shape drawn and one string field created) I want to upload to my database, so in the PostGIS Shapefile importer bundled with Postgresql, I connect to my database first:
And then import the shapefile, manually entering 27700 into the SRID field (British National Grid) and setting the 'Shape' field of the shapefile as the Geo Column (am I meant to do this? what is the Geo Column?). I also change the name of the Schema from 'public' to what I renamed it in pgAdmin.
issue: 1- Shapefile import failed...
==============================
Importing with configuration: test_poly, public, geom, D:\PostGIS\Test_poly.shp, mode=c, dump=1, simple=0, geography=0, index=1, shape=1, srid=0
Shapefile type: Polygon
PostGIS type: MULTIPOLYGON[2]
Shapefile import failed.
What's going on here? I've tried changing a few options including changing the input in the Geo Column to 'geom', then MULTIPOLYGON', then 'POLYGON' and back to 'shape', changing the encoding from UTF8 to LATIN1... no help.
Depending on your needs, a nice way to import geometries to PostGIS is using the Import into PostGIS tool from QGIS.
Just go to the toolbox (Advanced Interface) and search for Import into Postgis. The dialog is quite straightforward (The connection to the database needs to be done beforehand) ...
Based on this shapefile it creates the following table structure (Tested with QGIS 2.8.6-Wien):
CREATE TABLE public."tm_world_borders_simpl-0.3"
(
id integer NOT NULL DEFAULT nextval('"tm_world_borders_simpl-0.3_id_seq"'::regclass),
geom geometry(MultiPolygon,4326),
fips character varying(2),
iso2 character varying(2),
iso3 character varying(3),
un integer,
name character varying(50),
area integer,
pop2005 integer,
region integer,
subregion integer,
lon double precision,
lat double precision,
CONSTRAINT "tm_world_borders_simpl-0.3_pkey" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public."tm_world_borders_simpl-0.3"
OWNER TO disco2;
-- Index: public."sidx_tm_world_borders_simpl-0.3"
-- DROP INDEX public."sidx_tm_world_borders_simpl-0.3";
CREATE INDEX "sidx_tm_world_borders_simpl-0.3"
ON public."tm_world_borders_simpl-0.3"
USING gist
(geom);
Another PostGIS extension to import shapefiles is SPIT, but it is no longer maintained and can be quite bumpy with MultiPolygons. There are also other alternatives.
EDIT
How to add a new PostGIS Connection using QGIS:
Go to Add PostGIS Layer and click New
The enter your database address and credentials and click OK

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!