-
Notifications
You must be signed in to change notification settings - Fork 0
Some useful SQL Scripts
Pascal Dihé edited this page Jul 15, 2015
·
47 revisions
SELECT count(distinct r.id) FROM resource r
INNER JOIN geom g ON r.spatialcoverage = g.id
AND g.geo_field && st_GeomFromEWKT('SRID=4326;POLYGON ((-9.194288369255645 54.72093488558994, -9.194288369255645 35.72093488558994, 13.805711630744355 35.72093488558994, 13.805711630744355 54.72093488558994, -9.194288369255645 54.72093488558994))')
AND st_intersects(st_GeomFromEWKT('SRID=4326;POLYGON ((-9.194288369255645 54.72093488558994, -9.194288369255645 35.72093488558994, 13.805711630744355 35.72093488558994, 13.805711630744355 54.72093488558994, -9.194288369255645 54.72093488558994))'), geo_field)
WHERE r.id IS NOT NULSpatial search using the geom_search table
SELECT count (distinct resource.id) FROM resource
INNER JOIN geom_search ON resource.id = geom_search.resource
AND geom_search.geo_field && st_GeomFromEWKT('SRID=4326;POLYGON ((-9.194288369255645 54.72093488558994, -9.194288369255645 35.72093488558994, 13.805711630744355 35.72093488558994, 13.805711630744355 54.72093488558994, -9.194288369255645 54.72093488558994))')
AND st_intersects(st_GeomFromEWKT('SRID=4326;POLYGON ((-9.194288369255645 54.72093488558994, -9.194288369255645 35.72093488558994, 13.805711630744355 35.72093488558994, 13.805711630744355 54.72093488558994, -9.194288369255645 54.72093488558994))'), geom_search.geo_field)
WHERE resource.id IS NOT NULLFind geometries, that are not directly connected to resources.
SELECT count(geom.id)
FROM geom
WHERE geom.id NOT IN
(SELECT spatialcoverage
FROM resource WHERE spatialcoverage IS NOT NULL);Insert geometries
imported from a .shp file from a temporary import table into the common geom_search table and simplify them with ST_Envelope. Replace '4711' with the respective resource id.
INSERT INTO public.geom_search(resource, geo_field)
SELECT 4711,
ST_Envelope(geom)
FROM import_tables.geosearch_import;Find Multipoint / Multipolygons and small bboxes and add them to the geometry search table.
INSERT INTO public.geom_search(resource, geo_field, geom)
SELECT DISTINCT resource.id,
geom.geo_field,
geom.id
FROM resource
JOIN geom ON resource.spatialcoverage = geom.id
WHERE
(SELECT ST_GeometryType(geo_field) IN ('ST_MultiPoint',
'ST_MultiPolygon',
'ST_Point'))
OR (
(SELECT ST_GeometryType(geo_field) = 'ST_Polygon')
AND
(SELECT ST_Area(geo_field) < 100));SELECT name, description,
(SELECT ST_GeometryType(geo_field)) AS mp
FROM resource
JOIN geom ON resource.spatialcoverage = geom.id
WHERE
(SELECT ST_GeometryType(geo_field) = 'ST_MultiPoint')
ORDER BY resource.name;SELECT DISTINCT tag.name
FROM tag
INNER JOIN jt_resource_tag ON jt_resource_tag.tagid = tag.id
WHERE taggroup IN
(SELECT id
FROM taggroup
WHERE name ILIKE '%cuahsi%')
ORDER BY tag.nameSELECT tag.name,
COUNT(tag.name) AS NumOccurrences
FROM tag
LEFT JOIN taggroup ON tag.taggroup = taggroup.id
WHERE taggroup.name = 'keywords - open'
GROUP BY
tag.name
HAVING ( COUNT(tag.name) > 1 )
ORDER BY tag.name;SELECT DISTINCT n1.* FROM contact n1
inner join contact n2 on n2.organisation=n1.organisation
where n1.id <> n2.id
ORDER BY n1.organisationImport the CUAHSI Tabluar Ontology into new CUAHSI keyword list and skip duplicate keywords:
Get a list of all keywords in the Meta-Data Repository
select name from tag
where taggroup in
(select id from taggroup where name ilike '%keyword%')
group by tag.name order by tag.name