# Compare linestrings in postgis

Let us assume the following two lines The two geometries are extracted by two different tables: table_1 and table_2 with respective column geometries geometry_1 and geometry_2. I want to check if the two lines are identical. Condition for this is if the max distance between the two lines is not longer than x meters. I am running the following:

``````create index geom_1_idx ON table1 USING GIST (geometry_1);
select table1_id, table2_id  from table1 t1, table2 t2
where (select st_DWithin(t1.geometry_2 , t2.geometry_2, units_of_srid) = true)
``````

With spatial indexing, I understand that the comparison refers to the bounding boxes of the lines.

How correct is it?

Is a case like the one of the image covered?

It sounds like you need to know the maximum distance between the two lines, but `ST_DWithin` is computing the minimum distance between the two lines.

I think what you're after is the Hausdorff Distance metric, implemented in PostGIS as ST_HausdorffDistnace.

Because the Hausdorff distance is computed between sets of points, not lines, you may want to specify a densification parameter to interpolate additional points from the lines for use in the computation.

The densification parameter `f`, which must be between 0 and 1, causes each segment to be divided into approximately `1/f` smaller segments. With a densification fraction of 1.0, no segments will be divided. As the densifcation parameter approaches (but does not reach) zero, the approximated value generated by this function will approach the true Hausdorff distance.

• Thanks a lot for the reply. This is what I wanted. The densification parameter is in range of (0.1 - 1.0). If I choose 1.0, this means that all the points of the lines will be compared? – Dino C Feb 8 '17 at 16:00
• @DinoC, do the edits clear things up? – dbaston Feb 8 '17 at 16:55
• perfect. It's very clear – Dino C Feb 8 '17 at 17:25
• sth last. What about spatial indexing with ST_HausdorffDistnace? Can it make running faster? – Dino C Feb 8 '17 at 19:32
• You could say something like `where t1.geom && g2.geom and ST_HausdorffDistance(t1.geom, t2.geom) < xxx` – dbaston Feb 8 '17 at 20:16