| 49 | * first create two index to speed up queries |
| 50 | |
| 51 | |
| 52 | {{{ |
| 53 | CREATE INDEX indexiabret ON ia.iabret |
| 54 | USING GIST ( the_geom GIST_GEOMETRY_OPS ); |
| 55 | CREATE INDEX indexiaiav ON ia.iaiav |
| 56 | USING GIST ( the_geom GIST_GEOMETRY_OPS ); |
| 57 | }}} |
| 58 | Joining the riversegment and stationsp2, the_geom is that of stationsp2 |
| 59 | |
| 60 | -- creation de la table correspondance, je rajoute un the_geom a la requete pour pouvoir voir la table dans Qgis |
| 61 | DROP TABLE IF EXISTS bd_map.correspondance; |
| 62 | CREATE TABLE bd_map.correspondance as ( |
| 63 | SELECT distinct on (st_codecsp) st_codecsp, gid, min(distance) as distance, the_geom FROM ( |
| 64 | SELECT st_codecsp, gid ,CAST(distance(r.the_geom, s.the_geom) as decimal(15,1)) as distance,s.the_geom |
| 65 | FROM bd_map.stationsp2 As s |
| 66 | INNER JOIN riversegments r ON ST_DWithin(r.the_geom, s.the_geom,300) |
| 67 | WHERE s.the_geom IS NOT NULL |
| 68 | ORDER BY st_codecsp) AS sub |
| 69 | GROUP BY st_codecsp, gid, the_geom |
| 70 | ); |
| 71 | alter table bd_map.correspondance add column id serial; |
| 72 | -- mise à jour de la table geometry_columns |
| 73 | INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type") |
| 74 | SELECT '', 'bd_map', 'correspondance', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom) |
| 75 | FROM bd_map.correspondance LIMIT 1; |
| 76 | |
| 77 | -- creation d'index, clé primaire, et constraintes qui vont bien |
| 78 | alter table bd_map.correspondance add CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2); |
| 79 | alter table bd_map.correspondance add CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL); |
| 80 | alter table bd_map.correspondance add CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 3035); |
| 81 | alter table bd_map.correspondance ADD CONSTRAINT pk_id PRIMARY KEY(id); |
| 82 | CREATE INDEX indexcorrespondance ON bd_map.correspondance |
| 83 | USING GIST ( the_geom GIST_GEOMETRY_OPS ); |
| 84 | |