Thursday, 16 May 2019

Compare features in PostGIS with updating attributes


I have two polygonal layers: "existing" and "modified". enter image description here Attribute table of "existing" and "modified" layers have same column "feature_id" - number of feature. Please help me create query which write attributes 't' (if features matches) and 'f' (if features not matches) in 'compare_result' column of "modified" layer using ST_Equals function. Now I receive attributes 't' (if features matches) and 'f' (if features not matches) in pgAdmin result window using query:


SELECT  ST_Equals(modified.the_geom, existing.the_geom) AS compare_result FROM  modified, existing 
WHERE modified.feature_id=existing.feature_id

but I don't can create query? which write result in attribute table. Thanks!


EDIT: Solution without creating additional layer:


UPDATE
modified
SET

compare_result = compare_result_temp

FROM
(
SELECT
r."feature_id" AS idmodified,
ST_Equals(r.the_geom, m.the_geom) AS compare_result_temp
FROM
"modified" AS r,
"existing" AS m

WHERE r."feature_id" = m."feature_id"
) AS foo
WHERE
foo.idmodified = modified."feature_id";

Answer



You can try this :


-- create table temp


drop table if exists compare_result;
create table compare_result as (
SELECT

existing.feature_id as id_init,
modified.feature_id as id_modified,
ST_Equals(modified.geom, existing.geom) AS compare_result
FROM modified RIGHT OUTER JOIN existing ON (modified.feature_id=existing.feature_id)
);

-- then you can update an attibut (compare_result) -- ALTER TABLE modified add column compare_result varchar(1);


UPDATE modified set compare_result = 't' 
FROM compare_result
WHERE compare_result = 'TRUE' and modified.feature_id = compare_result.compare_result;


UPDATE modified set compare_result = 'f'
FROM compare_result
WHERE compare_result = 'FALSE' and modified.feature_id = compare_result.compare_result;

-- drop table


drop table if exists compare_result;

it should work...


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...