Referanser som gir bakgunn for å forstå eksemplene nedenfor:
NTNU 26.02.2021 - Sverre Stikbakke NTNU 26.02.2021 - ny versjon av GeoPandas, 0.9: oppdaterte linker
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon
# oppretter en GeoSeries for Smaragd-bygg-tomta
smaragd = gpd.GeoSeries(Polygon([
[591518.2, 6740464.0499],
[591442.74, 6740401.1999],
[591399.69, 6740452.8699],
[591460.55, 6740518.9299],
[591464.35, 6740525.3999],
[591471.27, 6740517.5099],
[591516.35, 6740466.1499],
[591518.2, 6740464.0499]
]), index=['smaragd'], crs=25832)
smaragd.plot()
<AxesSubplot:>
buffer = smaragd.buffer(25)
buffer.plot()
<AxesSubplot:>
smaragd.area
smaragd 7166.96175 dtype: float64
buffer.area
smaragd 17737.30388 dtype: float64
# oppretter flere (Pandas) Series-objekter for egenskapsdata. Disse har ikke geometri - så vi trenger ikke GeoSeries
localid = pd.Series(446124344, index=['smaragd'])
objtype = pd.Series('Teig', index=['smaragd'])
SHAPE_Length = pd.Series(344.415337703413, index=['smaragd'])
SHAPE_Area = pd.Series(7166.96174997119, index=['smaragd'])
# samler alle Series- og GeoSeries-objekter i en GeoDataFrame
teiger = gpd.GeoDataFrame({
'localid': localid,
'objtype': objtype,
'SHAPE_Length': SHAPE_Length,
'SHAPE_Area': SHAPE_Area,
'geometry': smaragd,
})
teiger
localid | objtype | SHAPE_Length | SHAPE_Area | geometry | |
---|---|---|---|---|---|
smaragd | 446124344 | Teig | 344.415338 | 7166.96175 | POLYGON ((591518.200 6740464.050, 591442.740 6... |
teiger.info()
<class 'geopandas.geodataframe.GeoDataFrame'> Index: 1 entries, smaragd to smaragd Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 localid 1 non-null int64 1 objtype 1 non-null object 2 SHAPE_Length 1 non-null float64 3 SHAPE_Area 1 non-null float64 4 geometry 1 non-null geometry dtypes: float64(2), geometry(1), int64(1), object(1) memory usage: 48.0+ bytes
# alternativ måte for å opprette en GeoDataFrame:
# oppretter først et python dictionary med data for Beryll-bygg-tomta. GeoSeries brukes for geometrien.
beryll = {
'localid': 49174530,
'objtype': 'Teig',
'SHAPE_Length': 306.207095792018,
'SHAPE_Area': 5747.21023925478,
'geometry': gpd.GeoSeries(Polygon([
[591535.31, 6740542.8499],
[591546.79, 6740525.9499],
[591565.3179, 6740498.2083],
[591527.4193, 6740467.2872],
[591516.35, 6740466.1499],
[591471.27, 6740517.5099],
[591464.35, 6740525.3999],
[591494.5, 6740576.6499],
[591514.4, 6740554.4999],
[591525.4, 6740550.7899],
[591535.31, 6740542.8499],
]), index=['beryll'], crs=25832),
}
beryll
{'localid': 49174530, 'objtype': 'Teig', 'SHAPE_Length': 306.207095792018, 'SHAPE_Area': 5747.21023925478, 'geometry': beryll POLYGON ((591535.310 6740542.850, 591546.790 6... dtype: geometry}
# oppretter GeoDataFrame av dictionariet
df = gpd.GeoDataFrame(beryll)
df.info()
<class 'geopandas.geodataframe.GeoDataFrame'> Index: 1 entries, beryll to beryll Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 localid 1 non-null int64 1 objtype 1 non-null object 2 SHAPE_Length 1 non-null float64 3 SHAPE_Area 1 non-null float64 4 geometry 1 non-null geometry dtypes: float64(2), geometry(1), int64(1), object(1) memory usage: 48.0+ bytes
# slår sammen to GeoDataFrames til én
teiger = teiger.append(df)
teiger.info()
<class 'geopandas.geodataframe.GeoDataFrame'> Index: 2 entries, smaragd to beryll Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 localid 2 non-null int64 1 objtype 2 non-null object 2 SHAPE_Length 2 non-null float64 3 SHAPE_Area 2 non-null float64 4 geometry 2 non-null geometry dtypes: float64(2), geometry(1), int64(1), object(1) memory usage: 96.0+ bytes
teiger.head()
localid | objtype | SHAPE_Length | SHAPE_Area | geometry | |
---|---|---|---|---|---|
smaragd | 446124344 | Teig | 344.415338 | 7166.961750 | POLYGON ((591518.200 6740464.050, 591442.740 6... |
beryll | 49174530 | Teig | 306.207096 | 5747.210239 | POLYGON ((591535.310 6740542.850, 591546.790 6... |
teiger.plot('localid')
<AxesSubplot:>
# index er nøkkelverdi for radene
teiger.index
Index(['smaragd', 'beryll'], dtype='object')
# .loc[] brukes for oppslag på nøkkelverdi
teiger.loc['beryll']
localid 49174530 objtype Teig SHAPE_Length 306.207096 SHAPE_Area 5747.210239 geometry POLYGON ((591535.3100000001 6740542.8499, 5915... Name: beryll, dtype: object
# .columns viser kolonne-navn/egenskapsnavn
teiger.columns
Index(['localid', 'objtype', 'SHAPE_Length', 'SHAPE_Area', 'geometry'], dtype='object')
# for å få verdier fra én kolonne:
teiger['localid']
smaragd 446124344 beryll 49174530 Name: localid, dtype: int64
type(teiger.loc[:,:]) # type(teiger) hadde gjort samme nytten
geopandas.geodataframe.GeoDataFrame
type(teiger['geometry'])
geopandas.geoseries.GeoSeries
type(teiger.loc['smaragd', 'geometry'])
shapely.geometry.polygon.Polygon
print(teiger.loc['smaragd', 'geometry'])
print(teiger.loc['beryll', 'geometry'])
POLYGON ((591518.2 6740464.0499, 591442.74 6740401.1999, 591399.6899999999 6740452.8699, 591460.55 6740518.9299, 591464.35 6740525.3999, 591471.27 6740517.5099, 591516.35 6740466.1499, 591518.2 6740464.0499)) POLYGON ((591535.3100000001 6740542.8499, 591546.79 6740525.9499, 591565.3179 6740498.2083, 591527.4193 6740467.2872, 591516.35 6740466.1499, 591471.27 6740517.5099, 591464.35 6740525.3999, 591494.5 6740576.6499, 591514.4 6740554.4999, 591525.4 6740550.7899, 591535.3100000001 6740542.8499))
teiger.loc['beryll', 'geometry']
teiger.loc['smaragd', 'geometry']
teiger['geometry'].total_bounds
array([ 591399.69 , 6740401.1999, 591565.3179, 6740576.6499])