Playing with rasterio and fiona

Variable declarations

sample_points_filepath – path to sample points shapefile
DEM_filepath – path to DEM raster
elevation_filepath – path to export excel file containing elevation values for each sample site


In [2]:
sample_points_filepath = ""

In [4]:
DEM_filepath = ""

In [5]:
elevation_filepath = ""

Import statements


In [150]:
import rasterio
import fiona
import pandas
import numpy
from pyproj import Proj, transform
from fiona.crs import from_epsg

In [226]:
with fiona.open(sample_points_filepath, 'r') as source_points:
    points = [f['geometry']['coordinates'] for f in source_points]
    
    original = Proj(source_points.crs)
    destination = Proj(from_epsg(4326))
    #destination = Proj(' +proj=latlong +ellps=bessel')
    
    with rasterio.drivers():
        with rasterio.open(DEM_filepath) as source_dem:
            s = source_dem.sample(points)
            elevs = numpy.array([n[0] for n in s])
            source_dem.close
    source_points.close

Transform points


In [227]:
points_projected = []
for p in points:
    x, y = p
    lat, long = transform(original, destination, x, y)
    points_projected.append((long,lat))

In [228]:
points_projected_pd = pandas.DataFrame(points_projected, columns=["lat", "long"])

In [ ]:


In [229]:
with fiona.open(sample_points_filepath, 'r') as source_points:
    names = numpy.array([p['properties']['NAME'] for p in source_points])
    IDs = numpy.array([p['properties']['ID'] for p in source_points])
    
    source_points.close

In [230]:
elevs_names = [{"ID":IDs[i],"elevation":elevs[i], "name":names[i], "latitude":points_projected[i][0], "longitude":points_projected[i][1]} for i in range(len(elevs))]

In [231]:
elevs_pd = pandas.DataFrame(elevs_names)

In [232]:
elevs_pd


Out[232]:
ID elevation latitude longitude name
0 2 665 47.267357 11.305684 Hofwald
1 103 768 47.270798 11.334081 Kerschbuchhof
2 4 725 47.270846 11.336290 Kranebitten
3 25 717 47.272745 11.358701 Buzihütte
4 27 698 47.272116 11.368856 Sadrach
5 7 639 47.267641 11.366170 Höttinger Rain
6 102 687 47.270875 11.374611 Schießstand
7 101 607 47.268002 11.378812 Sternwarte
8 22 906 47.288550 11.398594 Hungerburg
9 104 906 47.288532 11.398566 Hungerburg
10 14 868 47.293794 11.425235 Pumhof
11 105 841 47.292865 11.421865 Klamm
12 24 852 47.293343 11.431416 Rechenhof
13 13 726 47.289920 11.413216 Schillerhof
14 3 617 47.283083 11.428208 Arzler Straße
15 12 646 47.281084 11.399082 Alpenzoo
16 15 573 47.273462 11.397802 Hofgarten
17 26 575 47.272898 11.409550 Bienenstraße
18 5 579 47.264688 11.410329 Gumppstraße
19 21 581 47.260373 11.394184 Templstraße
20 18 576 47.255969 11.381954 Egger-Lienz-Straße
21 29 568 47.267725 11.430994 Rossau
22 10 693 47.247054 11.359071 Kosterberg
23 106 697 47.246986 11.358715 Mentlberg
24 107 837 47.242746 11.355434 Zur Eiche
25 30 752 47.248343 11.387784 Wittenberg
26 13 693 47.250833 11.388307 Andreas-Hofer
27 108 697 47.251008 11.387702 Andreas-Hofer
28 20 592 47.253766 11.399683 Wilten
29 109 757 47.235335 11.396415 Handlhof
30 10 726 47.231205 11.394951 Brenner
31 8 855 47.228640 11.400954 Igls
32 11 938 47.228642 11.420374 Ullwald
33 1 896 47.235221 11.424500 Lans
34 16 832 47.235003 11.404602 Vill
35 23 800 47.247155 11.409858 Poltenweg
36 110 800 47.247156 11.409861 Poltenweg
37 17 804 47.248124 11.425742 Golfcourse A
38 9 783 47.248622 11.427050 Golfcourse B
39 6 650 47.259881 11.449681 Pfaffensteig
40 111 656 47.257543 11.438292 Schloss Ambras

In [233]:
elevs_pd.to_excel(elevation_filepath)