Friday, 16 March 2018

topology - Is there a standard st_relation_readable() function for translate DE-9IM strings?


Spatial databases, such as PostGIS, implement the DE-9IM model with function st_relate(), which returns a string with domain of {0,1,2,F}, meaning 0=point, 1=line, 2=area, and F="empty set"...

See DE-9IM at Wikipedia.


The DE-9IM codes are not understandable for programmers and mortal users, but each code have the semantic of a list of predicates, then, it can be translated into a human-redable list of predicates.


Example: when st_relate(a,b)='FF1F0F1F2' or st_relate(a,b)='FF10F0102' or st_relate(a,b)='FF1F0F1F2' the DE-9IM code can be understood as "Intersects & Touches" or "Intersects & Touches with a point".


Is there a standard function or library that implements this translation, from a DE-9IM code to a human-readable string?




REPORT EXAMPLES: instead Report-1 I need Report-2.


Report-1 (have a standard string, but not human readable)


 0F1F00102 |       2 | group 1
0F1FF0102 | 616 |
total 618


1FF00F102 | 1 | group 2
1FF0FF102 | 1 |
total 2

1FFF0FFF2 | 23 | group 3
total 23

F01FF0102 | 31 | group 4
F01FFF102 | 1 |

FF10F0102 | 42 |
FF1F00102 | 19673 |
FF1F0F1F2 | 25 |
total 19772

Report-2 (human readable... There are a SQL function to do this?)


  618 | point-Intersects & point-Crosses & line-Overlaps
2 | point-Intersects & line-Crosses & line-Within & point-CoveredBy
23 | line-Equals
19772 | point-Intersects & point-Touches


Answer



I am using ST_relate_summary function, acting as the requested ST_Relate_readable(), that I posted into code.google as simple a SQL/PostGIS code. It is not from a standard library, neither a systematically tested or optimized one.


Thanks to Mike about JTS Topology Suite: JTS offers a (very good!) graphical interface, reporting spatial predicates... I need "string reports", listing predicates (operators) names only, all into SQL.


The ST_relate_summary function and the reports below illustrates an answer, but it is not based on a standard or homogated library, as requested by the question...
Well, if there are another oldest or standardized function, please answer or comment here...


EXAMPLES


REPORT-1: (illustrating usual report) Shows standard string codes (DE-9IM) but they are not human-readable.


SELECT count(*) as n, rel
FROM (
SELECT ST_Relate(r1.the_geom, r2.the_geom) AS rel

FROM mylayer r1, mylayer r2
WHERE r1.gid>r2.gid AND r1.the_geom && r2.the_geom
) AS t
GROUP BY rel;

Result:


   n   |    rel    
------+-----------
2 | 0F1F00102
10669 | FF1FF0102

616 | 0F1FF0102
25 | FF1F0F1F2
1 | 1FF0FF102
1 | 1FF00F102
1 | F01FFF102
19673 | FF1F00102
42 | FF10F0102
1 | FF1FF01F2
23 | 1FFF0FFF2
1 | FF1FFF102

31 | F01FF0102
(13 records)

REPORT-2: Shows non-standard but human-readable summary.


SELECT count(*) as n, rel
FROM (
SELECT ST_Relate_summary(ST_Relate(r1.the_geom, r2.the_geom)) AS rel
FROM mylayer r1, mylayer r2
WHERE r1.gid>r2.gid AND r1.the_geom && r2.the_geom
) AS t

GROUP BY rel;

Result:


   n   |                                 rel                                                       
------+-------------------------------------------------------------
10671 | Disjoint.
19772 | Intersects-point & Touches-point.
618 | Intersects-point & Crosses-line & Overlaps-line.
2 | Intersects-line & Crosses-line & Within-line & CoveredBy-line.
23 | Equals-line.


Same report, removing sufixes and abbreviating predicate names (changing ST_Relate_summary parameters)... Not so readable, not standard, but a compact summary:


   n   |     rel                                                       
------+------------------
10671 | dsj
19772 | int tch
618 | int crs ovr
2 | int crs wth cvb
23 | eql


REPORT-3: Explains and groups the standard codes.


SELECT count(*) as n, rel
FROM (
SELECT lib.st_relate_summary( ST_Relate(r1.the_geom,r2.the_geom) , 'las') AS rel
FROM mylayer r1, mylayer r2
WHERE r1.gid>r2.gid AND r1.the_geom && r2.the_geom
) AS t
GROUP BY rel
ORDER BY rel;


Result:


   n   |               rel               
------+---------------------------------
10669 | dsj - FF1FF0102
1 | dsj - FF1FF01F2
1 | dsj - FF1FFF102
23 | eql1 - 1FFF0FFF2
2 | int0 crs1 ovr1 - 0F1F00102
616 | int0 crs1 ovr1 - 0F1FF0102
31 | int0 tch0 - F01FF0102

1 | int0 tch0 - F01FFF102
42 | int0 tch0 - FF10F0102
19673 | int0 tch0 - FF1F00102
25 | int0 tch0 - FF1F0F1F2
1 | int1 crs1 wth1 cvb1 - 1FF00F102
1 | int1 crs1 wth1 cvb1 - 1FF0FF102
(13 records)

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...