I have import OpenstreetMap data into Postgres with gis extension with tool
osm2pgsql (-s option)
of course, I have the following tables
planet_osm_point
planet_osm_ways
....
Within planet_osm_ways I have a column called way, type geometry(LineString, 4326), content like following
"0102000020E6100000070000005E70BCF1A49F2540D3D226987B134840896764EB749F25403B5DCC858013484040D1860D609F2540C426327381134840CE50DCF1269F2540EF552B137E1348405AAB2CC02D9E2540F978324976134840D66F26A60B9D2540CE8877256E1348403CA81F2FFF9C2540BC1D86FB6D134840"
What is that ? How could I get all points along this way ?
Thanks a lot
That's hex-encoded extended well-known binary (EWKB) of a LINESTRING.
There are several methods to get the points along the way. To get individual coordinates as points, use ST_DumpPoints. Or to simply output the geometry in other human-readable formats (WKT, EWKT, GeoJSON, GML, etc.), see the relevant manual section.
Related
I have two tables with geometry data. One table has MULTIPOLYGON geometry in non-standard srid and another has POINT geometry in 4326. I have a spatial_ref_sys table with a correct description for my srid. Then I want to use st_contains for geometry from these tables. I use st_transform to convert geometry from one table to srid from another, but the function returns geometry under Africa (all geometry is on the territory of Europe). Also, I want to admit that I use proj description from QGIS, and in this program, all works correctly. I use PostGIS 3.2. Here is my code for st_contains:
select st_contains(st_transform(geom_multipolygon_in_4326, SRID_from_another_table), geom_point_in_non_standart_srid);
Here is my proj:
+proj=tmerc +lat_0=0 +lon_0=30 +k=1 +x_0=-10000 +y_0=-5540000 +ellps=krass +towgs84=23.92,-141.27,-80.9,0,0.35,0.82,-0.12000000004786 +units=m +no_defs
However, when I use PostGIS 2.2 all works correctly with an identical proj description. I don't understand what the problem is. Maybe someone can help me, because I only found a solution by creating a layer in qgis and then importing it into the database, but this is not a solution for me, because the point geometry is formed through a query to Google and I still need to transform it using st_transform.
Apologies if this is a silly question, I'm very new to PostgreSQL/PostGIS.
I have downloaded an .osm file for the entire state of Baden-Württemberg from here - https://download.geofabrik.de/europe/germany/baden-wuerttemberg.html
I have to answer the following questions, by writing queries for the same -
What is the area (expressed in m^2) of Baden-Württemberg?
What is by area the smallest city of Baden-Württemberg?
I have imported the osm file into a PostgreSQL using the osm2pgsql tool.
There seem to be multiple tables that are being created, core and generated by osm2pgsql. In addition to the core tables - planet_osm_nodes, planet_osm_ways and planet_osm_rels there are other tables as well, and I'm not quite sure how to use them to complete my tasks.
It would be really helpful if anybody could provide some insight into how I can go about writing the PostGIS query for the same.
I think something like this should give you the cities and their area in square meters in order
SELECT *, ST_Area(way)
FROM planet_osm_polygon
WHERE boundary='administrative'
AND admin_level=7
ORDER BY ST_Area(way)
Same thing should work with the border of Baden-Württemberg only that you would need admin_level=4
https://postgis.net/docs/ST_Area.html
https://wiki.openstreetmap.org/wiki/DE:Grenze
I have imported two shape-files using QGIS into pgAdmin 4 (PostgreSQL), I use the PostGIS extension to be able to access spatial commands.
I want to check all the POIS (points) that are included in the Gemeinden (multipolygon) table. To accomplish that I use the spatial command "ST_Contains" ([postgis.net documentation regarding the command])1. Contrary to the fact that the points are actually in the polygons, the returned query is empty (see QGIS and output screenshot). What could be the issue? Any help would be appreciated. Thank you in advance!
Output - pgAdmin
The command I use:
SELECT * FROM public."POIS" AS pois INNER JOIN public."Gemeinden" AS gem
ON (1 = 1)
WHERE ST_Contains(gem.geom, pois.geom) = true;
My tables:
POIS table content
[3
Gemeinden table content
[4
QGIS Screenshots:
Both shape-files together:
[5
Only the POIS shape-file:
[6
Only the Gemeinden shape-file:
[7
Update:
I have created a polygon table out of the multi-polygons using the command
CREATE TABLE polygon_table AS
SELECT id, public."Gemeinden".kg_nr, public."Gemeinden".kg, (ST_DUMP(geom)).geom::geometry(Polygon,4326) AS geom FROM public."Gemeinden";
Afterwards I've updated the SRID of both the POIS table and the newly created one using :
SELECT UpdateGeometrySRID('polygon_table','geom',4326);
and
SELECT UpdateGeometrySRID('POIS','geom',4326);
Sadly,
SELECT pois.* FROM public."POIS" AS pois JOIN public."polygon_table" AS
gem
ON ST_intersects(gem.geom, pois.geom);
still returns a empty query.
Any ideas? Thank you in advance!
First of all make sure the SRID of the both table must be same. If it is not same then spatial queries wont work.
Secondly convert the multipolygons to single polygons. Following link may help
PostGIS - convert multipolygon to single polygon
Finally the following query would be enough if you just want to get the POIS that intersects the polygons
SELECT pois.* FROM public."POIS" AS pois JOIN public."Gemeinden" AS gem
ON ST_intersects(gem.geom, pois.geom);
I have resolved the problem by going back to QGIS and saving both previously imported shape-files as the same SRID. Apparently, my code that converted the SRID in the database did not work.
Right click on the shape-file
Selecting the SRID
Note: I have done this for both shape-files, just to be sure.
Another factor that may have influenced the outcome was the selection of the "convert to single polygon" box while importing into the database from QGIS.
The box that I ticked for both shape-files while importing
PS: I use the German version of QGIS
I am new to sql, and attempting to use it to speed up spatial analysis on a set of ~1.2 million trips from a csv that contains the lat and lon for pickup and dropoff points.
What I am trying to do in plain English is:
select all trips that start in the area of interest (loaded into my database as a shapefile) into one table
select all trips that end in the area of interest into another
-perform a spatial join between these points and a shapefile of census tracks (which contains neighborhood names)
count by neighborhood name to list the most frequent origins/destination of trips to/ from the area of interest.
The code I am working with is below (If its helpful, NTA or neighborhood tabulation area, is the neighborhood name which I want to display in my table at the end of this operation) :
--Select all trips that end in project area
SELECT *
INTO end_PA
FROM trips, projarea
WHERE ST_Intersects(trips.dropoff, projarea.geom);
--for trips that end in project area - index by NTA of pick up point
ALTER TABLE end_PA ADD COLUMN GID SERIAL;
CREATE TABLE points_ct_end AS
SELECT nyct2010.ntacode as ct_nta, end_PA.gid as point_id
from nyct2010, end_PA WHERE ST_Intersects(nyct2010.geom , end_PA.pickup);
--Count most common NTA
--return count for each NAT as a csv
copy(
select count(ct_nta) from points_ct_end
group by ct_nta
order by count desc)
to 'C://TaxiData//Analysis//Trips_Arriving_LM.csv' DELIMITER ',' CSV HEADER;
However, I am having problems from the very start - ST_Intersects does not return any points within the area of interest!
Troubleshooting solutions I have tried thus far:
My first thought is that the points weren't in the correct SRID. When I created the 'dropoff' point I set the SRID to 4326. I tried both using ST_SetSRID to change the projection of both data sets to 4326, and manually re projecting the shapefiles to 4326 in ArcMap - but neither worked.
I plotted a small sample of the points from the 'trips' data set in Arc Map to ensure they were correctly projected and overlapping with the ProjArea shapefile. They are.
I imported the multipoint shapefile this created into my geo database to test if that worked with ST_Intersects. Nope.
I tried using ST_Within. This threw the error message:
ERROR: function st_within(character varying, geometry) does not exist
....
HINT: No function matches the given name and argument types. You
might need to add explicit type casts.
I am using Big SQL and postgres
Thanks!!
My first thought is that the points weren't in the correct SRID. When I created the 'dropoff' point I set the SRID to 4326. I tried both using ST_SetSRID to change the projection of both data sets to 4326, and manually re projecting the shapefiles to 4326 in ArcMap - but neither worked.
ST_SetSRID doesn't change the projection (reproject). It just changes the internal representation. This can totally screw everything up if the previous SRID matched the input data. You likely wanted ST_Transform().
There isn't enough information here to trouble shoot this problem. However, we can answer this...
ERROR: function st_within(character varying, geometry) does not exist
This simply means the first argument is not a geometery. Of course, we can't do anything with that at all because we don't have your query that you tried with ST_Within().
Your syntax for ST_Intersects() looks to be right. But, there simply isn't enough information provided to help. Show some schema and sample data.
I have some extravagant local spatial reference system and I have a lot of data stored in some old legacy system. Now I want to import this data to my Postgresql/Postgis database. On the client side I'm using JavaScript OpenLayers 3 library (if it matters), on the server side I'm used to storing geometry data with srid 3857, so my tables with layer data have such constraints:
CONSTRAINT enforce_dims_geom_layer_1_ CHECK (st_ndims(geom) = 2),
CONSTRAINT enforce_srid_geom_layer_1_ CHECK (st_srid(geom) = 3857)
So, if I have this legacy data, with some coordinates in a local reference system, how can I approach this problem to get a formula like:
+proj=longlat +ellps=bessel +towgs84=595.48,121.69,515.35,4.115,-2.9383,0.853,-3.408 +no_defs
Have a look at the public.spatial_ref_sys table. There the SRIDs are defined and you can insert your new SRID. The column proj4text includes the formulas.
The website https://epsg.io will generate an insert string for your SRID if you can find the page for your desired EPSG code. Once you find the page for the code you want, scroll down to "Export", below that on the left set it to "PostGIS". You can then "Copy TEXT", and paste that into your terminal or whatever you use to interact with your database.