I want to create a database view in Postgres/PostGIS which returns a subset of the points stored in another table called locations
. The subset is defined by an area stored in yet another table called district
.
I came up with the following query inspired by this answer:
SELECT l.*
FROM
public."locations" AS l,
public."district" AS d
WHERE
ST_CONTAINS(l.the_geom, d.the_geom);
This query returns an empty result.
I used QGIS to visually check that the district actually contains locations.
It might be that I store the district incorrectly. The data origins from OpenStreetMap. I hope the collection of way
objects I exported from OSM is interpreted as a polygon by PostGIS. This is the command I used to import the Shapefiles into PostGIS:
$ ogr2ogr -update -append -f "PostgreSQL" \
PG:"host=localhost user=username port=5432 \
dbname=databasename password=password" \
district.shp -lco GEOMETRY_NAME=the_geom -nln "public.district"
The database table district
looks like this:
CREATE TABLE public.district
(
ogc_fid serial NOT NULL,
the_geom geometry(LineString,4326),
name character varying(254),
cmt character varying(254),
"desc" character varying(254),
src character varying(254),
link1_href character varying(254),
link1_text character varying(254),
link1_type character varying(254),
link2_href character varying(254),
link2_text character varying(254),
link2_type character varying(254),
"number" numeric(10,0),
type character varying(254),
CONSTRAINT districts_pkey PRIMARY KEY (ogc_fid)
)
Do I need to SELECT
the district
table as a POLYGON
first?