Created
March 11, 2017 21:45
-
-
Save cjauvin/4e04e58cd3669a2dfcb1e2bea12e5106 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def intersect(cur, sn1, sn2, sdr, inter_table): | |
sn1_field = STREETNAME_FIELD | |
if isinstance(sn1, int): | |
sn1_field = 'norte' | |
sn2_field = STREETNAME_FIELD | |
if isinstance(sn2, int): | |
sn2_field = 'norte' | |
q = """ | |
with p1 as ( | |
select st_collect(geom) as p1 | |
from {str_table} str, {inter_table} inter | |
where {sn1_field} = %(sn1)s | |
and str.gid = inter.gid | |
and inter.sdridu = %(sdr)s | |
), p2 as ( | |
select st_collect(geom) as p2 | |
from {str_table} str, {inter_table} inter | |
where {sn2_field} = %(sn2)s | |
and str.gid = inter.gid | |
and inter.sdridu = %(sdr)s | |
), inter as ( | |
select st_intersection(p1, p2) as inter | |
from p1, p2 | |
), bb as ( | |
select st_envelope(inter) as bb | |
from inter | |
), centroid as ( | |
select st_centroid(bb) as centroid | |
from bb | |
), bb_radius_in_meters as ( | |
select st_distance( | |
centroid::geography, | |
st_setsrid( | |
st_makepoint( | |
st_xmin(bb), | |
st_ymin(bb) | |
), | |
{srid} | |
)::geography | |
) as bb_radius_in_meters | |
from centroid, bb | |
) | |
select st_astext(centroid) as centroid, | |
bb_radius_in_meters | |
from centroid, bb_radius_in_meters | |
where centroid is not null | |
and bb_radius_in_meters is not null | |
""" | |
q = q.format( | |
sn1_field=sn1_field, | |
sn2_field=sn2_field, | |
str_table=STR_TABLE, | |
inter_table=inter_table, | |
srid=SRID | |
) | |
values = {'sn1': sn1, 'sn2': sn2, 'sdr': sdr} | |
# print cur.mogrify(q, values) | |
cur.execute(q, values) | |
row = cur.fetchone() | |
return {'coords': row['centroid'], | |
'bb_radius': row['bb_radius_in_meters']} if row else {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment