# Postgis: intersection between geometry and geography

I am trying to detect if a geometry and a geography intersect or not. The problem is I cannot find such a method. There only is:

boolean ST_Intersects( geometry geomA , geometry geomB );
boolean ST_Intersects( geography geogA , geography geogB );


But this is not what I want, since (from my tests using the geography function) a cast is performed and not a conversion to get two geography objects.

If my geometry object is a line when I convert it to a geography object it should become a curved line. But it is still a line.

How can I perform such a computation between a geometry and a geography?

In the following example, the red lines correspond to what postgis interprets as geography (projected of course in 2D) while the black lines correspond to what postgis interprets as geometry. From this we can see that:

• Line A as a geometry intersects line B, both as a geometry and as a geography
• Line A as a geography does not intersect line B, both as a geometry and as a geography

How can I detect the intersection between line A as a geometry and line B as a geography?

• If the line have only to points, you might have to add granularity to it before casting into geography. Try using ST_Segmentize(). – Alexandre Neto Sep 22 '15 at 11:54
• I will do your solution. This is, I think, an excellent workaround for my specific case but not what I was looking for in the first place. – Redtakfeoh Sep 24 '15 at 12:32

## 1 Answer

The way to go is as you explain yourself, a cast:

SELECT ST_Intersects(my_geography, my_geometry::GEOGRAPHY);


How did you notice that my_geometry was not a curved linestring? Note that it depends on the viewer you use if it displays the GeoJSON or WKT correctly curved.

• We have a map using Luciad on which I draw my objects. On the map I have an example where a geometry (line) and the geography(line too) intersect, but if I consider twe two points making the geometry, then there is no intersection. – Redtakfeoh Sep 22 '15 at 12:34
• I don't know if this is a good way but I tried the following: st_astext((('LINESTRING(0.0 0.0,100.0 60.0)')::geometry)::geography). The result is the same as the input linestring – Redtakfeoh Sep 22 '15 at 12:37
• sure, the geometries and geographies have the same textual description. what did you excect? if you display this geometries in an advanced viewer like QGIS, you will see that the line is curved. In some simpler viewer (like leaflet) you will see a straigt line. – linqu Sep 22 '15 at 13:12
• That's why I wasn't sure it was a good example. But that does not change the problem: how can I convert geometry <=> geography with postgis? – Redtakfeoh Sep 22 '15 at 13:38
• by casting: my_geometry::GEOGRAPHY or my_geography::GEOMETRY – linqu Sep 22 '15 at 14:26