Overlay med GeoPandas og PostGIS

Viser bruk av PostGIS ST_INTERSECTS og ST_INTERSECTION se 8.11. Spatial Relationships og 8.13. Overlay Functions

Brukernavn og passord til databasen, samt vertsnavn (host), portnummer og databasenavn legges i en egen fil, mypostgisdb.py, slik:

postgis_connection_string = "postgresql://brukernavn:passord@host:5432/databasenavn"

In [1]:
import pandas as pd
import geopandas as gpd
import fiona as fi
In [2]:
from sqlalchemy import create_engine
from mypostgisdb import postgis_connection_string
In [3]:
db = create_engine(postgis_connection_string)
In [4]:
ledning_gdb = "Basisdata_1133_Hjelmeland_5972_FKB-Ledning_FGDB.gdb"
print(fi.listlayers(ledning_gdb))
['fkb_ledning_posisjon', 'fkb_ledning_omriss', 'fkb_ledning_beliggenhet', 'fkb_ledning_eier_tabell', 'fkb_ledning_fellesforing_tabell']
In [5]:
ledning = gpd.read_file(ledning_gdb, layer = 'fkb_ledning_beliggenhet')
In [6]:
ledning.plot( figsize=(15,15))
Out[6]:
<AxesSubplot:>
In [7]:
#ledning.to_postgis('ledning', db)
In [8]:
# Disse variablene brukes i spørringer med gpd.read_postgis()
geom_col = 'geometry'
crs = 'EPSG:25832'
In [9]:
sql = 'SELECT treslag, skogbonitet, geometry from prodskog.arealressursflate, public.ledning where ST_INTERSECTS(ledning.geometry, prodskog.arealressursflate.omraade)'
ledning_skog_intersection = gpd.read_postgis(sql, db, geom_col, crs)
ledning_skog_intersection
Out[9]:
treslag skogbonitet geometry
0 32 14 MULTILINESTRING Z ((355134.669 6579684.479 44....
1 32 11 MULTILINESTRING Z ((355134.669 6579684.479 44....
2 32 14 MULTILINESTRING Z ((355166.409 6579680.359 55....
3 31 14 MULTILINESTRING Z ((354812.129 6579065.119 45....
4 33 15 MULTILINESTRING Z ((347553.269 6576988.519 243...
... ... ... ...
1104 32 11 MULTILINESTRING Z ((353495.409 6574871.539 387...
1105 32 11 MULTILINESTRING Z ((353593.399 6574906.119 406...
1106 32 13 MULTILINESTRING Z ((352951.839 6574659.629 397...
1107 32 11 MULTILINESTRING Z ((353059.529 6574660.249 378...
1108 32 13 MULTILINESTRING Z ((352951.839 6574659.629 397...

1109 rows × 3 columns

In [10]:
ledning_skog_intersection.plot( figsize=(15,15))
Out[10]:
<AxesSubplot:>
In [11]:
ledning_skog_intersection
Out[11]:
treslag skogbonitet geometry
0 32 14 MULTILINESTRING Z ((355134.669 6579684.479 44....
1 32 11 MULTILINESTRING Z ((355134.669 6579684.479 44....
2 32 14 MULTILINESTRING Z ((355166.409 6579680.359 55....
3 31 14 MULTILINESTRING Z ((354812.129 6579065.119 45....
4 33 15 MULTILINESTRING Z ((347553.269 6576988.519 243...
... ... ... ...
1104 32 11 MULTILINESTRING Z ((353495.409 6574871.539 387...
1105 32 11 MULTILINESTRING Z ((353593.399 6574906.119 406...
1106 32 13 MULTILINESTRING Z ((352951.839 6574659.629 397...
1107 32 11 MULTILINESTRING Z ((353059.529 6574660.249 378...
1108 32 13 MULTILINESTRING Z ((352951.839 6574659.629 397...

1109 rows × 3 columns

In [12]:
sql = 'SELECT treslag, skogbonitet, ST_INTERSECTION(geometry, omraade) as geometry from prodskog.arealressursflate, public.ledning where ST_INTERSECTS(ledning.geometry, prodskog.arealressursflate.omraade)'
ledning_skog = gpd.read_postgis(sql, db, geom_col, crs)
In [13]:
base = ledning.plot(color='cyan', figsize=(15,15))
ledning_skog.plot(ax=base, color='red');