1

I've a query that has to be run against 1 million records. The query finds the intersection between two polygons and the area of the result of the intersection. Here's the query:

SELECT 
 ST_Area(
 ST_Collect(
 ST_Intersection(poly1.the_geog::geometry, poly2.the_geog::geometry)
 )::geography
 ),
 ST_AsGeoJSON(
 ST_Multi(
 ST_Collect(
 ST_Intersection(poly1.the_geog::geometry, poly2.the_geog::geometry)
 )
 )
 ) 
FROM poly1, poly2 
WHERE poly1.id = 1 AND
 ST_Intersects(poly1.the_geog::geometry,poly2.the_geog::geometry)

I'm trying to figure out if I can use the result of ST_Intersection query which is common for ST_AsGeoJSON and for calculating the area using ST_Area. Is it possible to do it ?

Michal Zimmermann
4,2621 gold badge25 silver badges40 bronze badges
asked Jul 29, 2016 at 9:10

1 Answer 1

1

Use nested SELECT statement. In the inner SELECT run the ST_Intersection, then pass the result to the outer SELECT to ST_Area and ST_AsGeoJSON. Be sure to have indices built on your table as well.

answered Jul 29, 2016 at 11:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.