├── .gitignore ├── README.md ├── code └── final │ ├── README.md │ ├── raster │ ├── README.md │ ├── img │ │ └── mslp_europe.png │ └── mslpeurope.py │ └── vector │ ├── README.md │ ├── buffertest.py │ ├── compbuffer.py │ └── img │ └── buffer.png ├── data ├── README.md ├── clima │ ├── mf06_clima.cpg │ ├── mf06_clima.dbf │ ├── mf06_clima.prj │ ├── mf06_clima.sbn │ ├── mf06_clima.sbx │ ├── mf06_clima.shp │ ├── mf06_clima.shp.xml │ └── mf06_clima.shx ├── geologia_marina │ ├── mm01_geologia.cpg │ ├── mm01_geologia.dbf │ ├── mm01_geologia.prj │ ├── mm01_geologia.sbn │ ├── mm01_geologia.sbx │ ├── mm01_geologia.shp │ ├── mm01_geologia.shp.xml │ └── mm01_geologia.shx ├── mde │ └── h10_1050_2-2 │ │ ├── h10_1050_2-2.aux │ │ ├── h10_1050_2-2.tfw │ │ └── h10_1050_2-2.tif ├── mslp_gfs │ ├── mslp │ ├── mslp.tif │ └── mslp_europe.nc ├── ne_110m_coastline │ ├── ne_110m_coastline.README.html │ ├── ne_110m_coastline.VERSION.txt │ ├── ne_110m_coastline.dbf │ ├── ne_110m_coastline.prj │ ├── ne_110m_coastline.shp │ └── ne_110m_coastline.shx ├── sampledata │ ├── samples.dbf │ ├── samples.prj │ ├── samples.qpj │ ├── samples.shp │ └── samples.shx ├── temp_gfs │ ├── tmp_2m_k_20150903.tif │ └── tmp_80m_k_20150903.tif └── world_emissions │ └── undata_emisspcap.csv ├── docs └── pdf │ ├── 01-introduccion.pdf │ ├── 02-Conceptos_basicos_python_I.pdf │ ├── 03-Conceptos_basicos_python_II.pdf │ ├── 04-python_comput_cientif.pdf │ ├── 05-lectura_escrit_datos_geo.pdf │ ├── 06-Analisis_geoproc_python.pdf │ └── 07-QGIS_y_python.pdf └── notebooks ├── unit01 ├── img │ ├── github.png │ ├── github2.png │ ├── github3.png │ ├── ipython3shell.png │ ├── ipythonshell.png │ ├── python3shell.png │ ├── pythoncli.png │ └── pythonshell.png ├── unit01_01.ipynb ├── unit01_02.ipynb ├── unit01_03.ipynb ├── unit01_04.ipynb └── unit01_05.ipynb ├── unit02 ├── unit02_01.ipynb ├── unit02_02.ipynb ├── unit02_03.ipynb ├── unit02_04.ipynb ├── unit02_05.ipynb ├── unit02_06.ipynb ├── unit02_07.ipynb └── unit02_08.ipynb ├── unit03 ├── testfile.txt ├── unit03_01.ipynb ├── unit03_02.ipynb ├── unit03_03.ipynb ├── unit03_04.ipynb ├── unit03_05.ipynb ├── unit03_06.ipynb └── unit03_07.ipynb ├── unit04 ├── unit04_01.ipynb ├── unit04_02.ipynb ├── unit04_03.ipynb ├── unit04_04.ipynb ├── unit04_05.ipynb ├── unit04_06.ipynb ├── unit04_07.ipynb └── unit04_08.ipynb ├── unit05 ├── unit05_01.ipynb ├── unit05_02.ipynb ├── unit05_03.ipynb ├── unit05_04.ipynb ├── unit05_05.ipynb ├── unit05_06.ipynb ├── unit05_07.ipynb ├── unit05_08.ipynb └── unit05_09.ipynb ├── unit06 ├── unit06_01.ipynb ├── unit06_02.ipynb ├── unit06_03.ipynb ├── unit06_04.ipynb ├── unit06_05.ipynb ├── unit06_06.ipynb └── unit06_07.ipynb └── unit07 ├── img ├── bbox_builder.png ├── qgis_shell.png └── qgis_shell_editor.png ├── unit07_01.ipynb ├── unit07_02.ipynb └── unit07_03.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | # jupyter notebooks 2 | .ipynb_checkpoints/ 3 | 4 | # data 5 | *.aux.xml 6 | 7 | 8 | # Byte-compiled / optimized / DLL files 9 | __pycache__/ 10 | *.py[cod] 11 | *$py.class 12 | 13 | # C extensions 14 | *.so 15 | 16 | # Distribution / packaging 17 | .Python 18 | env/ 19 | build/ 20 | develop-eggs/ 21 | dist/ 22 | downloads/ 23 | eggs/ 24 | .eggs/ 25 | lib/ 26 | lib64/ 27 | parts/ 28 | sdist/ 29 | var/ 30 | *.egg-info/ 31 | .installed.cfg 32 | *.egg 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *,cover 53 | .hypothesis/ 54 | 55 | # Translations 56 | *.mo 57 | *.pot 58 | 59 | # Django stuff: 60 | *.log 61 | local_settings.py 62 | 63 | # Flask instance folder 64 | instance/ 65 | 66 | # Scrapy stuff: 67 | .scrapy 68 | 69 | # Sphinx documentation 70 | docs/_build/ 71 | 72 | # PyBuilder 73 | target/ 74 | 75 | # IPython Notebook 76 | .ipynb_checkpoints 77 | 78 | # pyenv 79 | .python-version 80 | 81 | # celery beat schedule file 82 | celerybeat-schedule 83 | 84 | # dotenv 85 | .env 86 | 87 | # virtualenv 88 | venv/ 89 | ENV/ 90 | 91 | # Spyder project settings 92 | .spyderproject 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GeoPython Lessons 2 | 3 | __Introduction to geoprocessing data with Python.__ 4 | 5 | April 2016: 6 | [Laboratory of GIS and Remote Sensing (LAST) - Doñana Biological Station](http://www.ebd.csic.es/laboratorio-sig-y-teledeteccion-last). 7 | 8 | Author: [Cayetano Benavent](https://github.com/cayetanobv) - GIS Analyst at [Geographica](http://www.geographica.gs/). 9 | 10 | 11 | ### Unit01 Introduction to Python 12 | - [Introduction to Python - pdf](docs/pdf/01-introduccion.pdf) 13 | 14 | __Notebooks__ 15 | - [Notebook - unit01_01](notebooks/unit01/unit01_01.ipynb) 16 | - [Notebook - unit01_02](notebooks/unit01/unit01_02.ipynb) 17 | - [Notebook - unit01_03](notebooks/unit01/unit01_03.ipynb) 18 | - [Notebook - unit01_04](notebooks/unit01/unit01_04.ipynb) 19 | - [Notebook - unit01_05](notebooks/unit01/unit01_05.ipynb) 20 | 21 | ### Unit02 Basic concepts of Python I 22 | - [Basic concepts of Python I - pdf](docs/pdf/02-Conceptos_basicos_python_I.pdf) 23 | 24 | __Notebooks__ 25 | - [Notebook - unit02_01](notebooks/unit02/unit02_01.ipynb) 26 | - [Notebook - unit02_02](notebooks/unit02/unit02_02.ipynb) 27 | - [Notebook - unit02_03](notebooks/unit02/unit02_03.ipynb) 28 | - [Notebook - unit02_04](notebooks/unit02/unit02_04.ipynb) 29 | - [Notebook - unit02_05](notebooks/unit02/unit02_05.ipynb) 30 | - [Notebook - unit02_06](notebooks/unit02/unit02_06.ipynb) 31 | - [Notebook - unit02_07](notebooks/unit02/unit02_07.ipynb) 32 | - [Notebook - unit02_08](notebooks/unit02/unit02_08.ipynb) 33 | 34 | ### Unit03 Basic concepts of Python II 35 | - [Basic concepts of Python II - pdf](docs/pdf/03-Conceptos_basicos_python_II.pdf) 36 | 37 | __Notebooks__ 38 | - [Notebook - unit03_01](notebooks/unit03/unit03_01.ipynb) 39 | - [Notebook - unit03_02](notebooks/unit03/unit03_02.ipynb) 40 | - [Notebook - unit03_03](notebooks/unit03/unit03_03.ipynb) 41 | - [Notebook - unit03_04](notebooks/unit03/unit03_04.ipynb) 42 | - [Notebook - unit03_05](notebooks/unit03/unit03_05.ipynb) 43 | - [Notebook - unit03_06](notebooks/unit03/unit03_06.ipynb) 44 | - [Notebook - unit03_07](notebooks/unit03/unit03_07.ipynb) 45 | 46 | ### Unit04 Scientific computing with Python 47 | - [Scientific computing with Python - pdf](docs/pdf/04-python_comput_cientif.pdf) 48 | 49 | __Notebooks__ 50 | - [Notebook - unit04_01](notebooks/unit04/unit04_01.ipynb) 51 | - [Notebook - unit04_02](notebooks/unit04/unit04_02.ipynb) 52 | - [Notebook - unit04_03](notebooks/unit04/unit04_03.ipynb) 53 | - [Notebook - unit04_04](notebooks/unit04/unit04_04.ipynb) 54 | - [Notebook - unit04_05](notebooks/unit04/unit04_05.ipynb) 55 | - [Notebook - unit04_06](notebooks/unit04/unit04_06.ipynb) 56 | - [Notebook - unit04_07](notebooks/unit04/unit04_07.ipynb) 57 | - [Notebook - unit04_08](notebooks/unit04/unit04_08.ipynb) 58 | 59 | ### Unit05 Read and write geographic data 60 | - [Read and write geographic data - pdf](docs/pdf/05-lectura_escrit_datos_geo.pdf) 61 | 62 | __Notebooks__ 63 | - [Notebook - unit05_01](notebooks/unit05/unit05_01.ipynb) 64 | - [Notebook - unit05_02](notebooks/unit05/unit05_02.ipynb) 65 | - [Notebook - unit05_03](notebooks/unit05/unit05_03.ipynb) 66 | - [Notebook - unit05_04](notebooks/unit05/unit05_04.ipynb) 67 | - [Notebook - unit05_05](notebooks/unit05/unit05_05.ipynb) 68 | - [Notebook - unit05_06](notebooks/unit05/unit05_06.ipynb) 69 | - [Notebook - unit05_07](notebooks/unit05/unit05_07.ipynb) 70 | - [Notebook - unit05_08](notebooks/unit05/unit05_08.ipynb) 71 | - [Notebook - unit05_09](notebooks/unit05/unit05_09.ipynb) 72 | 73 | ### Unit06 Geographic data analysis 74 | - [Geographic data analysis - pdf](docs/pdf/06-Analisis_geoproc_python.pdf) 75 | 76 | __Notebooks__ 77 | - [Notebook - unit06_01](notebooks/unit06/unit06_01.ipynb) 78 | - [Notebook - unit06_02](notebooks/unit06/unit06_02.ipynb) 79 | - [Notebook - unit06_03](notebooks/unit06/unit06_03.ipynb) 80 | - [Notebook - unit06_04](notebooks/unit06/unit06_04.ipynb) 81 | - [Notebook - unit06_05](notebooks/unit06/unit06_05.ipynb) 82 | - [Notebook - unit06_06](notebooks/unit06/unit06_06.ipynb) 83 | - [Notebook - unit06_07](notebooks/unit06/unit06_07.ipynb) 84 | 85 | ### Unit07 QGIS and Python 86 | - [QGIS and Python - pdf](docs/pdf/07-QGIS_y_python.pdf) 87 | 88 | __Notebooks__ 89 | - [Notebook - unit07_01](notebooks/unit07/unit07_01.ipynb) 90 | - [Notebook - unit07_02](notebooks/unit07/unit07_02.ipynb) 91 | - [Notebook - unit07_03](notebooks/unit07/unit07_03.ipynb) 92 | 93 | ### Final Practice exercises 94 | - [Final - vector](code/final/vector) 95 | - [Final - raster](code/final/raster) 96 | -------------------------------------------------------------------------------- /code/final/README.md: -------------------------------------------------------------------------------- 1 | # Final practice 2 | 3 | 1. Vector. 4 | 2. Raster. 5 | -------------------------------------------------------------------------------- /code/final/raster/README.md: -------------------------------------------------------------------------------- 1 | # Final raster exercise 2 | 3 | Input data: 4 | - data/mslp_gfs/mslp_europe.nc 5 | - NetCDF dataset. 6 | - Source dataset: subsetted and translated NOAA GFS Grib data. 7 | - Source dataset unit: Pa 8 | 9 | Results: 10 | - Translate NetCDF dataset to GeoTiff. 11 | - Destiny CRS: ETRS89 / LCC Europe (EPSG:3034). 12 | - Destiny dataset unit: hPa 13 | 14 | Solved practical exercise: 15 | [mslpeurope.py](mslpeurope.py) 16 | 17 | ![mslpeurope](img/mslp_europe.png) 18 | -------------------------------------------------------------------------------- /code/final/raster/img/mslp_europe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/code/final/raster/img/mslp_europe.png -------------------------------------------------------------------------------- /code/final/raster/mslpeurope.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Author: Cayetano Benavent, 2016. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 2 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 | # MA 02110-1301, USA. 19 | # 20 | 21 | 22 | import os 23 | import shutil 24 | import rasterio 25 | from rasterio.warp import calculate_default_transform, reproject, RESAMPLING 26 | 27 | 28 | 29 | def createOutFolder(out_flname): 30 | try: 31 | outdir = os.path.dirname(out_flname) 32 | if not os.path.exists(outdir): 33 | os.makedirs(outdir) 34 | else: 35 | shutil.rmtree(outdir) 36 | os.makedirs(outdir) 37 | 38 | print("Output folder created") 39 | 40 | except Exception as error: 41 | print("Error creating out folder: {}".format(error)) 42 | 43 | 44 | def run(input_file, out_file, dst_crs): 45 | try: 46 | print("Starting proccess...") 47 | 48 | createOutFolder(out_file) 49 | 50 | with rasterio.open(input_file) as src: 51 | 52 | affine, width, height = calculate_default_transform( 53 | src.crs, dst_crs, src.width, src.height,*src.bounds) 54 | kwargs = src.meta.copy() 55 | kwargs.update({ 56 | 'crs': dst_crs, 57 | 'transform': affine, 58 | 'affine': affine, 59 | 'width': width, 60 | 'height': height, 61 | 'compress': 'lzw', 62 | 'nodata': 0 63 | }) 64 | 65 | with rasterio.open(out_file, 'w', **kwargs) as dst: 66 | 67 | reproject( 68 | source=rasterio.band(src, 1), 69 | destination=rasterio.band(dst, 1), 70 | src_transform=src.affine, 71 | src_crs=src.crs, 72 | dst_transform=affine, 73 | dst_crs=dst_crs, 74 | resampling=RESAMPLING.nearest) 75 | 76 | for i in dst.indexes: 77 | band_dst = dst.read(i) / 100 78 | dst.write_band(i, band_dst) 79 | 80 | print("Successfully finished proccess!") 81 | 82 | except Exception as error: 83 | print("Error creating out folder: {}".format(error)) 84 | 85 | def main(): 86 | input_file = "../../../data/mslp_gfs/mslp_europe.nc" 87 | out_file = "/tmp/res_raster/mslp_europe_rst.tif" 88 | dst_crs = 'EPSG:3034' 89 | run(input_file, out_file, dst_crs) 90 | 91 | if __name__ == '__main__': 92 | main() 93 | -------------------------------------------------------------------------------- /code/final/vector/README.md: -------------------------------------------------------------------------------- 1 | # Final vector exercise 2 | 3 | Input data: 4 | - data/sampledata/samples.shp 5 | - Shapefile data. 6 | - Geometry: Point. 7 | 8 | Results: 9 | - Compute Buffer area: 500 m. 10 | - Geometry: Polygon. 11 | - Filter features: 'ma' string in field 'name'. 12 | 13 | Solved practical exercise: 14 | - [buffertest.py](buffertest.py) 15 | - [compbuffer.py](compbuffer.py) 16 | 17 | ![buffer](img/buffer.png) 18 | -------------------------------------------------------------------------------- /code/final/vector/buffertest.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Author: Cayetano Benavent, 2016. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 2 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 | # MA 02110-1301, USA. 19 | # 20 | 21 | import compbuffer 22 | 23 | def main(): 24 | in_flname = "../../../data/sampledata/samples.shp" 25 | out_flname = "/tmp/results/buffsamples.shp" 26 | dist = 500 27 | field_fltr = 'name' 28 | find_str = 'ma' 29 | 30 | compbuffer.run(in_flname, out_flname, dist, field_fltr, find_str) 31 | 32 | if __name__ == '__main__': 33 | main() 34 | -------------------------------------------------------------------------------- /code/final/vector/compbuffer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Author: Cayetano Benavent, 2016. 4 | # 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 2 of the License, or 8 | # (at your option) any later version. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 | # MA 02110-1301, USA. 19 | # 20 | 21 | import os 22 | import shutil 23 | import fiona 24 | from shapely.geometry import shape, mapping 25 | 26 | 27 | def createOutFolder(out_flname): 28 | try: 29 | outdir = os.path.dirname(out_flname) 30 | if not os.path.exists(outdir): 31 | os.makedirs(outdir) 32 | else: 33 | shutil.rmtree(outdir) 34 | os.makedirs(outdir) 35 | 36 | print("Output folder created") 37 | 38 | except Exception as error: 39 | print("Error creating out folder: {}".format(error)) 40 | 41 | def run(in_flname, out_flname, dist, fld, fltr): 42 | try: 43 | print("Starting proccess...") 44 | 45 | createOutFolder(out_flname) 46 | 47 | with fiona.open(in_flname, 'r') as in_ds: 48 | in_drv = in_ds.driver 49 | in_crs = in_ds.crs 50 | in_sch = in_ds.schema 51 | 52 | out_schema = {'geometry': 'Polygon', 53 | 'properties': in_sch.get('properties')} 54 | 55 | with fiona.open(out_flname, 'w', 56 | driver=in_drv, 57 | crs=in_crs, 58 | schema=out_schema) as out_ds: 59 | for ft in in_ds: 60 | if fltr in ft['properties'].get(fld): 61 | geom_shl = shape(ft.get('geometry')).buffer(dist) 62 | out_ds.write({'geometry': mapping(geom_shl), 63 | 'properties': ft.get('properties'), 64 | 'type': ft.get('type'), 65 | 'id': ft.get('id')}) 66 | 67 | print("Buffer successfully created!") 68 | 69 | except Exception as error: 70 | print("Error computing Buffer: {}".format(error)) 71 | -------------------------------------------------------------------------------- /code/final/vector/img/buffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/code/final/vector/img/buffer.png -------------------------------------------------------------------------------- /data/README.md: -------------------------------------------------------------------------------- 1 | #Data sources 2 | 3 | - ne_110m_coastline 4 | - Source: Natural Earth 5 | - mslp_gfs 6 | - National Oceanic and Atmospheric Administration (NOAA) 7 | - temp_gfs 8 | - National Oceanic and Atmospheric Administration (NOAA) 9 | - world_emissions 10 | - Source: United Nations. 11 | - clima climate 12 | - Source: Institute of Statistics and Cartography of Andalusia 13 | - geologia_marina 14 | - Source: Institute of Statistics and Cartography of Andalusia 15 | - mde/h10_1050_2-2 16 | - Source: Institute of Statistics and Cartography of Andalusia 17 | - sampledata 18 | - Source: Cayetano Benavent. 19 | -------------------------------------------------------------------------------- /data/clima/mf06_clima.cpg: -------------------------------------------------------------------------------- 1 | UTF-8 -------------------------------------------------------------------------------- /data/clima/mf06_clima.dbf: -------------------------------------------------------------------------------- 1 | s a-TIPO_CLIMC(COD_ENTC Mediterráneo Continental f10 Continental Mediterráneo f10 Mediterráneo Subdesértico f10 Mediterráneo Subtropical f10 Mediterráneo Oceánico f10 Alta montaña f10  -------------------------------------------------------------------------------- /data/clima/mf06_clima.prj: -------------------------------------------------------------------------------- 1 | PROJCS["ETRS89 / UTM zone 30N",GEOGCS["ETRS89",DATUM["European_Terrestrial_Reference_System_1989",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6258"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4258"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","25830"]] -------------------------------------------------------------------------------- /data/clima/mf06_clima.sbn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/clima/mf06_clima.sbn -------------------------------------------------------------------------------- /data/clima/mf06_clima.sbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/clima/mf06_clima.sbx -------------------------------------------------------------------------------- /data/clima/mf06_clima.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/clima/mf06_clima.shp -------------------------------------------------------------------------------- /data/clima/mf06_clima.shp.xml: -------------------------------------------------------------------------------- 1 | 2 | 20150312124454001.0FALSEmf06_clima0020,000file://\\IECA-1444\C$\DERA_UTF8\G04_Medio_Fisico\mf06_clima.shpLocal Area NetworkProjectedGCS_ETRS_1989ETRS_1989_UTM_Zone_30N<ProjectedCoordinateSystem xsi:type='typens:ProjectedCoordinateSystem' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/10.0'><WKT>PROJCS[&quot;ETRS_1989_UTM_Zone_30N&quot;,GEOGCS[&quot;GCS_ETRS_1989&quot;,DATUM[&quot;D_ETRS_1989&quot;,SPHEROID[&quot;GRS_1980&quot;,6378137.0,298.257222101]],PRIMEM[&quot;Greenwich&quot;,0.0],UNIT[&quot;Degree&quot;,0.0174532925199433]],PROJECTION[&quot;Transverse_Mercator&quot;],PARAMETER[&quot;False_Easting&quot;,500000.0],PARAMETER[&quot;False_Northing&quot;,0.0],PARAMETER[&quot;Central_Meridian&quot;,-3.0],PARAMETER[&quot;Scale_Factor&quot;,0.9996],PARAMETER[&quot;Latitude_Of_Origin&quot;,0.0],UNIT[&quot;Meter&quot;,1.0],AUTHORITY[&quot;EPSG&quot;,25830]]</WKT><XOrigin>-5120900</XOrigin><YOrigin>-9998100</YOrigin><XYScale>450445547.3910538</XYScale><ZOrigin>-100000</ZOrigin><ZScale>10000</ZScale><MOrigin>-100000</MOrigin><MScale>10000</MScale><XYTolerance>0.001</XYTolerance><ZTolerance>0.001</ZTolerance><MTolerance>0.001</MTolerance><HighPrecision>true</HighPrecision><WKID>25830</WKID></ProjectedCoordinateSystem>CopyFeatures k:/DERA\G04_Medio_Fisico/mf06_clima.shp c:\DERA_UTF8\G04_Medio_Fisico\mf06_clima.shp # 0 0 020150312124454002015031212445400Microsoft Windows Server 2008 R2 Version 6.1 (Build 7601) Service Pack 1; ESRI ArcGIS 10.0.0.2414mf06_clima002Shapefile0,000datasetEPSG7.4.1Simple4FALSE0FALSEFALSE0mf06_climaFeature Class0FIDFIDOID400Internal feature number.ESRISequential unique whole numbers that are automatically generated.ShapeShapeGeometry000Feature geometry.ESRICoordinates defining the features.TIPO_CLIMTIPO_CLIMString4000COD_ENTCOD_ENTString40020150312 3 | -------------------------------------------------------------------------------- /data/clima/mf06_clima.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/clima/mf06_clima.shx -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.cpg: -------------------------------------------------------------------------------- 1 | UTF-8 -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/geologia_marina/mm01_geologia.dbf -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.prj: -------------------------------------------------------------------------------- 1 | PROJCS["ETRS89 / UTM zone 30N",GEOGCS["ETRS89",DATUM["European_Terrestrial_Reference_System_1989",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6258"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4258"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","25830"]] -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.sbn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/geologia_marina/mm01_geologia.sbn -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.sbx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/geologia_marina/mm01_geologia.sbx -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/geologia_marina/mm01_geologia.shp -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.shp.xml: -------------------------------------------------------------------------------- 1 | 2 | 20150312124457001.0FALSEmm01_geologia0020,000file://\\IECA-1444\C$\DERA_UTF8\G05_Medio_Marino\mm01_geologia.shpLocal Area NetworkProjectedGCS_ETRS_1989ETRS_1989_UTM_Zone_30N<ProjectedCoordinateSystem xsi:type='typens:ProjectedCoordinateSystem' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/10.0'><WKT>PROJCS[&quot;ETRS_1989_UTM_Zone_30N&quot;,GEOGCS[&quot;GCS_ETRS_1989&quot;,DATUM[&quot;D_ETRS_1989&quot;,SPHEROID[&quot;GRS_1980&quot;,6378137.0,298.257222101]],PRIMEM[&quot;Greenwich&quot;,0.0],UNIT[&quot;Degree&quot;,0.0174532925199433]],PROJECTION[&quot;Transverse_Mercator&quot;],PARAMETER[&quot;False_Easting&quot;,500000.0],PARAMETER[&quot;False_Northing&quot;,0.0],PARAMETER[&quot;Central_Meridian&quot;,-3.0],PARAMETER[&quot;Scale_Factor&quot;,0.9996],PARAMETER[&quot;Latitude_Of_Origin&quot;,0.0],UNIT[&quot;Meter&quot;,1.0],AUTHORITY[&quot;EPSG&quot;,25830]]</WKT><XOrigin>-5120900</XOrigin><YOrigin>-9998100</YOrigin><XYScale>450445547.3910538</XYScale><ZOrigin>-100000</ZOrigin><ZScale>10000</ZScale><MOrigin>-100000</MOrigin><MScale>10000</MScale><XYTolerance>0.001</XYTolerance><ZTolerance>0.001</ZTolerance><MTolerance>0.001</MTolerance><HighPrecision>true</HighPrecision><WKID>25830</WKID></ProjectedCoordinateSystem>CopyFeatures k:/DERA\G05_Medio_Marino/mm01_geologia.shp c:\DERA_UTF8\G05_Medio_Marino\mm01_geologia.shp # 0 0 020150312124457002015031212445700Microsoft Windows Server 2008 R2 Version 6.1 (Build 7601) Service Pack 1; ESRI ArcGIS 10.0.0.2414mm01_geologia002Shapefile0,000datasetEPSG7.4.1Simple4FALSE0FALSEFALSE0mm01_geologiaFeature Class0FIDFIDOID400Internal feature number.ESRISequential unique whole numbers that are automatically generated.ShapeShapeGeometry000Feature geometry.ESRICoordinates defining the features.DESCRIPCIODESCRIPCIOString5000COD_ENTCOD_ENTString100020150312 3 | -------------------------------------------------------------------------------- /data/geologia_marina/mm01_geologia.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/geologia_marina/mm01_geologia.shx -------------------------------------------------------------------------------- /data/mde/h10_1050_2-2/h10_1050_2-2.aux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/mde/h10_1050_2-2/h10_1050_2-2.aux -------------------------------------------------------------------------------- /data/mde/h10_1050_2-2/h10_1050_2-2.tfw: -------------------------------------------------------------------------------- 1 | 9.98712998712998790 2 | 0.00000000000000000 3 | 0.00000000000000000 4 | -9.98011928429423460 5 | 282294.99356499355000000 6 | 4074635.00994035790000000 7 | -------------------------------------------------------------------------------- /data/mde/h10_1050_2-2/h10_1050_2-2.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/mde/h10_1050_2-2/h10_1050_2-2.tif -------------------------------------------------------------------------------- /data/mslp_gfs/mslp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/mslp_gfs/mslp -------------------------------------------------------------------------------- /data/mslp_gfs/mslp.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/mslp_gfs/mslp.tif -------------------------------------------------------------------------------- /data/mslp_gfs/mslp_europe.nc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/mslp_gfs/mslp_europe.nc -------------------------------------------------------------------------------- /data/ne_110m_coastline/ne_110m_coastline.VERSION.txt: -------------------------------------------------------------------------------- 1 | 2.0.0 -------------------------------------------------------------------------------- /data/ne_110m_coastline/ne_110m_coastline.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/ne_110m_coastline/ne_110m_coastline.dbf -------------------------------------------------------------------------------- /data/ne_110m_coastline/ne_110m_coastline.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.017453292519943295]] -------------------------------------------------------------------------------- /data/ne_110m_coastline/ne_110m_coastline.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/ne_110m_coastline/ne_110m_coastline.shp -------------------------------------------------------------------------------- /data/ne_110m_coastline/ne_110m_coastline.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/ne_110m_coastline/ne_110m_coastline.shx -------------------------------------------------------------------------------- /data/sampledata/samples.dbf: -------------------------------------------------------------------------------- 1 | _aidN 2 | nameC 1ma01 2dc01 3ma02 4dc02 5ma03 6ma04 -------------------------------------------------------------------------------- /data/sampledata/samples.prj: -------------------------------------------------------------------------------- 1 | PROJCS["ETRS89_UTM_zone_30N",GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1]] -------------------------------------------------------------------------------- /data/sampledata/samples.qpj: -------------------------------------------------------------------------------- 1 | PROJCS["ETRS89 / UTM zone 30N",GEOGCS["ETRS89",DATUM["European_Terrestrial_Reference_System_1989",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6258"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4258"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","25830"]] 2 | -------------------------------------------------------------------------------- /data/sampledata/samples.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/sampledata/samples.shp -------------------------------------------------------------------------------- /data/sampledata/samples.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/sampledata/samples.shx -------------------------------------------------------------------------------- /data/temp_gfs/tmp_2m_k_20150903.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/temp_gfs/tmp_2m_k_20150903.tif -------------------------------------------------------------------------------- /data/temp_gfs/tmp_80m_k_20150903.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/data/temp_gfs/tmp_80m_k_20150903.tif -------------------------------------------------------------------------------- /docs/pdf/01-introduccion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/docs/pdf/01-introduccion.pdf -------------------------------------------------------------------------------- /docs/pdf/02-Conceptos_basicos_python_I.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/docs/pdf/02-Conceptos_basicos_python_I.pdf -------------------------------------------------------------------------------- /docs/pdf/03-Conceptos_basicos_python_II.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/docs/pdf/03-Conceptos_basicos_python_II.pdf -------------------------------------------------------------------------------- /docs/pdf/04-python_comput_cientif.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/docs/pdf/04-python_comput_cientif.pdf -------------------------------------------------------------------------------- /docs/pdf/05-lectura_escrit_datos_geo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/docs/pdf/05-lectura_escrit_datos_geo.pdf -------------------------------------------------------------------------------- /docs/pdf/06-Analisis_geoproc_python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/docs/pdf/06-Analisis_geoproc_python.pdf -------------------------------------------------------------------------------- /docs/pdf/07-QGIS_y_python.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/docs/pdf/07-QGIS_y_python.pdf -------------------------------------------------------------------------------- /notebooks/unit01/img/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/github.png -------------------------------------------------------------------------------- /notebooks/unit01/img/github2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/github2.png -------------------------------------------------------------------------------- /notebooks/unit01/img/github3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/github3.png -------------------------------------------------------------------------------- /notebooks/unit01/img/ipython3shell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/ipython3shell.png -------------------------------------------------------------------------------- /notebooks/unit01/img/ipythonshell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/ipythonshell.png -------------------------------------------------------------------------------- /notebooks/unit01/img/python3shell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/python3shell.png -------------------------------------------------------------------------------- /notebooks/unit01/img/pythoncli.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/pythoncli.png -------------------------------------------------------------------------------- /notebooks/unit01/img/pythonshell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit01/img/pythonshell.png -------------------------------------------------------------------------------- /notebooks/unit01/unit01_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Git + GitHub + Jupyter Notebooks" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "First, we go to this GitHub repository:\n", 22 | "\n", 23 | "https://github.com/GeographicaGS/geopython-lessons" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "Copy to clipboard git URL from GitHub toolbar (see screenshot):\n", 31 | "\n", 32 | " https://github.com/GeographicaGS/geopython-lessons.git https://github.com/cayetanobv/geopython-lessons.git\n", 33 | "\n", 34 | "![Github](img/github.png)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "Go to command line and type (go to the folder where you want to store notebooks):\n", 42 | "\n", 43 | "```bash\n", 44 | "$ cd /mylocalfolder\n", 45 | "$ git clone https://github.com/GeographicaGS/geopython-lessons.git\n", 46 | "```" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "And now type in command line to run jupyter-notebook:\n", 54 | "```bash\n", 55 | "$ cd geopython-lessons/notebooks\n", 56 | "$ jupyter-notebook\n", 57 | "```" 58 | ] 59 | } 60 | ], 61 | "metadata": { 62 | "kernelspec": { 63 | "display_name": "Python 3", 64 | "language": "python", 65 | "name": "python3" 66 | }, 67 | "language_info": { 68 | "codemirror_mode": { 69 | "name": "ipython", 70 | "version": 3 71 | }, 72 | "file_extension": ".py", 73 | "mimetype": "text/x-python", 74 | "name": "python", 75 | "nbconvert_exporter": "python", 76 | "pygments_lexer": "ipython3", 77 | "version": "3.4.3" 78 | } 79 | }, 80 | "nbformat": 4, 81 | "nbformat_minor": 0 82 | } 83 | -------------------------------------------------------------------------------- /notebooks/unit01/unit01_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Python Interpreter" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "To open the Python interpreter type in the shell:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "```\n", 29 | "$ python\n", 30 | "```" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "You must see something like this:\n", 38 | "\n", 39 | "![Python shell](img/pythonshell.png)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "On Unix-like environments to open a Python 3 interpreter you must type:\n", 47 | "```\n", 48 | "$ python3\n", 49 | "```\n", 50 | "\n", 51 | "You must see now something like this:\n", 52 | "\n", 53 | "![Python 3 shell](img/python3shell.png)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "You can type complete version to open interpreter (e.g.: version 3.4):\n", 61 | "```\n", 62 | "$ python3.4\n", 63 | "```" 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "When you see the prompt (```>>>```), type:" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 7, 76 | "metadata": { 77 | "collapsed": false 78 | }, 79 | "outputs": [ 80 | { 81 | "name": "stdout", 82 | "output_type": "stream", 83 | "text": [ 84 | "Hello, world!\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "print(\"Hello, world!\")" 90 | ] 91 | }, 92 | { 93 | "cell_type": "markdown", 94 | "metadata": {}, 95 | "source": [ 96 | "To show online help:" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 8, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "Help on built-in function print in module builtins:\n", 111 | "\n", 112 | "print(...)\n", 113 | " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", 114 | " \n", 115 | " Prints the values to a stream, or to sys.stdout by default.\n", 116 | " Optional keyword arguments:\n", 117 | " file: a file-like object (stream); defaults to the current sys.stdout.\n", 118 | " sep: string inserted between values, default a space.\n", 119 | " end: string appended after the last value, default a newline.\n", 120 | " flush: whether to forcibly flush the stream.\n", 121 | "\n" 122 | ] 123 | } 124 | ], 125 | "source": [ 126 | "help(print)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "Another interpreter command is dir(). This command shows all the variables and functions defined in the current scope:" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 9, 139 | "metadata": { 140 | "collapsed": false 141 | }, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "['In',\n", 147 | " 'Out',\n", 148 | " '_',\n", 149 | " '_3',\n", 150 | " '_5',\n", 151 | " '__',\n", 152 | " '___',\n", 153 | " '__builtin__',\n", 154 | " '__builtins__',\n", 155 | " '__doc__',\n", 156 | " '__loader__',\n", 157 | " '__name__',\n", 158 | " '__package__',\n", 159 | " '__spec__',\n", 160 | " '_dh',\n", 161 | " '_i',\n", 162 | " '_i1',\n", 163 | " '_i2',\n", 164 | " '_i3',\n", 165 | " '_i4',\n", 166 | " '_i5',\n", 167 | " '_i6',\n", 168 | " '_i7',\n", 169 | " '_i8',\n", 170 | " '_i9',\n", 171 | " '_ih',\n", 172 | " '_ii',\n", 173 | " '_iii',\n", 174 | " '_oh',\n", 175 | " '_sh',\n", 176 | " 'exit',\n", 177 | " 'get_ipython',\n", 178 | " 'hello',\n", 179 | " 'quit']" 180 | ] 181 | }, 182 | "execution_count": 9, 183 | "metadata": {}, 184 | "output_type": "execute_result" 185 | } 186 | ], 187 | "source": [ 188 | "dir()" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 10, 194 | "metadata": { 195 | "collapsed": true 196 | }, 197 | "outputs": [], 198 | "source": [ 199 | "hello = \"Hello, world!\"" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "Again type dir() and we can see variable ```hello```:" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 11, 212 | "metadata": { 213 | "collapsed": false 214 | }, 215 | "outputs": [ 216 | { 217 | "data": { 218 | "text/plain": [ 219 | "['In',\n", 220 | " 'Out',\n", 221 | " '_',\n", 222 | " '_3',\n", 223 | " '_5',\n", 224 | " '_9',\n", 225 | " '__',\n", 226 | " '___',\n", 227 | " '__builtin__',\n", 228 | " '__builtins__',\n", 229 | " '__doc__',\n", 230 | " '__loader__',\n", 231 | " '__name__',\n", 232 | " '__package__',\n", 233 | " '__spec__',\n", 234 | " '_dh',\n", 235 | " '_i',\n", 236 | " '_i1',\n", 237 | " '_i10',\n", 238 | " '_i11',\n", 239 | " '_i2',\n", 240 | " '_i3',\n", 241 | " '_i4',\n", 242 | " '_i5',\n", 243 | " '_i6',\n", 244 | " '_i7',\n", 245 | " '_i8',\n", 246 | " '_i9',\n", 247 | " '_ih',\n", 248 | " '_ii',\n", 249 | " '_iii',\n", 250 | " '_oh',\n", 251 | " '_sh',\n", 252 | " 'exit',\n", 253 | " 'get_ipython',\n", 254 | " 'hello',\n", 255 | " 'quit']" 256 | ] 257 | }, 258 | "execution_count": 11, 259 | "metadata": {}, 260 | "output_type": "execute_result" 261 | } 262 | ], 263 | "source": [ 264 | "dir()" 265 | ] 266 | }, 267 | { 268 | "cell_type": "markdown", 269 | "metadata": {}, 270 | "source": [ 271 | "To close the interpreter press keys CTRL+D." 272 | ] 273 | } 274 | ], 275 | "metadata": { 276 | "kernelspec": { 277 | "display_name": "Python 3", 278 | "language": "python", 279 | "name": "python3" 280 | }, 281 | "language_info": { 282 | "codemirror_mode": { 283 | "name": "ipython", 284 | "version": 3 285 | }, 286 | "file_extension": ".py", 287 | "mimetype": "text/x-python", 288 | "name": "python", 289 | "nbconvert_exporter": "python", 290 | "pygments_lexer": "ipython3", 291 | "version": "3.4.3" 292 | } 293 | }, 294 | "nbformat": 4, 295 | "nbformat_minor": 0 296 | } 297 | -------------------------------------------------------------------------------- /notebooks/unit01/unit01_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Executing Python scripts from the command line" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Open a text editor and type:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "```python\n", 29 | "print(\"Hello, world!\")\n", 30 | "```" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "Save file to disk with this name: \n", 38 | "\n", 39 | " helloworld.py\n", 40 | " \n", 41 | "*Remark: If we are in a Unix-like OS, we can put a shebang line as first line of the script:\n", 42 | "\n", 43 | "```#! /usr/bin/env python```" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "Open again the command line interface and type:\n", 51 | " \n", 52 | "```\n", 53 | "$ python helloworld.py\n", 54 | "```\n", 55 | "\n", 56 | "You must see something like this:\n", 57 | "\n", 58 | "![Python cli](img/pythoncli.png)\n", 59 | "\n", 60 | "\n", 61 | "To run with Python 3 on Unix-like environments, you must type:\n", 62 | "\n", 63 | "```\n", 64 | "$ python helloworld.py\n", 65 | "```" 66 | ] 67 | } 68 | ], 69 | "metadata": { 70 | "kernelspec": { 71 | "display_name": "Python 3", 72 | "language": "python", 73 | "name": "python3" 74 | }, 75 | "language_info": { 76 | "codemirror_mode": { 77 | "name": "ipython", 78 | "version": 3 79 | }, 80 | "file_extension": ".py", 81 | "mimetype": "text/x-python", 82 | "name": "python", 83 | "nbconvert_exporter": "python", 84 | "pygments_lexer": "ipython3", 85 | "version": "3.4.3" 86 | } 87 | }, 88 | "nbformat": 4, 89 | "nbformat_minor": 0 90 | } 91 | -------------------------------------------------------------------------------- /notebooks/unit01/unit01_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# IPython" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "To open the IPython interpreter type in the shell:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "```\n", 29 | "$ ipython\n", 30 | "```" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "You must see something like this:\n", 38 | "\n", 39 | "![IPython shell](img/ipythonshell.png)" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "On Unix-like environments to open a IPython 3 interpreter you must type:\n", 47 | "```\n", 48 | "$ ipython3\n", 49 | "```\n", 50 | "\n", 51 | "You must see now something like this:\n", 52 | "\n", 53 | "![IPython 3 shell](img/ipython3shell.png)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "When you see the prompt (```In [1]:```), type:" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 1, 66 | "metadata": { 67 | "collapsed": false 68 | }, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "Hello, world!\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "print(\"Hello, world!\")" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "IPython general help:" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 3, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [ 97 | "?" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "IPython help about objects:" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 4, 110 | "metadata": { 111 | "collapsed": true 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "print?" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "IPython Magic functions:" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 14, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "Hello, world!\n", 137 | "Hello, world!\n", 138 | "Hello, world!\n", 139 | "Hello, world!\n", 140 | "Hello, world!\n", 141 | "Hello, world!\n", 142 | "Hello, world!\n", 143 | "Hello, world!\n", 144 | "Hello, world!\n", 145 | "Hello, world!\n", 146 | "Hello, world!\n", 147 | "Hello, world!\n", 148 | "4 loops, best of 3: 5.61 µs per loop\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "%timeit -n4 print(\"Hello, world!\")" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "There are a lot of magic functions..." 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 10, 166 | "metadata": { 167 | "collapsed": false 168 | }, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "Hello, world!\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "%run ../../code/unit01/helloworld.py" 180 | ] 181 | } 182 | ], 183 | "metadata": { 184 | "kernelspec": { 185 | "display_name": "Python 3", 186 | "language": "python", 187 | "name": "python3" 188 | }, 189 | "language_info": { 190 | "codemirror_mode": { 191 | "name": "ipython", 192 | "version": 3 193 | }, 194 | "file_extension": ".py", 195 | "mimetype": "text/x-python", 196 | "name": "python", 197 | "nbconvert_exporter": "python", 198 | "pygments_lexer": "ipython3", 199 | "version": "3.4.3" 200 | } 201 | }, 202 | "nbformat": 4, 203 | "nbformat_minor": 0 204 | } 205 | -------------------------------------------------------------------------------- /notebooks/unit01/unit01_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Create a Git repository and upload to GitHub" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Go to GitHub website and create an account:\n", 22 | "\n", 23 | "https://github.com\n", 24 | " \n", 25 | "After this we must create a new repository (see green button \"New\") with the name \"python-ebd-2016\". We can also enter a description.\n", 26 | "\n", 27 | "![Github](img/github2.png)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "Go now to folder where file helloworld.py was stored. \n", 35 | "\n", 36 | "This folder with our first Python script will be our first Git repository." 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "To init Git repository we must type in command line:\n", 44 | "```bash\n", 45 | "$ cd /ourrepository\n", 46 | "$ git init\n", 47 | "```" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Check status:\n", 55 | "```bash\n", 56 | "$ git status\n", 57 | "```" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "Configure Git (change myusername y name@myemail.com):\n", 65 | "```bash\n", 66 | "$ git config --global user.name \"myusername\"\n", 67 | "$ git config --global user.email name@myemail.com\n", 68 | "```" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Now we are going to add our file:\n", 76 | "```bash\n", 77 | "$ git add helloworld.py\n", 78 | "$ git status\n", 79 | "```" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "Commit this change:\n", 87 | "```bash\n", 88 | "$ git commit -m 'first commit'\n", 89 | "$ git status\n", 90 | "```" 91 | ] 92 | }, 93 | { 94 | "cell_type": "markdown", 95 | "metadata": {}, 96 | "source": [ 97 | "Check log:\n", 98 | "```bash\n", 99 | "$ git log\n", 100 | "```" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": { 111 | "collapsed": true 112 | }, 113 | "source": [ 114 | "Link local repository to GitHub:\n", 115 | "```bash\n", 116 | "$ git remote add origin https://github.com/myusernamehere/python-ebd-2016.git\n", 117 | "$ git pull origin master\n", 118 | "$ git push origin master\n", 119 | "$ git status\n", 120 | "```" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "Check your branch (you are in branch master):\n", 128 | "```bash\n", 129 | "$ git branch\n", 130 | "```\n", 131 | "\n", 132 | "Create a new branch (change 'myname' with your name or whatever you want):\n", 133 | "```bash\n", 134 | "$ git checkout -b dev-myname\n", 135 | "$ git branch\n", 136 | "```" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Create new document file with name README.md and write in this file:\n", 144 | "\n", 145 | "```# Python EBD lessons 2016\n", 146 | "Learning Python and more...\n", 147 | "```" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "Type in command line:\n", 155 | "```bash\n", 156 | "$ git add README.md\n", 157 | "$ git status\n", 158 | "$ git commit -m 'add readme file'\n", 159 | "$ git status\n", 160 | "$ git push origin dev-myname\n", 161 | "$ git status\n", 162 | "```" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "To merge this branch with master:\n", 170 | "```bash\n", 171 | "$ git checkout master\n", 172 | "$ git merge dev-myname\n", 173 | "```" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "Upload to GitHub master last changes (git pull is only to check there aren't updates at remotes):\n", 181 | "```bash\n", 182 | "$ git pull origin master\n", 183 | "$ git push origin master\n", 184 | "```" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "Go to GitHub website and check there all the work done." 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "## Fork a repository" 199 | ] 200 | }, 201 | { 202 | "cell_type": "markdown", 203 | "metadata": {}, 204 | "source": [ 205 | "A fork is a copy of a repository. You can fork this course repository with your personal GitHub account:" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "If you are logged in GitHub you can go to course repository and press FORK button (see screenshot below):\n", 213 | "\n", 214 | "![Github](img/github3.png)" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "Now you can go to command-line clone the repository forked:\n", 222 | " \n", 223 | "```\n", 224 | "$ git clone https://github.com/yourrepositoryname/geopython-lessons.git\n", 225 | "```" 226 | ] 227 | } 228 | ], 229 | "metadata": { 230 | "kernelspec": { 231 | "display_name": "Python 3", 232 | "language": "python", 233 | "name": "python3" 234 | }, 235 | "language_info": { 236 | "codemirror_mode": { 237 | "name": "ipython", 238 | "version": 3 239 | }, 240 | "file_extension": ".py", 241 | "mimetype": "text/x-python", 242 | "name": "python", 243 | "nbconvert_exporter": "python", 244 | "pygments_lexer": "ipython3", 245 | "version": "3.4.3" 246 | } 247 | }, 248 | "nbformat": 4, 249 | "nbformat_minor": 0 250 | } 251 | -------------------------------------------------------------------------------- /notebooks/unit02/unit02_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Strings" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "c1 = \"Introduction to geoprocessing with Python\"" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "metadata": { 32 | "collapsed": true 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "c2 = 'Introduction to geoprocessing with Python'" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 3, 42 | "metadata": { 43 | "collapsed": false 44 | }, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Introduction to geoprocessing with Python\n" 51 | ] 52 | } 53 | ], 54 | "source": [ 55 | "print(c1)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": 4, 61 | "metadata": { 62 | "collapsed": false 63 | }, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Introduction to geoprocessing with Python\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "print(c2)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": 5, 80 | "metadata": { 81 | "collapsed": false 82 | }, 83 | "outputs": [ 84 | { 85 | "data": { 86 | "text/plain": [ 87 | "str" 88 | ] 89 | }, 90 | "execution_count": 5, 91 | "metadata": {}, 92 | "output_type": "execute_result" 93 | } 94 | ], 95 | "source": [ 96 | "type(c1)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 6, 102 | "metadata": { 103 | "collapsed": false 104 | }, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/plain": [ 109 | "str" 110 | ] 111 | }, 112 | "execution_count": 6, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "type(c2)" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 7, 124 | "metadata": { 125 | "collapsed": true 126 | }, 127 | "outputs": [], 128 | "source": [ 129 | "c3 = \"\"\"Introduction \n", 130 | "to geoprocessing \n", 131 | "with Python\"\"\"" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 8, 137 | "metadata": { 138 | "collapsed": false 139 | }, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "Introduction \n", 146 | "to geoprocessing \n", 147 | "with Python\n" 148 | ] 149 | } 150 | ], 151 | "source": [ 152 | "print(c3)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 9, 158 | "metadata": { 159 | "collapsed": false 160 | }, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "41" 166 | ] 167 | }, 168 | "execution_count": 9, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "len(c1)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 10, 180 | "metadata": { 181 | "collapsed": false 182 | }, 183 | "outputs": [ 184 | { 185 | "data": { 186 | "text/plain": [ 187 | "'INTRODUCTION TO GEOPROCESSING WITH PYTHON'" 188 | ] 189 | }, 190 | "execution_count": 10, 191 | "metadata": {}, 192 | "output_type": "execute_result" 193 | } 194 | ], 195 | "source": [ 196 | "c1.upper()" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 11, 202 | "metadata": { 203 | "collapsed": false 204 | }, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "13" 210 | ] 211 | }, 212 | "execution_count": 11, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "c1.find('to')" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 12, 224 | "metadata": { 225 | "collapsed": false 226 | }, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "'Introduct5on to geoprocess5ng w5th Python'" 232 | ] 233 | }, 234 | "execution_count": 12, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "c1.replace('i','5')" 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "Slicing strings:" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 13, 253 | "metadata": { 254 | "collapsed": false 255 | }, 256 | "outputs": [ 257 | { 258 | "data": { 259 | "text/plain": [ 260 | "'Introduction'" 261 | ] 262 | }, 263 | "execution_count": 13, 264 | "metadata": {}, 265 | "output_type": "execute_result" 266 | } 267 | ], 268 | "source": [ 269 | "c1[:12]" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 14, 275 | "metadata": { 276 | "collapsed": false 277 | }, 278 | "outputs": [ 279 | { 280 | "data": { 281 | "text/plain": [ 282 | "' to geoprocessing with Python'" 283 | ] 284 | }, 285 | "execution_count": 14, 286 | "metadata": {}, 287 | "output_type": "execute_result" 288 | } 289 | ], 290 | "source": [ 291 | "c1[12:]" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": 15, 297 | "metadata": { 298 | "collapsed": false 299 | }, 300 | "outputs": [ 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "'Python'" 305 | ] 306 | }, 307 | "execution_count": 15, 308 | "metadata": {}, 309 | "output_type": "execute_result" 310 | } 311 | ], 312 | "source": [ 313 | "c1[-6:]" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 16, 319 | "metadata": { 320 | "collapsed": false 321 | }, 322 | "outputs": [ 323 | { 324 | "data": { 325 | "text/plain": [ 326 | "'to geoprocessing'" 327 | ] 328 | }, 329 | "execution_count": 16, 330 | "metadata": {}, 331 | "output_type": "execute_result" 332 | } 333 | ], 334 | "source": [ 335 | "c1[13:29]" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 17, 341 | "metadata": { 342 | "collapsed": false 343 | }, 344 | "outputs": [], 345 | "source": [ 346 | "c4 = c1[16:29] + \" data \" + c1[-11:] " 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 18, 352 | "metadata": { 353 | "collapsed": false 354 | }, 355 | "outputs": [ 356 | { 357 | "name": "stdout", 358 | "output_type": "stream", 359 | "text": [ 360 | "geoprocessing data with Python\n" 361 | ] 362 | } 363 | ], 364 | "source": [ 365 | "print(c4)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 19, 371 | "metadata": { 372 | "collapsed": false 373 | }, 374 | "outputs": [ 375 | { 376 | "data": { 377 | "text/plain": [ 378 | "'g'" 379 | ] 380 | }, 381 | "execution_count": 19, 382 | "metadata": {}, 383 | "output_type": "execute_result" 384 | } 385 | ], 386 | "source": [ 387 | "c4[0]" 388 | ] 389 | }, 390 | { 391 | "cell_type": "markdown", 392 | "metadata": {}, 393 | "source": [ 394 | "Strings are inmutable objects!! You can not do things like this..." 395 | ] 396 | }, 397 | { 398 | "cell_type": "code", 399 | "execution_count": 20, 400 | "metadata": { 401 | "collapsed": false 402 | }, 403 | "outputs": [ 404 | { 405 | "ename": "TypeError", 406 | "evalue": "'str' object does not support item assignment", 407 | "output_type": "error", 408 | "traceback": [ 409 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 410 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 411 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mc4\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"G\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 412 | "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" 413 | ] 414 | } 415 | ], 416 | "source": [ 417 | "c4[0] = \"G\"" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": {}, 423 | "source": [ 424 | "Data formating (new an old way...):" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": null, 430 | "metadata": { 431 | "collapsed": false 432 | }, 433 | "outputs": [], 434 | "source": [ 435 | "\"{0} data {1}\".format(c1[16:29], c1[-11:])" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": null, 441 | "metadata": { 442 | "collapsed": false 443 | }, 444 | "outputs": [], 445 | "source": [ 446 | "\"%s data %s\"%(c1[16:29], c1[-11:])" 447 | ] 448 | } 449 | ], 450 | "metadata": { 451 | "kernelspec": { 452 | "display_name": "Python 3", 453 | "language": "python", 454 | "name": "python3" 455 | }, 456 | "language_info": { 457 | "codemirror_mode": { 458 | "name": "ipython", 459 | "version": 3 460 | }, 461 | "file_extension": ".py", 462 | "mimetype": "text/x-python", 463 | "name": "python", 464 | "nbconvert_exporter": "python", 465 | "pygments_lexer": "ipython3", 466 | "version": "3.4.3" 467 | } 468 | }, 469 | "nbformat": 4, 470 | "nbformat_minor": 0 471 | } 472 | -------------------------------------------------------------------------------- /notebooks/unit02/unit02_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Tuples" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 2, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "coords = (37.5, -6.03)" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 11, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "tuple" 39 | ] 40 | }, 41 | "execution_count": 11, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "type(coords)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "Unpacking tuples:" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "metadata": { 61 | "collapsed": true 62 | }, 63 | "outputs": [], 64 | "source": [ 65 | "lat, lon = coords" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "37.5\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "print(lat)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 5, 90 | "metadata": { 91 | "collapsed": false 92 | }, 93 | "outputs": [ 94 | { 95 | "name": "stdout", 96 | "output_type": "stream", 97 | "text": [ 98 | "-6.03\n" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "print(lon)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "markdown", 108 | "metadata": {}, 109 | "source": [ 110 | "Mixing data types:" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 6, 116 | "metadata": { 117 | "collapsed": true 118 | }, 119 | "outputs": [], 120 | "source": [ 121 | "mytuple = (\"hello\", 1, 4.5, \"world\")" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "Accesing:" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 7, 134 | "metadata": { 135 | "collapsed": false 136 | }, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "'hello'" 142 | ] 143 | }, 144 | "execution_count": 7, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "mytuple[0]" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 8, 156 | "metadata": { 157 | "collapsed": false 158 | }, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "(4.5, 'world')" 164 | ] 165 | }, 166 | "execution_count": 8, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "mytuple[-2:]" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "Tuples are immutables!!" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": 10, 185 | "metadata": { 186 | "collapsed": false 187 | }, 188 | "outputs": [ 189 | { 190 | "ename": "TypeError", 191 | "evalue": "'tuple' object does not support item assignment", 192 | "output_type": "error", 193 | "traceback": [ 194 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 195 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 196 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmytuple\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m4.3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 197 | "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" 198 | ] 199 | } 200 | ], 201 | "source": [ 202 | "mytuple[0] = 4.3" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "Converting tuple in list:" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 14, 215 | "metadata": { 216 | "collapsed": false 217 | }, 218 | "outputs": [], 219 | "source": [ 220 | "mylist = list(mytuple)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 15, 226 | "metadata": { 227 | "collapsed": false 228 | }, 229 | "outputs": [ 230 | { 231 | "data": { 232 | "text/plain": [ 233 | "list" 234 | ] 235 | }, 236 | "execution_count": 15, 237 | "metadata": {}, 238 | "output_type": "execute_result" 239 | } 240 | ], 241 | "source": [ 242 | "type(mylist)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 16, 248 | "metadata": { 249 | "collapsed": false 250 | }, 251 | "outputs": [ 252 | { 253 | "name": "stdout", 254 | "output_type": "stream", 255 | "text": [ 256 | "['hello', 1, 4.5, 'world']\n" 257 | ] 258 | } 259 | ], 260 | "source": [ 261 | "print(mylist)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "markdown", 266 | "metadata": {}, 267 | "source": [ 268 | "A list of tuples (...of tuples):" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 24, 274 | "metadata": { 275 | "collapsed": true 276 | }, 277 | "outputs": [], 278 | "source": [ 279 | "locations = [( \"waypoint_01\", (38.4, -6.2)), ( \"waypoint_02\", (38.3, -6.7)), ( \"waypoint_03\", (37.9, -6.1))]" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 25, 285 | "metadata": { 286 | "collapsed": false 287 | }, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "[('waypoint_01', (38.4, -6.2)), ('waypoint_02', (38.3, -6.7)), ('waypoint_03', (37.9, -6.1))]\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "print(locations)" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": 26, 304 | "metadata": { 305 | "collapsed": false 306 | }, 307 | "outputs": [ 308 | { 309 | "data": { 310 | "text/plain": [ 311 | "3" 312 | ] 313 | }, 314 | "execution_count": 26, 315 | "metadata": {}, 316 | "output_type": "execute_result" 317 | } 318 | ], 319 | "source": [ 320 | "len(locations)" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 27, 326 | "metadata": { 327 | "collapsed": false 328 | }, 329 | "outputs": [ 330 | { 331 | "data": { 332 | "text/plain": [ 333 | "list" 334 | ] 335 | }, 336 | "execution_count": 27, 337 | "metadata": {}, 338 | "output_type": "execute_result" 339 | } 340 | ], 341 | "source": [ 342 | "type(locations)" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 28, 348 | "metadata": { 349 | "collapsed": false 350 | }, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "tuple" 356 | ] 357 | }, 358 | "execution_count": 28, 359 | "metadata": {}, 360 | "output_type": "execute_result" 361 | } 362 | ], 363 | "source": [ 364 | "type(locations[0])" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 29, 370 | "metadata": { 371 | "collapsed": true 372 | }, 373 | "outputs": [], 374 | "source": [ 375 | "locations.append(( \"waypoint_04\", (38.5, -6.6)))" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 30, 381 | "metadata": { 382 | "collapsed": false 383 | }, 384 | "outputs": [ 385 | { 386 | "name": "stdout", 387 | "output_type": "stream", 388 | "text": [ 389 | "[('waypoint_01', (38.4, -6.2)), ('waypoint_02', (38.3, -6.7)), ('waypoint_03', (37.9, -6.1)), ('waypoint_04', (38.5, -6.6))]\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "print(locations)" 395 | ] 396 | } 397 | ], 398 | "metadata": { 399 | "kernelspec": { 400 | "display_name": "Python 3", 401 | "language": "python", 402 | "name": "python3" 403 | }, 404 | "language_info": { 405 | "codemirror_mode": { 406 | "name": "ipython", 407 | "version": 3 408 | }, 409 | "file_extension": ".py", 410 | "mimetype": "text/x-python", 411 | "name": "python", 412 | "nbconvert_exporter": "python", 413 | "pygments_lexer": "ipython3", 414 | "version": "3.4.3" 415 | } 416 | }, 417 | "nbformat": 4, 418 | "nbformat_minor": 0 419 | } 420 | -------------------------------------------------------------------------------- /notebooks/unit02/unit02_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Control of flow: conditionals" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## IF sentence" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "Simple IF sentence:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 23, 34 | "metadata": { 35 | "collapsed": true 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "a = 5" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 24, 45 | "metadata": { 46 | "collapsed": false 47 | }, 48 | "outputs": [ 49 | { 50 | "name": "stdout", 51 | "output_type": "stream", 52 | "text": [ 53 | "Value of a: 5\n" 54 | ] 55 | } 56 | ], 57 | "source": [ 58 | "if a == 5:\n", 59 | " print(\"Value of a: {0}\".format(a))" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 25, 65 | "metadata": { 66 | "collapsed": true 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "a = 2" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 26, 76 | "metadata": { 77 | "collapsed": true 78 | }, 79 | "outputs": [], 80 | "source": [ 81 | "if a == 5:\n", 82 | " print(\"Value of a: {0}\".format(a))" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "IF + ELSE sentence:" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 27, 95 | "metadata": { 96 | "collapsed": false 97 | }, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "a is not 5... Value of a: 2\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "if a == 5:\n", 109 | " print(\"Value of a: {0}\".format(a))\n", 110 | "else:\n", 111 | " print(\"a is not 5... Value of a: {0}\".format(a))" 112 | ] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": 28, 117 | "metadata": { 118 | "collapsed": false 119 | }, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "Value of a: 2\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "if a <= 5:\n", 131 | " print(\"Value of a: {0}\".format(a))\n", 132 | "else:\n", 133 | " print(\"a is not <= 5... Value of a: {0}\".format(a))" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 29, 139 | "metadata": { 140 | "collapsed": true 141 | }, 142 | "outputs": [], 143 | "source": [ 144 | "b = 15" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 30, 150 | "metadata": { 151 | "collapsed": false 152 | }, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "Value of a: 2\n", 159 | "Value of b: 15\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "if a <= 5 and b > 10:\n", 165 | " print(\"Value of a: {0}\\nValue of b: {1}\".format(a,b))\n", 166 | "else:\n", 167 | " print(\"Incorrect values.\\na: {0} b: {1}\".format(a,b))" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 31, 173 | "metadata": { 174 | "collapsed": true 175 | }, 176 | "outputs": [], 177 | "source": [ 178 | "a = 6" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 32, 184 | "metadata": { 185 | "collapsed": false 186 | }, 187 | "outputs": [ 188 | { 189 | "name": "stdout", 190 | "output_type": "stream", 191 | "text": [ 192 | "Incorrect values.\n", 193 | "a: 6 b: 15\n" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "if a <= 5 and b > 10:\n", 199 | " print(\"Value of a: {0}\\nValue of b: {1}\".format(a,b))\n", 200 | "else: \n", 201 | " print(\"Incorrect values.\\na: {0} b: {1}\".format(a,b))" 202 | ] 203 | }, 204 | { 205 | "cell_type": "markdown", 206 | "metadata": {}, 207 | "source": [ 208 | "IF + ELIF + ELSE sentence:" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 33, 214 | "metadata": { 215 | "collapsed": true 216 | }, 217 | "outputs": [], 218 | "source": [ 219 | "b = 1" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 34, 225 | "metadata": { 226 | "collapsed": false 227 | }, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "Value of b: 1\n", 234 | "Incorrect value of a: 6\n" 235 | ] 236 | } 237 | ], 238 | "source": [ 239 | "if a <= 5 and b > 10:\n", 240 | " print(\"Value of a: {0}\\nValue of b: {1}\".format(a,b))\n", 241 | "elif b < 2:\n", 242 | " print(\"Value of b: {0}\\nIncorrect value of a: {1}\".format(b, a))\n", 243 | "else: \n", 244 | " print(\"Incorrect values.\\na: {0} b: {1}\".format(a,b))" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "Nested IF sentences:" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 38, 257 | "metadata": { 258 | "collapsed": false 259 | }, 260 | "outputs": [], 261 | "source": [ 262 | "a = None\n", 263 | "b = False" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 39, 269 | "metadata": { 270 | "collapsed": false 271 | }, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "We need a value!\n", 278 | "a: None b: False\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "if a or b:\n", 284 | " if a <= 5 and b > 10:\n", 285 | " print(\"Value of a: {0}\\nValue of b: {1}\".format(a,b))\n", 286 | " elif b < 2:\n", 287 | " print(\"Value of b: {0}\\nIncorrect value of a: {1}\".format(b, a))\n", 288 | " else: \n", 289 | " print(\"Incorrect values.\\na: {0} b: {1}\".format(a,b))\n", 290 | "else:\n", 291 | " print(\"We need a value!\\na: {0} b: {1}\".format(a,b))" 292 | ] 293 | } 294 | ], 295 | "metadata": { 296 | "kernelspec": { 297 | "display_name": "Python 3", 298 | "language": "python", 299 | "name": "python3" 300 | }, 301 | "language_info": { 302 | "codemirror_mode": { 303 | "name": "ipython", 304 | "version": 3 305 | }, 306 | "file_extension": ".py", 307 | "mimetype": "text/x-python", 308 | "name": "python", 309 | "nbconvert_exporter": "python", 310 | "pygments_lexer": "ipython3", 311 | "version": "3.4.3" 312 | } 313 | }, 314 | "nbformat": 4, 315 | "nbformat_minor": 0 316 | } 317 | -------------------------------------------------------------------------------- /notebooks/unit02/unit02_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Control of flow: loops" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## WHILE loop" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "x = 0" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 2, 38 | "metadata": { 39 | "collapsed": false 40 | }, 41 | "outputs": [ 42 | { 43 | "name": "stdout", 44 | "output_type": "stream", 45 | "text": [ 46 | "Value of x: 0\n", 47 | "Value of x: 1\n", 48 | "Value of x: 2\n", 49 | "Value of x: 3\n", 50 | "Value of x: 4\n", 51 | "Value of x: 5\n", 52 | "Value of x: 6\n", 53 | "Value of x: 7\n", 54 | "Value of x: 8\n", 55 | "Value of x: 9\n", 56 | "Value of x: 10\n", 57 | "Value of x: 11\n", 58 | "Value of x: 12\n", 59 | "Value of x: 13\n", 60 | "Value of x: 14\n", 61 | "Value of x: 15\n", 62 | "Value of x: 16\n", 63 | "Value of x: 17\n", 64 | "Value of x: 18\n", 65 | "Value of x: 19\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "while x < 20:\n", 71 | " print(\"Value of x: {0}\".format(x))\n", 72 | " x += 1" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "Warning: WHILE loop can be infinite if condition always is True!!!!\n", 80 | "\n", 81 | "Example:\n", 82 | "\n", 83 | "```python\n", 84 | "while x:\n", 85 | " print(\"Value of x: {0}\".format(x))```\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## FOR loop:" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 3, 98 | "metadata": { 99 | "collapsed": true 100 | }, 101 | "outputs": [], 102 | "source": [ 103 | "locations = [('waypoint_01', (38.4, -6.2)), ('waypoint_02', (38.3, -6.7)), ('waypoint_03', (37.9, -6.1)), ('waypoint_04', (38.5, -6.6))]" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 4, 109 | "metadata": { 110 | "collapsed": false 111 | }, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "Location: ('waypoint_01', (38.4, -6.2))\n", 118 | "Location: ('waypoint_02', (38.3, -6.7))\n", 119 | "Location: ('waypoint_03', (37.9, -6.1))\n", 120 | "Location: ('waypoint_04', (38.5, -6.6))\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "for i in locations:\n", 126 | " print(\"Location: {0}\".format(i))" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 5, 132 | "metadata": { 133 | "collapsed": true 134 | }, 135 | "outputs": [], 136 | "source": [ 137 | "n = 0" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 6, 143 | "metadata": { 144 | "collapsed": false 145 | }, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "Location 0: ('waypoint_01', (38.4, -6.2))\n", 152 | "Location 1: ('waypoint_02', (38.3, -6.7))\n", 153 | "Location 2: ('waypoint_03', (37.9, -6.1))\n", 154 | "Location 3: ('waypoint_04', (38.5, -6.6))\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "for i in locations:\n", 160 | " print(\"Location {0}: {1}\".format(n, i))\n", 161 | " n += 1" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "A Pythonic way to handle enumerations:" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 7, 174 | "metadata": { 175 | "collapsed": false 176 | }, 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "Location 0: ('waypoint_01', (38.4, -6.2))\n", 183 | "Location 1: ('waypoint_02', (38.3, -6.7))\n", 184 | "Location 2: ('waypoint_03', (37.9, -6.1))\n", 185 | "Location 3: ('waypoint_04', (38.5, -6.6))\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "for idx, loc in enumerate(locations):\n", 191 | " print(\"Location {0}: {1}\".format(idx, loc))" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "metadata": {}, 197 | "source": [ 198 | "Loops with ranges:" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 8, 204 | "metadata": { 205 | "collapsed": false 206 | }, 207 | "outputs": [ 208 | { 209 | "name": "stdout", 210 | "output_type": "stream", 211 | "text": [ 212 | "0\n", 213 | "1\n", 214 | "2\n", 215 | "3\n", 216 | "4\n", 217 | "5\n", 218 | "6\n", 219 | "7\n", 220 | "8\n", 221 | "9\n" 222 | ] 223 | } 224 | ], 225 | "source": [ 226 | "for i in range(10):\n", 227 | " print(i)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 9, 233 | "metadata": { 234 | "collapsed": true 235 | }, 236 | "outputs": [], 237 | "source": [ 238 | "rg = range(10)" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 10, 244 | "metadata": { 245 | "collapsed": false 246 | }, 247 | "outputs": [ 248 | { 249 | "data": { 250 | "text/plain": [ 251 | "range" 252 | ] 253 | }, 254 | "execution_count": 10, 255 | "metadata": {}, 256 | "output_type": "execute_result" 257 | } 258 | ], 259 | "source": [ 260 | "type(rg)" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "Remark: In Python 2, with ```range()``` function we build a list object." 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "Looping a dictionary:" 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "execution_count": 11, 280 | "metadata": { 281 | "collapsed": false 282 | }, 283 | "outputs": [], 284 | "source": [ 285 | "locations_dict = {'waypoint_01': (38.4, -6.2), \n", 286 | " 'waypoint_02': (38.3, -6.7), \n", 287 | " 'waypoint_04': (38.5, -6.6), \n", 288 | " 'waypoint_03': (37.9, -6.1)}" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": 12, 294 | "metadata": { 295 | "collapsed": false 296 | }, 297 | "outputs": [ 298 | { 299 | "name": "stdout", 300 | "output_type": "stream", 301 | "text": [ 302 | "(38.3, -6.7)\n", 303 | "(38.5, -6.6)\n", 304 | "(37.9, -6.1)\n", 305 | "(38.4, -6.2)\n" 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "for k in locations_dict:\n", 311 | " print(locations_dict.get(k))" 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "execution_count": 13, 317 | "metadata": { 318 | "collapsed": false 319 | }, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "waypoint_02: (38.3, -6.7)\n", 326 | "waypoint_04: (38.5, -6.6)\n", 327 | "waypoint_03: (37.9, -6.1)\n", 328 | "waypoint_01: (38.4, -6.2)\n" 329 | ] 330 | } 331 | ], 332 | "source": [ 333 | "for k in locations_dict:\n", 334 | " val = locations_dict.get(k)\n", 335 | " print(\"{0}: {1}\".format(k, val))" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": {}, 341 | "source": [ 342 | "Looping several lists:" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 14, 348 | "metadata": { 349 | "collapsed": false 350 | }, 351 | "outputs": [], 352 | "source": [ 353 | "locations2 = [('pk01', 34.5), ('pk02', 35.9), ('pk03', 42.1), ('pk04', 45.3)]" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 15, 359 | "metadata": { 360 | "collapsed": false 361 | }, 362 | "outputs": [ 363 | { 364 | "name": "stdout", 365 | "output_type": "stream", 366 | "text": [ 367 | "Location1: ('waypoint_01', (38.4, -6.2))\n", 368 | "Location2: ('pk01', 34.5)\n", 369 | "\n", 370 | "Location1: ('waypoint_02', (38.3, -6.7))\n", 371 | "Location2: ('pk02', 35.9)\n", 372 | "\n", 373 | "Location1: ('waypoint_03', (37.9, -6.1))\n", 374 | "Location2: ('pk03', 42.1)\n", 375 | "\n", 376 | "Location1: ('waypoint_04', (38.5, -6.6))\n", 377 | "Location2: ('pk04', 45.3)\n", 378 | "\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "for loc1,loc2 in zip(locations, locations2):\n", 384 | " print(\"Location1: {0}\".format(loc1))\n", 385 | " print(\"Location2: {0}\\n\".format(loc2))" 386 | ] 387 | } 388 | ], 389 | "metadata": { 390 | "kernelspec": { 391 | "display_name": "Python 3", 392 | "language": "python", 393 | "name": "python3" 394 | }, 395 | "language_info": { 396 | "codemirror_mode": { 397 | "name": "ipython", 398 | "version": 3 399 | }, 400 | "file_extension": ".py", 401 | "mimetype": "text/x-python", 402 | "name": "python", 403 | "nbconvert_exporter": "python", 404 | "pygments_lexer": "ipython3", 405 | "version": "3.4.3" 406 | } 407 | }, 408 | "nbformat": 4, 409 | "nbformat_minor": 0 410 | } 411 | -------------------------------------------------------------------------------- /notebooks/unit03/testfile.txt: -------------------------------------------------------------------------------- 1 | array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0) 2 | 3 | Create an array. 4 | 5 | Parameters 6 | ---------- 7 | object : array_like 8 | An array, any object exposing the array interface, an 9 | object whose __array__ method returns an array, or any 10 | (nested) sequence. 11 | dtype : data-type, optional 12 | The desired data-type for the array. If not given, then 13 | the type will be determined as the minimum type required 14 | to hold the objects in the sequence. This argument can only 15 | be used to 'upcast' the array. For downcasting, use the 16 | .astype(t) method. 17 | copy : bool, optional 18 | If true (default), then the object is copied. Otherwise, a copy 19 | will only be made if __array__ returns a copy, if obj is a 20 | nested sequence, or if a copy is needed to satisfy any of the other 21 | requirements (`dtype`, `order`, etc.). 22 | order : {'C', 'F', 'A'}, optional 23 | Specify the order of the array. If order is 'C', then the array 24 | will be in C-contiguous order (last-index varies the fastest). 25 | If order is 'F', then the returned array will be in 26 | Fortran-contiguous order (first-index varies the fastest). 27 | If order is 'A' (default), then the returned array may be 28 | in any order (either C-, Fortran-contiguous, or even discontiguous), 29 | unless a copy is required, in which case it will be C-contiguous. 30 | subok : bool, optional 31 | If True, then sub-classes will be passed-through, otherwise 32 | the returned array will be forced to be a base-class array (default). 33 | ndmin : int, optional 34 | Specifies the minimum number of dimensions that the resulting 35 | array should have. Ones will be pre-pended to the shape as 36 | needed to meet this requirement. 37 | -------------------------------------------------------------------------------- /notebooks/unit03/unit03_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Modules" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "source": [ 23 | "First we create a module to import after from another module.\n", 24 | "\n", 25 | "Open a text editor and put all this code in a file with name: \n", 26 | "\n", 27 | " circle.py" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "```python\n", 35 | "import math\n", 36 | "\n", 37 | "PI = math.pi\n", 38 | "\n", 39 | "def getPerim(rad):\n", 40 | " return 2 * PI * rad\n", 41 | "\n", 42 | "def getArea(rad):\n", 43 | " return PI * (rad**2)\n", 44 | "```" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "We create a module to import previous module.\n", 52 | "\n", 53 | "Open a text editor and put this code ina file with this name:\n", 54 | " \n", 55 | " testcircle.py" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "```python\n", 63 | "import circle\n", 64 | " \n", 65 | "radious = 12\n", 66 | " \n", 67 | "perim = circle.getPerim(radious)\n", 68 | " \n", 69 | "area = circle.getArea(radious)\n", 70 | " \n", 71 | "print(\"With circle radious {0}, perimeter is {1} and area is {2}\".format(radious, perim, area))\n", 72 | "```" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "Execute file from command line:\n", 80 | "\n", 81 | "```\n", 82 | "$ python3 testcircle.py\n", 83 | "```" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "We can use circle module from interpreter:" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "metadata": { 97 | "collapsed": true 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "import circle" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 7, 107 | "metadata": { 108 | "collapsed": false 109 | }, 110 | "outputs": [ 111 | { 112 | "data": { 113 | "text/plain": [ 114 | "125.66370614359172" 115 | ] 116 | }, 117 | "execution_count": 7, 118 | "metadata": {}, 119 | "output_type": "execute_result" 120 | } 121 | ], 122 | "source": [ 123 | "circle.getPerim(20)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 8, 129 | "metadata": { 130 | "collapsed": false 131 | }, 132 | "outputs": [ 133 | { 134 | "data": { 135 | "text/plain": [ 136 | "1256.6370614359173" 137 | ] 138 | }, 139 | "execution_count": 8, 140 | "metadata": {}, 141 | "output_type": "execute_result" 142 | } 143 | ], 144 | "source": [ 145 | "circle.getArea(20)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 9, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "3.141592653589793" 159 | ] 160 | }, 161 | "execution_count": 9, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "circle.PI" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "Now we add documentation to each function in circle module:\n", 175 | "\n", 176 | "```python\n", 177 | "import math\n", 178 | "\n", 179 | "PI = math.pi\n", 180 | "\n", 181 | "def getPerim(rad):\n", 182 | " \"\"\"\n", 183 | " Compute circle perimeter\n", 184 | " \"\"\"\n", 185 | " return 2 * PI * rad\n", 186 | "\n", 187 | "def getArea(rad):\n", 188 | " \"\"\"\n", 189 | " Compute circle perimeter\n", 190 | " \"\"\"\n", 191 | " return PI * (rad**2)\n", 192 | "```" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "Import module again and see docs (in IPython interpreter):" 200 | ] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": 10, 205 | "metadata": { 206 | "collapsed": true 207 | }, 208 | "outputs": [], 209 | "source": [ 210 | "import circle" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 25, 216 | "metadata": { 217 | "collapsed": false 218 | }, 219 | "outputs": [], 220 | "source": [ 221 | "circle.getArea?" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 26, 227 | "metadata": { 228 | "collapsed": true 229 | }, 230 | "outputs": [], 231 | "source": [ 232 | "circle.getPerim?" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "Another way to do a module import:" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 28, 245 | "metadata": { 246 | "collapsed": true 247 | }, 248 | "outputs": [], 249 | "source": [ 250 | "from circle import getArea, getPerim" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 29, 256 | "metadata": { 257 | "collapsed": false, 258 | "scrolled": true 259 | }, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "314.1592653589793" 265 | ] 266 | }, 267 | "execution_count": 29, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "getArea(10)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 30, 279 | "metadata": { 280 | "collapsed": false 281 | }, 282 | "outputs": [ 283 | { 284 | "data": { 285 | "text/plain": [ 286 | "62.83185307179586" 287 | ] 288 | }, 289 | "execution_count": 30, 290 | "metadata": {}, 291 | "output_type": "execute_result" 292 | } 293 | ], 294 | "source": [ 295 | "getPerim(10)" 296 | ] 297 | }, 298 | { 299 | "cell_type": "markdown", 300 | "metadata": { 301 | "collapsed": true 302 | }, 303 | "source": [ 304 | "## Execute module as script" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "You can execute modules as scripts. \n", 312 | "\n", 313 | "You must add this code to circle module to execute as script properly:" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": {}, 319 | "source": [ 320 | "```python\n", 321 | "import sys\n", 322 | "import math\n", 323 | "\n", 324 | "PI = math.pi\n", 325 | "\n", 326 | "def getPerim(rad):\n", 327 | " \"\"\"\n", 328 | " Compute circle perimeter\n", 329 | " \"\"\"\n", 330 | " return 2 * PI * rad\n", 331 | "\n", 332 | "def getArea(rad):\n", 333 | " \"\"\"\n", 334 | " Compute circle perimeter\n", 335 | " \"\"\"\n", 336 | " return PI * (rad**2)\n", 337 | "\n", 338 | "def main():\n", 339 | " print(\"Started circle module...\")\n", 340 | "\n", 341 | " radious = float(sys.argv[1])\n", 342 | "\n", 343 | " perim = getPerim(radious)\n", 344 | "\n", 345 | " area = getArea(radious)\n", 346 | "\n", 347 | " print(\"With circle radious {0}, perimeter is {1} and area is {2}\".format(radious, perim, area))\n", 348 | "\n", 349 | "\n", 350 | "if __name__ == \"__main__\":\n", 351 | " main()\n", 352 | "```" 353 | ] 354 | }, 355 | { 356 | "cell_type": "markdown", 357 | "metadata": {}, 358 | "source": [ 359 | "Remarks: \n", 360 | "- ```main()``` is executed only if run as a script.\n", 361 | "- With sys.argv we pass an argument (radious) from command line.\n", 362 | "- On Unix-like OS remember to put shebang as first line of the script." 363 | ] 364 | } 365 | ], 366 | "metadata": { 367 | "kernelspec": { 368 | "display_name": "Python 3", 369 | "language": "python", 370 | "name": "python3" 371 | }, 372 | "language_info": { 373 | "codemirror_mode": { 374 | "name": "ipython", 375 | "version": 3 376 | }, 377 | "file_extension": ".py", 378 | "mimetype": "text/x-python", 379 | "name": "python", 380 | "nbconvert_exporter": "python", 381 | "pygments_lexer": "ipython3", 382 | "version": "3.4.3" 383 | } 384 | }, 385 | "nbformat": 4, 386 | "nbformat_minor": 0 387 | } 388 | -------------------------------------------------------------------------------- /notebooks/unit03/unit03_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Handling exceptions" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "source": [ 23 | "Handling exceptions with TRY-EXCEPT:" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "def circlePerim(rad):\n", 35 | " return 2 * math.pi * rad" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "An error executing this script because we do not import Math module:" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "metadata": { 49 | "collapsed": false 50 | }, 51 | "outputs": [ 52 | { 53 | "ename": "NameError", 54 | "evalue": "name 'math' is not defined", 55 | "output_type": "error", 56 | "traceback": [ 57 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 58 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 59 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcirclePerim\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hola\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 60 | "\u001b[0;32m\u001b[0m in \u001b[0;36mcirclePerim\u001b[0;34m(rad)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcirclePerim\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrad\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpi\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mrad\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 61 | "\u001b[0;31mNameError\u001b[0m: name 'math' is not defined" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "circlePerim(\"hola\")" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 3, 72 | "metadata": { 73 | "collapsed": true 74 | }, 75 | "outputs": [], 76 | "source": [ 77 | "def circlePerim(rad):\n", 78 | " try:\n", 79 | " return 2 * math.pi * rad\n", 80 | " except Exception as error:\n", 81 | " print(\"Error: {0}\".format(error))" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 4, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "Error: name 'math' is not defined\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "circlePerim(\"hola\")" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 5, 106 | "metadata": { 107 | "collapsed": true 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "import math\n", 112 | "\n", 113 | "def circlePerim(rad):\n", 114 | " try:\n", 115 | " return 2 * math.pi * rad\n", 116 | " except Exception as error:\n", 117 | " print(\"Error: {0}\".format(error))" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 6, 123 | "metadata": { 124 | "collapsed": false 125 | }, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "Error: can't multiply sequence by non-int of type 'float'\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "circlePerim(\"hola\")" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "Handling specific errors:" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 7, 149 | "metadata": { 150 | "collapsed": true 151 | }, 152 | "outputs": [], 153 | "source": [ 154 | "def circlePerim(rad):\n", 155 | " try:\n", 156 | " return 2 * math.pi * rad\n", 157 | " except TypeError as error:\n", 158 | " print(\"Type Error: {0}\".format(error))\n", 159 | " except Exception as error:\n", 160 | " print(\"Error: {0}\".format(error))" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 8, 166 | "metadata": { 167 | "collapsed": false 168 | }, 169 | "outputs": [ 170 | { 171 | "name": "stdout", 172 | "output_type": "stream", 173 | "text": [ 174 | "Type Error: can't multiply sequence by non-int of type 'float'\n" 175 | ] 176 | } 177 | ], 178 | "source": [ 179 | "circlePerim(\"hola\")" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "FINALLY clause (always is executed):" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": 12, 192 | "metadata": { 193 | "collapsed": true 194 | }, 195 | "outputs": [], 196 | "source": [ 197 | "def circlePerim(rad):\n", 198 | " try:\n", 199 | " return 2 * math.pi * rad\n", 200 | " except TypeError as error:\n", 201 | " print(\"Type Error: {0}\".format(error))\n", 202 | " except Exception as error:\n", 203 | " print(\"Error: {0}\".format(error))\n", 204 | " finally:\n", 205 | " print(\"Finalized execution. Value of radious: {0}\".format(rad))" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 13, 211 | "metadata": { 212 | "collapsed": false 213 | }, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "Type Error: can't multiply sequence by non-int of type 'float'\n", 220 | "Finalized execution. Value of radious: hola\n" 221 | ] 222 | } 223 | ], 224 | "source": [ 225 | "circlePerim(\"hola\")" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 14, 231 | "metadata": { 232 | "collapsed": false 233 | }, 234 | "outputs": [ 235 | { 236 | "name": "stdout", 237 | "output_type": "stream", 238 | "text": [ 239 | "Finalized execution. Value of radious: 10\n" 240 | ] 241 | }, 242 | { 243 | "data": { 244 | "text/plain": [ 245 | "62.83185307179586" 246 | ] 247 | }, 248 | "execution_count": 14, 249 | "metadata": {}, 250 | "output_type": "execute_result" 251 | } 252 | ], 253 | "source": [ 254 | "circlePerim(10)" 255 | ] 256 | } 257 | ], 258 | "metadata": { 259 | "kernelspec": { 260 | "display_name": "Python 3", 261 | "language": "python", 262 | "name": "python3" 263 | }, 264 | "language_info": { 265 | "codemirror_mode": { 266 | "name": "ipython", 267 | "version": 3 268 | }, 269 | "file_extension": ".py", 270 | "mimetype": "text/x-python", 271 | "name": "python", 272 | "nbconvert_exporter": "python", 273 | "pygments_lexer": "ipython3", 274 | "version": "3.4.3" 275 | } 276 | }, 277 | "nbformat": 4, 278 | "nbformat_minor": 0 279 | } 280 | -------------------------------------------------------------------------------- /notebooks/unit03/unit03_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Comprehension lists" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 1, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "x = [i**3 for i in range(20)]" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859]\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "print(x)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "Previous comprehension list in the form of a FOR loop:" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 6, 57 | "metadata": { 58 | "collapsed": true 59 | }, 60 | "outputs": [], 61 | "source": [ 62 | "x = []\n", 63 | "for i in range(20):\n", 64 | " x.append(i**3)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 7, 70 | "metadata": { 71 | "collapsed": false 72 | }, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096, 4913, 5832, 6859]\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "print(x)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "metadata": { 90 | "collapsed": false 91 | }, 92 | "outputs": [ 93 | { 94 | "data": { 95 | "text/plain": [ 96 | "[0, 8, 64, 216, 512, 1000, 1728, 2744, 4096, 5832]" 97 | ] 98 | }, 99 | "execution_count": 4, 100 | "metadata": {}, 101 | "output_type": "execute_result" 102 | } 103 | ], 104 | "source": [ 105 | "[i**3 for i in range(20) if i % 2 == 0]" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "Previous comprehension list in the form of a FOR loop:" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 8, 118 | "metadata": { 119 | "collapsed": true 120 | }, 121 | "outputs": [], 122 | "source": [ 123 | "x = []\n", 124 | "for i in range(20):\n", 125 | " if i % 2 == 0:\n", 126 | " x.append(i**3)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 9, 132 | "metadata": { 133 | "collapsed": false 134 | }, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | "[0, 8, 64, 216, 512, 1000, 1728, 2744, 4096, 5832]\n" 141 | ] 142 | } 143 | ], 144 | "source": [ 145 | "print(x)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 5, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "data": { 157 | "text/plain": [ 158 | "[0,\n", 159 | " False,\n", 160 | " 8,\n", 161 | " False,\n", 162 | " 64,\n", 163 | " False,\n", 164 | " 216,\n", 165 | " False,\n", 166 | " 512,\n", 167 | " False,\n", 168 | " 1000,\n", 169 | " False,\n", 170 | " 1728,\n", 171 | " False,\n", 172 | " 2744,\n", 173 | " False,\n", 174 | " 4096,\n", 175 | " False,\n", 176 | " 5832,\n", 177 | " False]" 178 | ] 179 | }, 180 | "execution_count": 5, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "[i**3 if i % 2 == 0 else False for i in range(20)]" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "Previous comprehension list in the form of a FOR loop:" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 10, 199 | "metadata": { 200 | "collapsed": true 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "x = []\n", 205 | "for i in range(20):\n", 206 | " if i % 2 == 0:\n", 207 | " x.append(i**3)\n", 208 | " else:\n", 209 | " x.append(False)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 11, 215 | "metadata": { 216 | "collapsed": false 217 | }, 218 | "outputs": [ 219 | { 220 | "name": "stdout", 221 | "output_type": "stream", 222 | "text": [ 223 | "[0, False, 8, False, 64, False, 216, False, 512, False, 1000, False, 1728, False, 2744, False, 4096, False, 5832, False]\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "print(x)" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": null, 234 | "metadata": { 235 | "collapsed": true 236 | }, 237 | "outputs": [], 238 | "source": [] 239 | } 240 | ], 241 | "metadata": { 242 | "kernelspec": { 243 | "display_name": "Python 3", 244 | "language": "python", 245 | "name": "python3" 246 | }, 247 | "language_info": { 248 | "codemirror_mode": { 249 | "name": "ipython", 250 | "version": 3 251 | }, 252 | "file_extension": ".py", 253 | "mimetype": "text/x-python", 254 | "name": "python", 255 | "nbconvert_exporter": "python", 256 | "pygments_lexer": "ipython3", 257 | "version": "3.4.3" 258 | } 259 | }, 260 | "nbformat": 4, 261 | "nbformat_minor": 0 262 | } 263 | -------------------------------------------------------------------------------- /notebooks/unit03/unit03_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Comprehension dicts" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 8, 20 | "metadata": { 21 | "collapsed": false 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "my_dict = {\"Item_{0}\".format(idx): it**3 for idx,it in enumerate(range(20))}" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 9, 31 | "metadata": { 32 | "collapsed": false 33 | }, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "{'Item_0': 0, 'Item_9': 729, 'Item_19': 6859, 'Item_14': 2744, 'Item_17': 4913, 'Item_18': 5832, 'Item_11': 1331, 'Item_16': 4096, 'Item_4': 64, 'Item_2': 8, 'Item_7': 343, 'Item_3': 27, 'Item_5': 125, 'Item_6': 216, 'Item_10': 1000, 'Item_15': 3375, 'Item_12': 1728, 'Item_8': 512, 'Item_1': 1, 'Item_13': 2197}\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "print(my_dict)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "Previous comprehension dict in the form of a FOR loop:" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 12, 57 | "metadata": { 58 | "collapsed": true 59 | }, 60 | "outputs": [], 61 | "source": [ 62 | "my_dict = {}\n", 63 | "for idx,it in enumerate(range(20)):\n", 64 | " my_dict[\"Item_{0}\".format(idx)] = it**3" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 13, 70 | "metadata": { 71 | "collapsed": false 72 | }, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "{'Item_0': 0, 'Item_9': 729, 'Item_19': 6859, 'Item_14': 2744, 'Item_17': 4913, 'Item_18': 5832, 'Item_11': 1331, 'Item_16': 4096, 'Item_4': 64, 'Item_2': 8, 'Item_7': 343, 'Item_3': 27, 'Item_5': 125, 'Item_6': 216, 'Item_10': 1000, 'Item_15': 3375, 'Item_12': 1728, 'Item_8': 512, 'Item_1': 1, 'Item_13': 2197}\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "print(my_dict)" 84 | ] 85 | } 86 | ], 87 | "metadata": { 88 | "kernelspec": { 89 | "display_name": "Python 3", 90 | "language": "python", 91 | "name": "python3" 92 | }, 93 | "language_info": { 94 | "codemirror_mode": { 95 | "name": "ipython", 96 | "version": 3 97 | }, 98 | "file_extension": ".py", 99 | "mimetype": "text/x-python", 100 | "name": "python", 101 | "nbconvert_exporter": "python", 102 | "pygments_lexer": "ipython3", 103 | "version": "3.4.3" 104 | } 105 | }, 106 | "nbformat": 4, 107 | "nbformat_minor": 0 108 | } 109 | -------------------------------------------------------------------------------- /notebooks/unit03/unit03_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Classes and objects in Python" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Defining a class in Python:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "import math\n", 33 | "\n", 34 | "\n", 35 | "PI = math.pi\n", 36 | "\n", 37 | "class Circle:\n", 38 | " \n", 39 | " def __init__(self, rad):\n", 40 | " self.rad = rad\n", 41 | "\n", 42 | " def getPerim(self):\n", 43 | " \"\"\"\n", 44 | " Compute circle perimeter\n", 45 | " \"\"\"\n", 46 | " return 2 * PI * self.rad\n", 47 | "\n", 48 | " def getArea(self):\n", 49 | " \"\"\"\n", 50 | " Compute circle area\n", 51 | " \"\"\"\n", 52 | " return PI * (self.rad**2)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "Creating an instance of the Circle class (creating an object of the type Circle):" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 2, 65 | "metadata": { 66 | "collapsed": false 67 | }, 68 | "outputs": [], 69 | "source": [ 70 | "cr = Circle(10)" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 3, 76 | "metadata": { 77 | "collapsed": false 78 | }, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "__main__.Circle" 84 | ] 85 | }, 86 | "execution_count": 3, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "type(cr)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "Calling getArea method:" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 4, 105 | "metadata": { 106 | "collapsed": false 107 | }, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "314.1592653589793" 113 | ] 114 | }, 115 | "execution_count": 4, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "cr.getArea()" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "Calling getPerim method:" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 5, 134 | "metadata": { 135 | "collapsed": false 136 | }, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "62.83185307179586" 142 | ] 143 | }, 144 | "execution_count": 5, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "cr.getPerim()" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "Accessing rad property:" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 6, 163 | "metadata": { 164 | "collapsed": false 165 | }, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "10" 171 | ] 172 | }, 173 | "execution_count": 6, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "cr.rad" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "## Inheritance" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "Defining the Circle class inheriting from Geom class:" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 7, 199 | "metadata": { 200 | "collapsed": true 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "import math\n", 205 | "\n", 206 | "\n", 207 | "PI = math.pi\n", 208 | "\n", 209 | "class Geom:\n", 210 | " def __init__(self, title):\n", 211 | " self.title = title\n", 212 | "\n", 213 | " def getTitle(self):\n", 214 | " return self.title\n", 215 | "\n", 216 | "class Circle(Geom):\n", 217 | " \n", 218 | " def __init__(self, rad):\n", 219 | " self.rad = rad\n", 220 | " super(Circle, self).__init__(\"Welcome to circle world!\")\n", 221 | "\n", 222 | " def getPerim(self):\n", 223 | " \"\"\"\n", 224 | " Compute circle perimeter\n", 225 | " \"\"\"\n", 226 | " return 2 * PI * self.rad\n", 227 | "\n", 228 | " def getArea(self):\n", 229 | " \"\"\"\n", 230 | " Compute circle area\n", 231 | " \"\"\"\n", 232 | " return PI * (self.rad**2)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "Instantiating Circle class again:" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 8, 245 | "metadata": { 246 | "collapsed": true 247 | }, 248 | "outputs": [], 249 | "source": [ 250 | "cr2 = Circle(5)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 9, 256 | "metadata": { 257 | "collapsed": false 258 | }, 259 | "outputs": [ 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "78.53981633974483" 264 | ] 265 | }, 266 | "execution_count": 9, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "cr2.getArea()" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 10, 278 | "metadata": { 279 | "collapsed": false 280 | }, 281 | "outputs": [ 282 | { 283 | "data": { 284 | "text/plain": [ 285 | "31.41592653589793" 286 | ] 287 | }, 288 | "execution_count": 10, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "cr2.getPerim()" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "Accessing rad property:" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 11, 307 | "metadata": { 308 | "collapsed": false 309 | }, 310 | "outputs": [ 311 | { 312 | "data": { 313 | "text/plain": [ 314 | "5" 315 | ] 316 | }, 317 | "execution_count": 11, 318 | "metadata": {}, 319 | "output_type": "execute_result" 320 | } 321 | ], 322 | "source": [ 323 | "cr2.rad" 324 | ] 325 | }, 326 | { 327 | "cell_type": "markdown", 328 | "metadata": {}, 329 | "source": [ 330 | "Calling getTitle method:" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": 12, 336 | "metadata": { 337 | "collapsed": false 338 | }, 339 | "outputs": [ 340 | { 341 | "data": { 342 | "text/plain": [ 343 | "'Welcome to circle world!'" 344 | ] 345 | }, 346 | "execution_count": 12, 347 | "metadata": {}, 348 | "output_type": "execute_result" 349 | } 350 | ], 351 | "source": [ 352 | "cr2.getTitle()" 353 | ] 354 | }, 355 | { 356 | "cell_type": "code", 357 | "execution_count": 13, 358 | "metadata": { 359 | "collapsed": false 360 | }, 361 | "outputs": [], 362 | "source": [ 363 | "cr2.title = 'Welcome again to circle world!'" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 14, 369 | "metadata": { 370 | "collapsed": false 371 | }, 372 | "outputs": [ 373 | { 374 | "data": { 375 | "text/plain": [ 376 | "'Welcome again to circle world!'" 377 | ] 378 | }, 379 | "execution_count": 14, 380 | "metadata": {}, 381 | "output_type": "execute_result" 382 | } 383 | ], 384 | "source": [ 385 | "cr2.getTitle()" 386 | ] 387 | }, 388 | { 389 | "cell_type": "markdown", 390 | "metadata": {}, 391 | "source": [ 392 | "## Import a Class" 393 | ] 394 | }, 395 | { 396 | "cell_type": "markdown", 397 | "metadata": {}, 398 | "source": [ 399 | "Store Circle class last code on a file called geom.py inside a folder \"pygeom\".\n", 400 | "\n", 401 | "Create an empty file called ```__init__.py``` inside \"pygeom\" folder.\n", 402 | "\n", 403 | "Import the class:" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 15, 409 | "metadata": { 410 | "collapsed": true 411 | }, 412 | "outputs": [], 413 | "source": [ 414 | "from pygeom.geom import Circle" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": 16, 420 | "metadata": { 421 | "collapsed": true 422 | }, 423 | "outputs": [], 424 | "source": [ 425 | "cr3 = Circle(20)" 426 | ] 427 | }, 428 | { 429 | "cell_type": "code", 430 | "execution_count": 17, 431 | "metadata": { 432 | "collapsed": false 433 | }, 434 | "outputs": [ 435 | { 436 | "data": { 437 | "text/plain": [ 438 | "1256.6370614359173" 439 | ] 440 | }, 441 | "execution_count": 17, 442 | "metadata": {}, 443 | "output_type": "execute_result" 444 | } 445 | ], 446 | "source": [ 447 | "cr3.getArea()" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 18, 453 | "metadata": { 454 | "collapsed": false 455 | }, 456 | "outputs": [ 457 | { 458 | "data": { 459 | "text/plain": [ 460 | "125.66370614359172" 461 | ] 462 | }, 463 | "execution_count": 18, 464 | "metadata": {}, 465 | "output_type": "execute_result" 466 | } 467 | ], 468 | "source": [ 469 | "cr3.getPerim()" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 19, 475 | "metadata": { 476 | "collapsed": false 477 | }, 478 | "outputs": [ 479 | { 480 | "data": { 481 | "text/plain": [ 482 | "'Welcome to circle world!'" 483 | ] 484 | }, 485 | "execution_count": 19, 486 | "metadata": {}, 487 | "output_type": "execute_result" 488 | } 489 | ], 490 | "source": [ 491 | "cr3.getTitle()" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 20, 497 | "metadata": { 498 | "collapsed": false 499 | }, 500 | "outputs": [ 501 | { 502 | "data": { 503 | "text/plain": [ 504 | "pygeom.geom.Circle" 505 | ] 506 | }, 507 | "execution_count": 20, 508 | "metadata": {}, 509 | "output_type": "execute_result" 510 | } 511 | ], 512 | "source": [ 513 | "type(cr3)" 514 | ] 515 | } 516 | ], 517 | "metadata": { 518 | "kernelspec": { 519 | "display_name": "Python 3", 520 | "language": "python", 521 | "name": "python3" 522 | }, 523 | "language_info": { 524 | "codemirror_mode": { 525 | "name": "ipython", 526 | "version": 3 527 | }, 528 | "file_extension": ".py", 529 | "mimetype": "text/x-python", 530 | "name": "python", 531 | "nbconvert_exporter": "python", 532 | "pygments_lexer": "ipython3", 533 | "version": "3.4.3" 534 | } 535 | }, 536 | "nbformat": 4, 537 | "nbformat_minor": 0 538 | } 539 | -------------------------------------------------------------------------------- /notebooks/unit05/unit05_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Pyproj (Proj.4 Python binding)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "## Working with Pyproj" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "Importing library:" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 2, 34 | "metadata": { 35 | "collapsed": false 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "import pyproj" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "First example (function reprojecting coordinates using EPSG codes):" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": { 53 | "collapsed": true 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "def runTest01(x1, y1):\n", 58 | " p1 = pyproj.Proj(init='epsg:25830')\n", 59 | " p2 = pyproj.Proj(init='epsg:25829')\n", 60 | "\n", 61 | " x2, y2 = pyproj.transform(p1,p2,x1,y1)\n", 62 | " return(x2,y2)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "Second example (function reprojecting coordinates using Proj4 definitions):" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": { 76 | "collapsed": true 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "def runTest02(x1, y1):\n", 81 | " p1 = pyproj.Proj(proj='utm',zone=30,ellps='GRS80',towgs84='0,0,0,0,0,0,0',units='m')\n", 82 | " p2 = pyproj.Proj(proj='utm',zone=29,ellps='GRS80',towgs84='0,0,0,0,0,0,0',units='m')\n", 83 | "\n", 84 | " x2, y2 = pyproj.transform(p1,p2,x1,y1)\n", 85 | "\n", 86 | " return(x2,y2)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Run examples (No differences in results):" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 5, 99 | "metadata": { 100 | "collapsed": true 101 | }, 102 | "outputs": [], 103 | "source": [ 104 | "x1, y1 = (209105,4138693)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 6, 110 | "metadata": { 111 | "collapsed": true 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "res01 = runTest01(x1, y1)\n", 116 | "res02 = runTest02(x1, y1)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 7, 122 | "metadata": { 123 | "collapsed": false 124 | }, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "x1, y1: 209105,4138693\n", 131 | "Test01: (740575.202318952, 4137093.0821367116)\n", 132 | "Test02: (740575.202318952, 4137093.0821367116)\n", 133 | "True\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "print(\"x1, y1: {0},{1}\\nTest01: {2}\\nTest02: {3}\".format(x1, y1, res01, res02))\n", 139 | "print(res01 == res02)" 140 | ] 141 | } 142 | ], 143 | "metadata": { 144 | "kernelspec": { 145 | "display_name": "Python 3", 146 | "language": "python", 147 | "name": "python3" 148 | }, 149 | "language_info": { 150 | "codemirror_mode": { 151 | "name": "ipython", 152 | "version": 3 153 | }, 154 | "file_extension": ".py", 155 | "mimetype": "text/x-python", 156 | "name": "python", 157 | "nbconvert_exporter": "python", 158 | "pygments_lexer": "ipython3", 159 | "version": "3.4.3" 160 | } 161 | }, 162 | "nbformat": 4, 163 | "nbformat_minor": 0 164 | } 165 | -------------------------------------------------------------------------------- /notebooks/unit05/unit05_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Pyproj Geod examples (with GeodesicLinesToGIS library)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Example: creating geodesic lines with library GeodesicLinesToGIS which is using Pyproj Geod module to do computations:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "https://github.com/GeographicaGS/GeodesicLinesToGIS" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 1, 34 | "metadata": { 35 | "collapsed": true 36 | }, 37 | "outputs": [], 38 | "source": [ 39 | "import os\n", 40 | "import numpy as np\n", 41 | "from geodesiclinestogis.geodesicline2gisfile import GeodesicLine2Gisfile" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "Coordinates list to compute geodesic lines between origins and destinies:\n", 49 | "\n", 50 | "lons_lats: input coordinates. (start longitude, start latitude, end longitude, end latitude)\n" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": { 57 | "collapsed": true 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "coord = [(-6.,37.,-145.,11.),\n", 62 | " (-150.,37.,140.,11.),\n", 63 | " (-6.,37.,120.,50.),\n", 64 | " (-3.6,40.5,-118.4,33.9),\n", 65 | " (-118.4,33.9,139.8,35.5),\n", 66 | " (-118.4,33.9,104.,1.35),\n", 67 | " (-118.4,33.9,151.,-33.9),\n", 68 | " (-20.4,33.9,178.,-33.9)\n", 69 | " ]" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "For faster computations, coordinates list is converted in a NumPy array:" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 3, 82 | "metadata": { 83 | "collapsed": true 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "coord_np = np.array(coord)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "Create folder to store layers:" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": { 101 | "collapsed": true 102 | }, 103 | "outputs": [], 104 | "source": [ 105 | "folderpath = \"/tmp/geod_line\"\n", 106 | "\n", 107 | "if not os.path.exists(folderpath):\n", 108 | " os.makedirs(folderpath)\n", 109 | "\n", 110 | "layername = \"geodesicline\"" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Run computations:" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 5, 123 | "metadata": { 124 | "collapsed": true 125 | }, 126 | "outputs": [], 127 | "source": [ 128 | "gtg = GeodesicLine2Gisfile()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 6, 134 | "metadata": { 135 | "collapsed": true 136 | }, 137 | "outputs": [], 138 | "source": [ 139 | "props = [\"prop01\",\"prop02\"]" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 7, 145 | "metadata": { 146 | "collapsed": false 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "gtg.gdlToGisFileMulti(coord_np, folderpath, layername, prop=props)" 151 | ] 152 | } 153 | ], 154 | "metadata": { 155 | "kernelspec": { 156 | "display_name": "Python 2", 157 | "language": "python", 158 | "name": "python2" 159 | }, 160 | "language_info": { 161 | "codemirror_mode": { 162 | "name": "ipython", 163 | "version": 2 164 | }, 165 | "file_extension": ".py", 166 | "mimetype": "text/x-python", 167 | "name": "python", 168 | "nbconvert_exporter": "python", 169 | "pygments_lexer": "ipython2", 170 | "version": "2.7.6" 171 | } 172 | }, 173 | "nbformat": 4, 174 | "nbformat_minor": 0 175 | } 176 | -------------------------------------------------------------------------------- /notebooks/unit05/unit05_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Writing vector data with GDAL (OGR)" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 73, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import os\n", 26 | "import sys\n", 27 | "from osgeo import ogr" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": {}, 33 | "source": [ 34 | "## Create new layer from another layer (filtering attributes)" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 74, 40 | "metadata": { 41 | "collapsed": false 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "driver = ogr.GetDriverByName('ESRI shapefile')" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 75, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "geol_fl = \"../../data/geologia_marina/mm01_geologia.shp\"" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 76, 62 | "metadata": { 63 | "collapsed": false 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "in_ds = driver.Open(geol_fl, 0)\n", 68 | "if in_ds is None:\n", 69 | " sys.exit('Error opening file')" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "Creating output datasource (check first if exists):" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 77, 82 | "metadata": { 83 | "collapsed": true 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "out_file = \"/tmp/abanico_delt.shp\"" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 78, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "if os.path.exists(out_file):\n", 99 | " driver.DeleteDataSource(out_file)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 79, 105 | "metadata": { 106 | "collapsed": true 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "out_ds = driver.CreateDataSource(out_file)" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "Get input layer:" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 80, 123 | "metadata": { 124 | "collapsed": true 125 | }, 126 | "outputs": [], 127 | "source": [ 128 | "in_lyr = in_ds.GetLayer()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "Set attribute filter:" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 81, 141 | "metadata": { 142 | "collapsed": false 143 | }, 144 | "outputs": [ 145 | { 146 | "data": { 147 | "text/plain": [ 148 | "0" 149 | ] 150 | }, 151 | "execution_count": 81, 152 | "metadata": {}, 153 | "output_type": "execute_result" 154 | } 155 | ], 156 | "source": [ 157 | "in_lyr.SetAttributeFilter(\"DESCRIPCIO = 'Abanico deltaico'\")" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "Create output layer:" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 82, 170 | "metadata": { 171 | "collapsed": false 172 | }, 173 | "outputs": [], 174 | "source": [ 175 | "out_lyr = out_ds.CreateLayer(\"abanico_delt\", geom_type=ogr.wkbPolygon )" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "Creating fields in output layer:" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 83, 188 | "metadata": { 189 | "collapsed": false 190 | }, 191 | "outputs": [], 192 | "source": [ 193 | "in_lyr_def = in_lyr.GetLayerDefn()\n", 194 | "for i in range(0, in_lyr_def.GetFieldCount()):\n", 195 | " field_def = in_lyr_def.GetFieldDefn(i)\n", 196 | " out_lyr.CreateField(field_def)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "Building features in output layer:" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 84, 209 | "metadata": { 210 | "collapsed": true 211 | }, 212 | "outputs": [], 213 | "source": [ 214 | "out_lyr_def = out_lyr.GetLayerDefn()\n", 215 | "\n", 216 | "for in_ft in in_lyr:\n", 217 | " out_ft = ogr.Feature(out_lyr_def)\n", 218 | "\n", 219 | " for i in range(0, out_lyr_def.GetFieldCount()):\n", 220 | " field_def = out_lyr_def.GetFieldDefn(i)\n", 221 | " out_ft.SetField(out_lyr_def.GetFieldDefn(i).GetNameRef(), in_ft.GetField(i))\n", 222 | "\n", 223 | " geom = in_ft.GetGeometryRef()\n", 224 | " out_ft.SetGeometry(geom.Clone())\n", 225 | " \n", 226 | " out_lyr.CreateFeature(out_ft)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "markdown", 231 | "metadata": {}, 232 | "source": [ 233 | "Closing datasets:" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 85, 239 | "metadata": { 240 | "collapsed": true 241 | }, 242 | "outputs": [], 243 | "source": [ 244 | "in_ds = None\n", 245 | "out_ds = None" 246 | ] 247 | } 248 | ], 249 | "metadata": { 250 | "kernelspec": { 251 | "display_name": "Python 3", 252 | "language": "python", 253 | "name": "python3" 254 | }, 255 | "language_info": { 256 | "codemirror_mode": { 257 | "name": "ipython", 258 | "version": 3 259 | }, 260 | "file_extension": ".py", 261 | "mimetype": "text/x-python", 262 | "name": "python", 263 | "nbconvert_exporter": "python", 264 | "pygments_lexer": "ipython3", 265 | "version": "3.4.3" 266 | } 267 | }, 268 | "nbformat": 4, 269 | "nbformat_minor": 0 270 | } 271 | -------------------------------------------------------------------------------- /notebooks/unit05/unit05_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Reading a raster with GDAL" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 26, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "from osgeo import gdal" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 27, 31 | "metadata": { 32 | "collapsed": true 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "input_filename = \"../../data/mde/h10_1050_2-2/h10_1050_2-2.tif\"" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 28, 42 | "metadata": { 43 | "collapsed": true 44 | }, 45 | "outputs": [], 46 | "source": [ 47 | "src_ds = gdal.Open(input_filename)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 29, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "PROJCS[\"ETRS89 / UTM zone 30N (N-E)\",GEOGCS[\"ETRS89\",DATUM[\"European_Terrestrial_Reference_System_1989\",SPHEROID[\"GRS 1980\",6378137,298.2572221010002,AUTHORITY[\"EPSG\",\"7019\"]],AUTHORITY[\"EPSG\",\"6258\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4258\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",-3],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AUTHORITY[\"EPSG\",\"3042\"]]\n" 62 | ] 63 | } 64 | ], 65 | "source": [ 66 | "src_crs = src_ds.GetProjection()\n", 67 | "print(src_crs)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 30, 73 | "metadata": { 74 | "collapsed": false 75 | }, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "(282285.0, 10.0, 0.0, 4074645.0, 0.0, -10.0)\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "src_geot = src_ds.GetGeoTransform()\n", 87 | "print(src_geot)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "GDAL Geotransform (affine transformation coefficients):\n", 95 | "- GeoTransform[0] - top left x\n", 96 | "- GeoTransform[1] - x scale\n", 97 | "- GeoTransform[2] - rotation (shear in x direction)\n", 98 | "- GeoTransform[3] - top left y\n", 99 | "- GeoTransform[4] - rotation (shear in y direction)\n", 100 | "- GeoTransform[5] - y scale (negative value)\n", 101 | "\n", 102 | "The affine transform coefficients map pixel coordinates into georeferenced space.\n", 103 | "\n", 104 | "More info: http://www.gdal.org/gdal_datamodel.html\n" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 31, 110 | "metadata": { 111 | "collapsed": false 112 | }, 113 | "outputs": [ 114 | { 115 | "name": "stdout", 116 | "output_type": "stream", 117 | "text": [ 118 | "1\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "count_bands = src_ds.RasterCount\n", 124 | "print(count_bands)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 32, 130 | "metadata": { 131 | "collapsed": false 132 | }, 133 | "outputs": [ 134 | { 135 | "name": "stdout", 136 | "output_type": "stream", 137 | "text": [ 138 | "Minimum=557.4820, Maximum=1,641.9017, Mean=1,009.9320, StdDev=206.4086\n" 139 | ] 140 | } 141 | ], 142 | "source": [ 143 | "for band in range(count_bands):\n", 144 | " src_band = src_ds.GetRasterBand(band + 1)\n", 145 | " stats = src_band.GetStatistics(True, True)\n", 146 | " if stats:\n", 147 | " print(\"Minimum={:,.4f}, Maximum={:,.4f}, Mean={:,.4f}, StdDev={:,.4f}\".format(stats[0], stats[1], stats[2], stats[3]))" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 33, 153 | "metadata": { 154 | "collapsed": true 155 | }, 156 | "outputs": [], 157 | "source": [ 158 | "src_ds = None" 159 | ] 160 | } 161 | ], 162 | "metadata": { 163 | "kernelspec": { 164 | "display_name": "Python 3", 165 | "language": "python", 166 | "name": "python3" 167 | }, 168 | "language_info": { 169 | "codemirror_mode": { 170 | "name": "ipython", 171 | "version": 3 172 | }, 173 | "file_extension": ".py", 174 | "mimetype": "text/x-python", 175 | "name": "python", 176 | "nbconvert_exporter": "python", 177 | "pygments_lexer": "ipython3", 178 | "version": "3.4.3" 179 | } 180 | }, 181 | "nbformat": 4, 182 | "nbformat_minor": 0 183 | } 184 | -------------------------------------------------------------------------------- /notebooks/unit05/unit05_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Writing raster data with GDAL" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 6, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "from osgeo import gdal" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "## Translate datasets in GDAL\n", 33 | "Translate example: NetCDF datset to GeoTiff." 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 10, 39 | "metadata": { 40 | "collapsed": true 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "input_filename = \"../../data/mslp_gfs/mslp_europe.nc\"\n", 45 | "src_ds = gdal.Open(input_filename)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "Run translate:" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 11, 58 | "metadata": { 59 | "collapsed": true 60 | }, 61 | "outputs": [], 62 | "source": [ 63 | "out_format_r = \"GTiff\"\n", 64 | "driver_r = gdal.GetDriverByName(out_format_r)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 12, 70 | "metadata": { 71 | "collapsed": true 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "out_rasterfile = \"/tmp/mslp_europe_gdal.tif\"\n", 76 | "dst_ds = driver_r.CreateCopy(out_rasterfile, src_ds)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 13, 82 | "metadata": { 83 | "collapsed": true 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "dst_ds = None\n", 88 | "src_ds = None" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "### Translate dataset with creation options" 96 | ] 97 | }, 98 | { 99 | "cell_type": "markdown", 100 | "metadata": {}, 101 | "source": [ 102 | "A complex translation adding LZW compression to GeoTiff:" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 14, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "input_filename = \"../../data/mslp_gfs/mslp_europe.nc\"\n", 114 | "src_ds = gdal.Open(input_filename)\n", 115 | "\n", 116 | "out_format_r = \"GTiff\"\n", 117 | "driver_r = gdal.GetDriverByName(out_format_r)\n", 118 | "\n", 119 | "out_rasterfile = \"/tmp/mslp_europe_gdal_compress.tif\"\n", 120 | "dst_ds = driver_r.CreateCopy(out_rasterfile, src_ds, 0, ['COMPRESS=LZW'])\n", 121 | "\n", 122 | "dst_ds = None\n", 123 | "src_ds = None" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "More creation options for GeoTiff:\n", 131 | "\n", 132 | "http://www.gdal.org/frmt_gtiff.html" 133 | ] 134 | } 135 | ], 136 | "metadata": { 137 | "kernelspec": { 138 | "display_name": "Python 2", 139 | "language": "python", 140 | "name": "python2" 141 | }, 142 | "language_info": { 143 | "codemirror_mode": { 144 | "name": "ipython", 145 | "version": 2 146 | }, 147 | "file_extension": ".py", 148 | "mimetype": "text/x-python", 149 | "name": "python", 150 | "nbconvert_exporter": "python", 151 | "pygments_lexer": "ipython2", 152 | "version": "2.7.6" 153 | } 154 | }, 155 | "nbformat": 4, 156 | "nbformat_minor": 0 157 | } 158 | -------------------------------------------------------------------------------- /notebooks/unit05/unit05_08.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Writing vector data with Fiona" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Importing library:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "import fiona" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "## Create a new dataset from an existing file" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "Creating dataset filtering attribute:" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 2, 52 | "metadata": { 53 | "collapsed": false 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "with fiona.open(\"../../data/clima/mf06_clima.shp\", 'r') as in_ds:\n", 58 | " in_drv = in_ds.driver\n", 59 | " in_crs = in_ds.crs\n", 60 | " in_sch = in_ds.schema\n", 61 | " \n", 62 | " with fiona.open(\"/tmp/clima_continental.shp\", 'w', \n", 63 | " driver=in_drv,\n", 64 | " crs=in_crs,\n", 65 | " schema=in_sch) as out_ds:\n", 66 | " for ft in in_ds:\n", 67 | " if ft['properties'].get('TIPO_CLIM') == 'Mediterráneo Continental':\n", 68 | " out_ds.write(ft)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": { 74 | "collapsed": false 75 | }, 76 | "source": [ 77 | "Converting dataset to another file format (GeoJSON):" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 13, 83 | "metadata": { 84 | "collapsed": false 85 | }, 86 | "outputs": [], 87 | "source": [ 88 | "with fiona.open(\"../../data/clima/mf06_clima.shp\", 'r') as in_ds:\n", 89 | " in_crs = in_ds.crs\n", 90 | " in_sch = in_ds.schema\n", 91 | " \n", 92 | " with fiona.open(\"/tmp/clima_continental.geojson\", 'w', \n", 93 | " driver=\"GeoJSON\",\n", 94 | " crs=in_crs,\n", 95 | " schema=in_sch) as out_ds:\n", 96 | " for ft in in_ds:\n", 97 | " if ft['properties'].get('TIPO_CLIM') == 'Mediterráneo Continental':\n", 98 | " out_ds.write(ft)" 99 | ] 100 | } 101 | ], 102 | "metadata": { 103 | "kernelspec": { 104 | "display_name": "Python 3", 105 | "language": "python", 106 | "name": "python3" 107 | }, 108 | "language_info": { 109 | "codemirror_mode": { 110 | "name": "ipython", 111 | "version": 3 112 | }, 113 | "file_extension": ".py", 114 | "mimetype": "text/x-python", 115 | "name": "python", 116 | "nbconvert_exporter": "python", 117 | "pygments_lexer": "ipython3", 118 | "version": "3.4.3" 119 | } 120 | }, 121 | "nbformat": 4, 122 | "nbformat_minor": 0 123 | } 124 | -------------------------------------------------------------------------------- /notebooks/unit05/unit05_09.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Reading and writing raster data with Rasterio" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Importing library:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 36, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "import rasterio" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "## Translate datasets in Rasterio\n", 40 | "\n", 41 | "Translate example: NetCDF datset to Geotiff." 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": 37, 47 | "metadata": { 48 | "collapsed": true 49 | }, 50 | "outputs": [], 51 | "source": [ 52 | "input_file = \"../../data/mslp_gfs/mslp_europe.nc\"\n", 53 | "out_tif_file = \"/tmp/mslp_europe_rst.tif\"" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "Run translate:" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 38, 66 | "metadata": { 67 | "collapsed": false 68 | }, 69 | "outputs": [], 70 | "source": [ 71 | "with rasterio.drivers():\n", 72 | " rasterio.copy(input_file, out_tif_file, driver='GTiff')" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "Reading raster data and printing basic metadata:" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "execution_count": 39, 85 | "metadata": { 86 | "collapsed": false 87 | }, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "- Driver: netCDF\n", 94 | "\n", 95 | "- Width 320, Height 180\n", 96 | "\n", 97 | "- Shape: (180, 320)\n", 98 | "\n", 99 | "- CRS (Proj4): \n", 100 | "{'a': 6367470, 'proj': 'longlat', 'no_defs': True, 'b': 6367470}\n", 101 | "\n", 102 | "- CRS (WKT): \n", 103 | "GEOGCS[\"unnamed ellipse\",DATUM[\"unknown\",SPHEROID[\"unnamed\",6367470,0]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]]\n", 104 | "\n", 105 | "- Affine transf. matrix: \n", 106 | "| 0.25, 0.00,-30.12|\n", 107 | "| 0.00,-0.25, 75.12|\n", 108 | "| 0.00, 0.00, 1.00|\n", 109 | "\n", 110 | "- Band count: 1\n", 111 | "\n", 112 | "- Band indexes: [1]\n", 113 | "\n", 114 | "- Metadata summary: \n", 115 | "{'height': 180, 'dtype': 'float64', 'count': 1, 'crs': {'a': 6367470, 'proj': 'longlat', 'no_defs': True, 'b': 6367470}, 'affine': Affine(0.25, 0.0, -30.125,\n", 116 | " 0.0, -0.25, 75.125), 'width': 320, 'transform': (-30.125, 0.25, 0.0, 75.125, 0.0, -0.25), 'driver': 'netCDF', 'nodata': 9.969209968386869e+36}\n", 117 | "\n", 118 | "- GDAL tags: \n", 119 | "{'crs#semi_major_axis': '6367470', 'crs#GeoTransform': '-30.125 0.25 0 75.125 0 -0.25 ', 'Band1#GRIB_SHORT_NAME': '0-SFC', 'Band1#grid_mapping': 'crs', 'Band1#GRIB_FORECAST_SECONDS': '0 sec', 'crs#grid_mapping_name': 'latitude_longitude', 'crs#inverse_flattening': '0', 'Band1#GRIB_REF_TIME': ' 283996800 sec UTC', 'crs#longitude_of_prime_meridian': '0', 'lon#long_name': 'longitude', 'crs#spatial_ref': 'GEOGCS[\"unnamed ellipse\",DATUM[\"unknown\",SPHEROID[\"unnamed\",6367470,0]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433]]', 'Band1#_FillValue': '9.969209968386869e+36', 'lat#long_name': 'latitude', 'Band1#GRIB_UNIT': '[Pa]', 'NC_GLOBAL#history': 'Sat Apr 02 22:54:59 2016: GDAL CreateCopy( /tmp/mslp_europe.nc, ... )', 'Band1#GRIB_ELEMENT': 'MSL', 'Band1#GRIB_VALID_TIME': ' 283996800 sec UTC', 'NC_GLOBAL#Conventions': 'CF-1.5', 'lat#units': 'degrees_north', 'Band1#long_name': 'GDAL Band Number 1', 'lon#units': 'degrees_east', 'lat#standard_name': 'latitude', 'Band1#GRIB_COMMENT': 'Mean sea level pressure [Pa]', 'lon#standard_name': 'longitude', 'NC_GLOBAL#GDAL': 'GDAL 1.11.2, released 2015/02/10'}\n", 120 | "\n" 121 | ] 122 | } 123 | ], 124 | "source": [ 125 | "with rasterio.drivers():\n", 126 | " with rasterio.open(input_file) as src:\n", 127 | " print(\"- Driver: {}\\n\".format(src.driver))\n", 128 | " print(\"- Width {}, Height {}\\n\".format(src.width, src.height))\n", 129 | " print(\"- Shape: {}\\n\".format(src.shape))\n", 130 | " print(\"- CRS (Proj4): \\n{}\\n\".format(src.crs))\n", 131 | " print(\"- CRS (WKT): \\n{}\\n\".format(src.crs_wkt))\n", 132 | " print(\"- Affine transf. matrix: \\n{}\\n\".format(src.affine))\n", 133 | " print(\"- Band count: {}\\n\".format(src.count))\n", 134 | " print(\"- Band indexes: {}\\n\".format(src.indexes))\n", 135 | " print(\"- Metadata summary: \\n{}\\n\".format(src.meta))\n", 136 | " print(\"- GDAL tags: \\n{}\\n\".format(src.tags()))" 137 | ] 138 | }, 139 | { 140 | "cell_type": "markdown", 141 | "metadata": {}, 142 | "source": [ 143 | "### Affine transformation in Rasterio\n", 144 | "In the above result we can see affine transformation matrix which is described below:\n", 145 | "```\n", 146 | "a: Scale X \n", 147 | "e: Scale Y\n", 148 | "d: Rotation (shear in Y direction)\n", 149 | "b: Rotation (shear in X direction)\n", 150 | "c: Translation X\n", 151 | "f: Translation Y\n", 152 | "\n", 153 | "| x' | | a b c | | x |\n", 154 | "| y' | = | d e f | | y |\n", 155 | "| 1 | | 0 0 1 | | 1 |\n", 156 | "\n", 157 | "\n", 158 | "GDAL coefficients order in GeoTransform:\n", 159 | "c, a, b, f, d, e\n", 160 | "```\n", 161 | "More info:\n", 162 | "\n", 163 | "https://github.com/sgillies/affine\n", 164 | "\n", 165 | "https://en.wikipedia.org/wiki/File:2D_affine_transformation_matrix.svg" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": null, 171 | "metadata": { 172 | "collapsed": true 173 | }, 174 | "outputs": [], 175 | "source": [] 176 | } 177 | ], 178 | "metadata": { 179 | "kernelspec": { 180 | "display_name": "Python 3", 181 | "language": "python", 182 | "name": "python3" 183 | }, 184 | "language_info": { 185 | "codemirror_mode": { 186 | "name": "ipython", 187 | "version": 3 188 | }, 189 | "file_extension": ".py", 190 | "mimetype": "text/x-python", 191 | "name": "python", 192 | "nbconvert_exporter": "python", 193 | "pygments_lexer": "ipython3", 194 | "version": "3.4.3" 195 | } 196 | }, 197 | "nbformat": 4, 198 | "nbformat_minor": 0 199 | } 200 | -------------------------------------------------------------------------------- /notebooks/unit06/unit06_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Vector data CRS transform with Fiona" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "In this example we are going to reproject vector dataset.\n", 22 | "\n", 23 | "From EPSG:25830 to EPSG:25829" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 1, 29 | "metadata": { 30 | "collapsed": true 31 | }, 32 | "outputs": [], 33 | "source": [ 34 | "import fiona\n", 35 | "from fiona import transform" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "Open ESRI Shapefile file and export to GeoJSON (after reprojection):" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 2, 48 | "metadata": { 49 | "collapsed": false 50 | }, 51 | "outputs": [], 52 | "source": [ 53 | "with fiona.open(\"../../data/clima/mf06_clima.shp\", 'r') as in_ds:\n", 54 | " in_crs = in_ds.crs\n", 55 | " out_crs={'init': 'epsg:25829'}\n", 56 | " in_sch = in_ds.schema\n", 57 | " \n", 58 | " with fiona.open(\"/tmp/clima_continental_utm29.geojson\", 'w', \n", 59 | " driver=\"GeoJSON\",\n", 60 | " crs=out_crs,\n", 61 | " schema=in_sch) as out_ds:\n", 62 | " for ft in in_ds:\n", 63 | " if ft['properties'].get('TIPO_CLIM') == 'Mediterráneo Continental':\n", 64 | " geom_in = ft.get('geometry')\n", 65 | " geom_out = transform.transform_geom(in_crs, out_crs, geom_in)\n", 66 | " ft['geometry'] = geom_out\n", 67 | " out_ds.write(ft)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 3, 73 | "metadata": { 74 | "collapsed": true 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "transform.transform?" 79 | ] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": { 85 | "collapsed": true 86 | }, 87 | "outputs": [], 88 | "source": [] 89 | } 90 | ], 91 | "metadata": { 92 | "kernelspec": { 93 | "display_name": "Python 3", 94 | "language": "python", 95 | "name": "python3" 96 | }, 97 | "language_info": { 98 | "codemirror_mode": { 99 | "name": "ipython", 100 | "version": 3 101 | }, 102 | "file_extension": ".py", 103 | "mimetype": "text/x-python", 104 | "name": "python", 105 | "nbconvert_exporter": "python", 106 | "pygments_lexer": "ipython3", 107 | "version": "3.4.3" 108 | } 109 | }, 110 | "nbformat": 4, 111 | "nbformat_minor": 0 112 | } 113 | -------------------------------------------------------------------------------- /notebooks/unit06/unit06_04.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Vector analysis with Shapely and NumPy" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "We review in this examples how to use Shapely with vector data and NumPy:" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 1, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "import numpy as np\n", 33 | "from shapely.wkt import loads\n", 34 | "from shapely.geometry import LineString" 35 | ] 36 | }, 37 | { 38 | "cell_type": "code", 39 | "execution_count": 2, 40 | "metadata": { 41 | "collapsed": true 42 | }, 43 | "outputs": [], 44 | "source": [ 45 | "def runTest01():\n", 46 | " wkt_ln = \"LINESTRING(178044.505034367 4080523.32981373,185051.202770193 4115968.97717771,218436.056686834 4125036.46836944,231625.134776712 4158833.48097265,229564.341325156 4174495.51120244,216787.42192546 4189333.22405021,212253.676331916 4213238.42808471,216787.421925395 4231373.41045699)\"\n", 47 | "\n", 48 | " ln = loads(wkt_ln)\n", 49 | " ln_np = np.array(ln)\n", 50 | "\n", 51 | " ln_buf = ln.buffer(20000)\n", 52 | "\n", 53 | " return(ln_np, ln_buf.wkt)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "def runTest02():\n", 65 | " lat = np.array([88.2598888, 87.8656022505306, 87.47131570106119, 87.07729258677767, 86.68423509786567, 86.29117760895366, 85.89812012004164, 85.50506263112963, 85.11200514221763, 84.71894765330562, 84.35408870391326, 84.00746812090851, 83.66121815097686, 83.31589924664645, 82.96712093784359, 82.6290776997575, 82.25749423519197, 81.87712508822979, 81.49675594126761, 81.09931880902852, 80.6998987769223, 80.38219308946239, 80.06394742324582, 79.70348526693819, 79.58169647523991, 79.7679112, 79.7679112])\n", 66 | " lon = np.array([49.7934288, 49.86079434688794, 49.92815989377588, 49.99699057427323, 50.07119172258097, 50.14539287088871, 50.21959401919645, 50.2937951675042, 50.367996315811936, 50.44219746411967, 50.59280312395832, 50.792436728275504, 50.99271393197662, 51.19459015098207, 51.390376419073085, 51.60421432299298, 51.746632193203595, 51.87040340164587, 51.99417461008816, 52.02591753929958, 52.04744979306631, 51.826426464592416, 51.58531405704919, 51.43357869745786, 51.39167315475912, 51.3637112, 51.3637112])\n", 67 | "\n", 68 | " coor = np.vstack((lon, lat))\n", 69 | " coor_t = coor.T\n", 70 | "\n", 71 | " ln = LineString(coor_t)\n", 72 | "\n", 73 | " ln_wkt = ln.wkt\n", 74 | "\n", 75 | " return(ln_wkt)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": { 82 | "collapsed": true 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "res01 = runTest01()\n", 87 | "res02 = runTest02()" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 5, 93 | "metadata": { 94 | "collapsed": false 95 | }, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "Test01 array: \n", 102 | "[[ 178044.50503437 4080523.32981373]\n", 103 | " [ 185051.20277019 4115968.97717771]\n", 104 | " [ 218436.05668683 4125036.46836944]\n", 105 | " [ 231625.13477671 4158833.48097265]\n", 106 | " [ 229564.34132516 4174495.51120244]\n", 107 | " [ 216787.42192546 4189333.22405021]\n", 108 | " [ 212253.67633192 4213238.42808471]\n", 109 | " [ 216787.42192539 4231373.41045699]]\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "print(\"Test01 array: \\n{}\".format(res01[0]))" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 6, 120 | "metadata": { 121 | "collapsed": false 122 | }, 123 | "outputs": [ 124 | { 125 | "name": "stdout", 126 | "output_type": "stream", 127 | "text": [ 128 | "Test01 buff wkt: \n", 129 | "POLYGON ((165430.8635830167 4119847.416319962, 165921.6067607095 4121804.94809213, 166607.1255483623 4123703.058599259, 167480.4400630784 4125522.421472323, 168532.6583048801 4127244.512142734, 169753.066694152 4128851.796457624, 171129.2391562242 4130327.909210815, 172647.1636424063 4131657.820771689, 174291.3847992491 4132827.990115369, 176045.1613333884 4133826.502696061, 177890.6364696993 4134643.191759754, 179809.0197671729 4135269.74186108, 203468.2671049733 4141695.710277001, 211125.8141482724 4161318.174574235, 210513.957774473 4165968.283014506, 201632.0458799098 4176282.76134125, 200351.1938988329 4177938.031719375, 199250.3392348049 4179718.093749422, 198341.53762748 4181603.453518976, 197634.7415946002 4183573.463973271, 197137.6914394404 4185606.551026347, 192603.9458458964 4209511.755060846, 192316.5713361304 4211653.548156443, 192261.9556375852 4213813.844177521, 192440.736369668 4215967.42241456, 192850.8263291123 4218089.140585848, 197384.5719225913 4236224.122958128, 197953.4549166156 4238102.577323119, 198703.7193086281 4239916.226132652, 199628.1396423975 4241647.602957251, 200717.81324477 4243280.033691626, 201962.2459632786 4244797.79713543, 203349.453230568 4246186.276397005, 204866.0754823294 4247432.09966204, 206497.5068172085 4248523.268971459, 208228.0356596191 4249449.275768317, 210040.9960708057 4250201.202100954, 211918.9282509435 4250771.806507742, 213843.7466865555 4251155.593756319, 215796.9143238942 4251348.867765682, 217759.6210909019 4251349.767201466, 219712.9650484876 4251158.283401614, 221638.1344265334 4250776.260459794, 223516.5887915241 4250207.37746577, 225330.2376010571 4249457.113073757, 227061.6144256555 4248532.692739988, 228694.0451600314 4247443.019137615, 230211.8086038354 4246198.586419106, 231600.2878654097 4244811.379151817, 232846.1111304456 4243294.756900055, 233937.2804398643 4241663.325565176, 234863.2872367223 4239932.796722766, 235615.2135693592 4238119.83631158, 236185.8179761468 4236241.904131441, 236569.6052247238 4234317.085695829, 236762.8792340868 4232363.91805849, 236763.7786698714 4230401.211291483, 236572.2948700193 4228447.867333897, 236190.2719281987 4226522.697955851, 232721.9215191716 4212649.296320994, 235438.830068518 4198323.778517858, 244719.7173707063 4187545.9739114, 245908.7016030077 4186022.084288478, 246945.0326530277 4184390.538689276, 247819.0313880259 4182666.575435976, 248522.5348278711 4180866.296013875, 249048.9723857055 4179006.514686618, 249393.427235963 4177104.601454201, 251454.220687519 4161442.571224411, 251615.9858384124 4159438.356024965, 251575.6911367771 4157428.026980566, 251333.7438659973 4155431.90372588, 250892.5895362567 4153470.162308903, 250256.68716626 4151562.631259203, 237067.609076382 4117765.618655994, 236308.7205333188 4116060.506747945, 235394.1915413952 4114433.560171473, 234331.9860898644 4112898.946859104, 233131.3541783169 4111470.03067796, 231802.7512649446 4110159.255053247, 230357.7472173158 4108978.034607226, 228808.9255585422 4107936.655757299, 227169.7738862334 4107044.187138842, 225454.5664185037 4106308.400632846, 223678.2396898541 4105735.70368607, 202265.7300857639 4099919.960333928, 197664.8442215433 4076644.890671478, 197190.2134119071 4074740.43687661, 196531.1989644863 4072891.675501978, 195694.1475486635 4071116.411130254, 194687.1204271521 4069431.740527797, 193519.8158216292 4067853.887993443, 192203.4755135595 4066398.049109639, 190750.7765796946 4065078.244400681, 189175.7093048937 4063907.184307392, 187493.4424480323 4062896.146778638, 185720.1771585628 4062054.868658511, 183872.990950588 4061391.451915196, 181969.6732370655 4060912.285614592, 180028.5540080358 4060621.984390114, 178068.3273027983 4060523.344001247, 176107.8711760931 4060617.314408858, 174166.0658921149 4060902.990626554, 172261.612097247 4061377.62143619, 170412.8507226145 4062036.635883611, 168637.5863508907 4062873.687299433, 166952.9157484337 4063880.714420945, 165375.0632140797 4065048.019026468, 163919.2243302764 4066364.359334537, 162599.4196213174 4067817.058268402, 161428.3595280286 4069392.125543203, 160417.321999275 4071074.392400065, 159576.0438791477 4072847.657689534, 158912.627135833 4074694.843897509, 158433.4608352295 4076598.161611031, 158143.1596107509 4078539.280840061, 158044.5192218838 4080499.507545299, 158138.4896294954 4082459.963672004, 158424.1658471907 4084401.768955982, 165430.8635830167 4119847.416319962))\n" 130 | ] 131 | } 132 | ], 133 | "source": [ 134 | "print(\"Test01 buff wkt: \\n{}\".format(res01[1]))" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 7, 140 | "metadata": { 141 | "collapsed": false 142 | }, 143 | "outputs": [ 144 | { 145 | "name": "stdout", 146 | "output_type": "stream", 147 | "text": [ 148 | "Test02 wkt: \n", 149 | "LINESTRING (49.7934288 88.2598888, 49.86079434688794 87.8656022505306, 49.92815989377588 87.47131570106119, 49.99699057427323 87.07729258677767, 50.07119172258097 86.68423509786567, 50.14539287088871 86.29117760895366, 50.21959401919645 85.89812012004164, 50.2937951675042 85.50506263112963, 50.36799631581194 85.11200514221763, 50.44219746411967 84.71894765330562, 50.59280312395832 84.35408870391326, 50.7924367282755 84.00746812090851, 50.99271393197662 83.66121815097686, 51.19459015098207 83.31589924664645, 51.39037641907309 82.96712093784359, 51.60421432299298 82.6290776997575, 51.7466321932036 82.25749423519197, 51.87040340164587 81.87712508822979, 51.99417461008816 81.49675594126761, 52.02591753929958 81.09931880902852, 52.04744979306631 80.6998987769223, 51.82642646459242 80.38219308946239, 51.58531405704919 80.06394742324582, 51.43357869745786 79.70348526693819, 51.39167315475912 79.58169647523991, 51.3637112 79.7679112, 51.3637112 79.7679112)\n" 150 | ] 151 | } 152 | ], 153 | "source": [ 154 | "print(\"Test02 wkt: \\n{}\".format(res02))" 155 | ] 156 | } 157 | ], 158 | "metadata": { 159 | "kernelspec": { 160 | "display_name": "Python 3", 161 | "language": "python", 162 | "name": "python3" 163 | }, 164 | "language_info": { 165 | "codemirror_mode": { 166 | "name": "ipython", 167 | "version": 3 168 | }, 169 | "file_extension": ".py", 170 | "mimetype": "text/x-python", 171 | "name": "python", 172 | "nbconvert_exporter": "python", 173 | "pygments_lexer": "ipython3", 174 | "version": "3.4.3" 175 | } 176 | }, 177 | "nbformat": 4, 178 | "nbformat_minor": 0 179 | } 180 | -------------------------------------------------------------------------------- /notebooks/unit06/unit06_05.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Map Algebra with GDAL and NumPy" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 60, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "from osgeo import gdal" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 61, 31 | "metadata": { 32 | "collapsed": true 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "src_file01 = '../../data/temp_gfs/tmp_2m_k_20150903.tif'\n", 37 | "src_file02 = '../../data/temp_gfs/tmp_80m_k_20150903.tif'" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "Read datasets:" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 62, 50 | "metadata": { 51 | "collapsed": true 52 | }, 53 | "outputs": [], 54 | "source": [ 55 | "src_ds01 = gdal.Open(src_file01)\n", 56 | "src_ds02 = gdal.Open(src_file02)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 63, 62 | "metadata": { 63 | "collapsed": true 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "src01_band = src_ds01.GetRasterBand(1)\n", 68 | "src02_band = src_ds02.GetRasterBand(1)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "Read bands as NumPy arrays:" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 64, 81 | "metadata": { 82 | "collapsed": true 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "src01_ar = src01_band.ReadAsArray()\n", 87 | "src02_ar = src02_band.ReadAsArray()" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 65, 93 | "metadata": { 94 | "collapsed": true 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "src_ds01 = None\n", 99 | "src_ds02 = None" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 66, 105 | "metadata": { 106 | "collapsed": false 107 | }, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "numpy.ndarray" 113 | ] 114 | }, 115 | "execution_count": 66, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "type(src01_ar)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 67, 127 | "metadata": { 128 | "collapsed": false 129 | }, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/plain": [ 134 | "numpy.ndarray" 135 | ] 136 | }, 137 | "execution_count": 67, 138 | "metadata": {}, 139 | "output_type": "execute_result" 140 | } 141 | ], 142 | "source": [ 143 | "type(src02_ar)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 68, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "data": { 155 | "text/plain": [ 156 | "array([[ 270.55001831, 270.55001831, 270.55001831, ..., 270.55001831,\n", 157 | " 270.55001831, 270.55001831],\n", 158 | " [ 270.55001831, 270.55001831, 270.55001831, ..., 270.55001831,\n", 159 | " 270.55001831, 270.55001831],\n", 160 | " [ 270.55001831, 270.55001831, 270.55001831, ..., 270.55001831,\n", 161 | " 270.55001831, 270.55001831],\n", 162 | " ..., \n", 163 | " [ 218.55000305, 218.55000305, 218.55000305, ..., 218.55000305,\n", 164 | " 218.55000305, 218.55000305],\n", 165 | " [ 219.3500061 , 219.3500061 , 219.3500061 , ..., 219.3500061 ,\n", 166 | " 219.3500061 , 219.3500061 ],\n", 167 | " [ 219.75001526, 219.75001526, 219.75001526, ..., 219.75001526,\n", 168 | " 219.75001526, 219.75001526]], dtype=float32)" 169 | ] 170 | }, 171 | "execution_count": 68, 172 | "metadata": {}, 173 | "output_type": "execute_result" 174 | } 175 | ], 176 | "source": [ 177 | "src01_ar" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 69, 183 | "metadata": { 184 | "collapsed": false 185 | }, 186 | "outputs": [ 187 | { 188 | "data": { 189 | "text/plain": [ 190 | "array([[ 269.8500061 , 269.8500061 , 269.8500061 , ..., 269.8500061 ,\n", 191 | " 269.8500061 , 269.8500061 ],\n", 192 | " [ 269.95001221, 269.95001221, 269.95001221, ..., 269.95001221,\n", 193 | " 269.95001221, 269.95001221],\n", 194 | " [ 269.95001221, 269.95001221, 270.05001831, ..., 269.95001221,\n", 195 | " 269.95001221, 269.95001221],\n", 196 | " ..., \n", 197 | " [ 218.25001526, 218.25001526, 218.25001526, ..., 218.25001526,\n", 198 | " 218.25001526, 218.25001526],\n", 199 | " [ 218.95001221, 218.95001221, 218.95001221, ..., 218.95001221,\n", 200 | " 218.95001221, 218.95001221],\n", 201 | " [ 219.45001221, 219.45001221, 219.45001221, ..., 219.45001221,\n", 202 | " 219.45001221, 219.45001221]], dtype=float32)" 203 | ] 204 | }, 205 | "execution_count": 69, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "src02_ar" 212 | ] 213 | }, 214 | { 215 | "cell_type": "markdown", 216 | "metadata": {}, 217 | "source": [ 218 | "Operating with bands as NumPy arrays:" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 70, 224 | "metadata": { 225 | "collapsed": true 226 | }, 227 | "outputs": [], 228 | "source": [ 229 | "res = (src01_ar -273.15) - (src02_ar -273.15)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 71, 235 | "metadata": { 236 | "collapsed": false 237 | }, 238 | "outputs": [ 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "numpy.ndarray" 243 | ] 244 | }, 245 | "execution_count": 71, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "type(res)" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 72, 257 | "metadata": { 258 | "collapsed": false 259 | }, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "array([[ 0.70001221, 0.70001221, 0.70001221, ..., 0.70001221,\n", 265 | " 0.70001221, 0.70001221],\n", 266 | " [ 0.6000061 , 0.6000061 , 0.6000061 , ..., 0.6000061 ,\n", 267 | " 0.6000061 , 0.6000061 ],\n", 268 | " [ 0.6000061 , 0.6000061 , 0.5 , ..., 0.6000061 ,\n", 269 | " 0.6000061 , 0.6000061 ],\n", 270 | " ..., \n", 271 | " [ 0.29998779, 0.29998779, 0.29998779, ..., 0.29998779,\n", 272 | " 0.29998779, 0.29998779],\n", 273 | " [ 0.3999939 , 0.3999939 , 0.3999939 , ..., 0.3999939 ,\n", 274 | " 0.3999939 , 0.3999939 ],\n", 275 | " [ 0.30000305, 0.30000305, 0.30000305, ..., 0.30000305,\n", 276 | " 0.30000305, 0.30000305]], dtype=float32)" 277 | ] 278 | }, 279 | "execution_count": 72, 280 | "metadata": {}, 281 | "output_type": "execute_result" 282 | } 283 | ], 284 | "source": [ 285 | "res" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "metadata": {}, 291 | "source": [ 292 | "Store NumPy array result on a new raster dataset:" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 73, 298 | "metadata": { 299 | "collapsed": true 300 | }, 301 | "outputs": [], 302 | "source": [ 303 | "dst_file = '/tmp/out_mapalgebra_gdal.tif'\n", 304 | "\n", 305 | "drv_gtif = gdal.GetDriverByName(\"GTiff\")\n", 306 | "src_ds = gdal.Open(src_file01)" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 74, 312 | "metadata": { 313 | "collapsed": false 314 | }, 315 | "outputs": [ 316 | { 317 | "name": "stdout", 318 | "output_type": "stream", 319 | "text": [ 320 | "(-180.0, 0.25, 0.0, 90.0, 0.0, -0.2498266296809986)\n", 321 | "1440 721\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "src_geot = src_ds.GetGeoTransform()\n", 327 | "\n", 328 | "x_sz = src_ds.RasterXSize\n", 329 | "y_sz = src_ds.RasterYSize\n", 330 | "\n", 331 | "print(src_geot)\n", 332 | "print(x_sz, y_sz)" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": 75, 338 | "metadata": { 339 | "collapsed": false 340 | }, 341 | "outputs": [ 342 | { 343 | "data": { 344 | "text/plain": [ 345 | "0" 346 | ] 347 | }, 348 | "execution_count": 75, 349 | "metadata": {}, 350 | "output_type": "execute_result" 351 | } 352 | ], 353 | "source": [ 354 | "datatype = gdal.GDT_Float32\n", 355 | "n_band = 1\n", 356 | "nodata_val = -99999\n", 357 | "\n", 358 | "dst_raster = drv_gtif.Create(dst_file, x_sz, y_sz, n_band, datatype)\n", 359 | "dst_raster.SetGeoTransform(src_geot)" 360 | ] 361 | }, 362 | { 363 | "cell_type": "markdown", 364 | "metadata": {}, 365 | "source": [ 366 | "Write NumPy results" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 76, 372 | "metadata": { 373 | "collapsed": false 374 | }, 375 | "outputs": [ 376 | { 377 | "data": { 378 | "text/plain": [ 379 | "0" 380 | ] 381 | }, 382 | "execution_count": 76, 383 | "metadata": {}, 384 | "output_type": "execute_result" 385 | } 386 | ], 387 | "source": [ 388 | "out_band = dst_raster.GetRasterBand(n_band)\n", 389 | "out_band.SetNoDataValue(nodata_val)\n", 390 | "out_band.WriteArray(res)" 391 | ] 392 | }, 393 | { 394 | "cell_type": "markdown", 395 | "metadata": {}, 396 | "source": [ 397 | "Close datasets:" 398 | ] 399 | }, 400 | { 401 | "cell_type": "code", 402 | "execution_count": 77, 403 | "metadata": { 404 | "collapsed": true 405 | }, 406 | "outputs": [], 407 | "source": [ 408 | "src_ds = None\n", 409 | "dst_raster = None" 410 | ] 411 | } 412 | ], 413 | "metadata": { 414 | "kernelspec": { 415 | "display_name": "Python 3", 416 | "language": "python", 417 | "name": "python3" 418 | }, 419 | "language_info": { 420 | "codemirror_mode": { 421 | "name": "ipython", 422 | "version": 3 423 | }, 424 | "file_extension": ".py", 425 | "mimetype": "text/x-python", 426 | "name": "python", 427 | "nbconvert_exporter": "python", 428 | "pygments_lexer": "ipython3", 429 | "version": "3.4.3" 430 | } 431 | }, 432 | "nbformat": 4, 433 | "nbformat_minor": 0 434 | } 435 | -------------------------------------------------------------------------------- /notebooks/unit06/unit06_06.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "This notebook was prepared by Cayetano Benavent, 2016." 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "# Map algebra with Rasterio and NumPy" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 1, 22 | "metadata": { 23 | "collapsed": true 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "import rasterio" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 1, 33 | "metadata": { 34 | "collapsed": true 35 | }, 36 | "outputs": [], 37 | "source": [ 38 | "src_file01 = '../../data/temp_gfs/tmp_2m_k_20150903.tif'\n", 39 | "src_file02 = '../../data/temp_gfs/tmp_80m_k_20150903.tif'\n", 40 | "dst_file = '/tmp/out_mapalgebra_rst.tif'" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "Explore input datasets:" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 16, 53 | "metadata": { 54 | "collapsed": false 55 | }, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "1 204.15 314.55\n", 62 | "1 216.35 312.85\n" 63 | ] 64 | } 65 | ], 66 | "source": [ 67 | "with rasterio.open(src_file01) as src:\n", 68 | " for i in src.indexes:\n", 69 | " band = src.read(i)\n", 70 | " print(i, band.min(), band.max())\n", 71 | "\n", 72 | "with rasterio.open(src_file02) as src:\n", 73 | " for i in src.indexes:\n", 74 | " band = src.read(i)\n", 75 | " print(i, band.min(), band.max())" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": {}, 81 | "source": [ 82 | "Read bands as NumPy arrays:" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 4, 88 | "metadata": { 89 | "collapsed": false 90 | }, 91 | "outputs": [ 92 | { 93 | "name": "stdout", 94 | "output_type": "stream", 95 | "text": [ 96 | "\n", 97 | "(721, 1440) 2 1038240\n", 98 | "[[ 270.55001831 270.55001831 270.55001831 ..., 270.55001831\n", 99 | " 270.55001831 270.55001831]\n", 100 | " [ 270.55001831 270.55001831 270.55001831 ..., 270.55001831\n", 101 | " 270.55001831 270.55001831]\n", 102 | " [ 270.55001831 270.55001831 270.55001831 ..., 270.55001831\n", 103 | " 270.55001831 270.55001831]\n", 104 | " ..., \n", 105 | " [ 218.55000305 218.55000305 218.55000305 ..., 218.55000305\n", 106 | " 218.55000305 218.55000305]\n", 107 | " [ 219.3500061 219.3500061 219.3500061 ..., 219.3500061 219.3500061\n", 108 | " 219.3500061 ]\n", 109 | " [ 219.75001526 219.75001526 219.75001526 ..., 219.75001526\n", 110 | " 219.75001526 219.75001526]]\n" 111 | ] 112 | } 113 | ], 114 | "source": [ 115 | "with rasterio.open(src_file01) as src:\n", 116 | " for i in src.indexes:\n", 117 | " band = src.read(i)\n", 118 | " print(type(band))\n", 119 | " print(band.shape, band.ndim, band.size)\n", 120 | " print(band)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 15, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "[[ 270.55001831 270.55001831 270.55001831 ..., 270.55001831\n", 135 | " 270.55001831 270.55001831]\n", 136 | " [ 270.55001831 270.55001831 270.55001831 ..., 270.55001831\n", 137 | " 270.55001831 270.55001831]\n", 138 | " [ 270.55001831 270.55001831 270.55001831 ..., 270.55001831\n", 139 | " 270.55001831 270.55001831]\n", 140 | " ..., \n", 141 | " [ 218.55000305 218.55000305 218.55000305 ..., 218.55000305\n", 142 | " 218.55000305 218.55000305]\n", 143 | " [ 219.3500061 219.3500061 219.3500061 ..., 219.3500061 219.3500061\n", 144 | " 219.3500061 ]\n", 145 | " [ 219.75001526 219.75001526 219.75001526 ..., 219.75001526\n", 146 | " 219.75001526 219.75001526]]\n", 147 | "\n", 148 | "[[ 269.8500061 269.8500061 269.8500061 ..., 269.8500061 269.8500061\n", 149 | " 269.8500061 ]\n", 150 | " [ 269.95001221 269.95001221 269.95001221 ..., 269.95001221\n", 151 | " 269.95001221 269.95001221]\n", 152 | " [ 269.95001221 269.95001221 270.05001831 ..., 269.95001221\n", 153 | " 269.95001221 269.95001221]\n", 154 | " ..., \n", 155 | " [ 218.25001526 218.25001526 218.25001526 ..., 218.25001526\n", 156 | " 218.25001526 218.25001526]\n", 157 | " [ 218.95001221 218.95001221 218.95001221 ..., 218.95001221\n", 158 | " 218.95001221 218.95001221]\n", 159 | " [ 219.45001221 219.45001221 219.45001221 ..., 219.45001221\n", 160 | " 219.45001221 219.45001221]]\n", 161 | "\n" 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "with rasterio.open(src_file01) as src01:\n", 167 | " with rasterio.open(src_file02) as src02:\n", 168 | " for i in src01.indexes:\n", 169 | " band_src01 = src01.read(i)\n", 170 | " print(band_src01)\n", 171 | " print(type(band_src01))\n", 172 | " for i in src02.indexes:\n", 173 | " band_src02 = src02.read(i)\n", 174 | " print(band_src02)\n", 175 | " print(type(band_src01))" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "Operating with bands as NumPy arrays:" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": 6, 188 | "metadata": { 189 | "collapsed": false 190 | }, 191 | "outputs": [ 192 | { 193 | "name": "stdout", 194 | "output_type": "stream", 195 | "text": [ 196 | "[[ 0.70001221 0.70001221 0.70001221 ..., 0.70001221 0.70001221\n", 197 | " 0.70001221]\n", 198 | " [ 0.6000061 0.6000061 0.6000061 ..., 0.6000061 0.6000061\n", 199 | " 0.6000061 ]\n", 200 | " [ 0.6000061 0.6000061 0.5 ..., 0.6000061 0.6000061\n", 201 | " 0.6000061 ]\n", 202 | " ..., \n", 203 | " [ 0.29998779 0.29998779 0.29998779 ..., 0.29998779 0.29998779\n", 204 | " 0.29998779]\n", 205 | " [ 0.3999939 0.3999939 0.3999939 ..., 0.3999939 0.3999939\n", 206 | " 0.3999939 ]\n", 207 | " [ 0.30000305 0.30000305 0.30000305 ..., 0.30000305 0.30000305\n", 208 | " 0.30000305]]\n" 209 | ] 210 | } 211 | ], 212 | "source": [ 213 | "with rasterio.open(src_file01) as src01:\n", 214 | " with rasterio.open(src_file02) as src02:\n", 215 | " for i in src01.indexes:\n", 216 | " band_src01 = src01.read(i)\n", 217 | " for i in src02.indexes:\n", 218 | " band_src02 = src02.read(i)\n", 219 | " res = (band_src01 -273.15) - (band_src02 -273.15)\n", 220 | " print(res)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "markdown", 225 | "metadata": {}, 226 | "source": [ 227 | "Computing with NumPy and storing results on a new raster dataset:" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 7, 233 | "metadata": { 234 | "collapsed": false 235 | }, 236 | "outputs": [ 237 | { 238 | "name": "stderr", 239 | "output_type": "stream", 240 | "text": [ 241 | "/usr/local/lib/python3.4/dist-packages/rasterio/__init__.py:99: FutureWarning: GDAL-style transforms are deprecated and will not be supported in Rasterio 1.0.\n", 242 | " transform = guard_transform(transform)\n" 243 | ] 244 | } 245 | ], 246 | "source": [ 247 | "with rasterio.open(src_file01) as src01:\n", 248 | " with rasterio.open(src_file02) as src02:\n", 249 | " for i in src01.indexes:\n", 250 | " band_src01 = src01.read(i)\n", 251 | " for i in src02.indexes:\n", 252 | " band_src02 = src02.read(i)\n", 253 | " res = (band_src01 -273.15) - (band_src02 -273.15)\n", 254 | " kwargs = src.meta\n", 255 | " with rasterio.open(dst_file, 'w', **kwargs) as dst:\n", 256 | " dst.write_band(1, res)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "Key 'transform' of kwargs dict is a GDAL-style transform (raise a warning):" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 10, 269 | "metadata": { 270 | "collapsed": false 271 | }, 272 | "outputs": [ 273 | { 274 | "name": "stdout", 275 | "output_type": "stream", 276 | "text": [ 277 | "{'transform': (-180.0, 0.25, 0.0, 90.0, 0.0, -0.2498266296809986), 'count': 1, 'crs': {'b': 6371229, 'a': 6371229, 'no_defs': True, 'proj': 'longlat', 'wktext': True}, 'affine': Affine(0.25, 0.0, -180.0,\n", 278 | " 0.0, -0.2498266296809986, 90.0), 'dtype': 'float32', 'driver': 'GTiff', 'width': 1440, 'nodata': -3.4028234663852886e+38, 'height': 721}\n" 279 | ] 280 | } 281 | ], 282 | "source": [ 283 | "print(kwargs)" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": {}, 289 | "source": [ 290 | "If we remove 'transform' key from kwargs dict, the warning disappear. " 291 | ] 292 | }, 293 | { 294 | "cell_type": "code", 295 | "execution_count": 13, 296 | "metadata": { 297 | "collapsed": true 298 | }, 299 | "outputs": [], 300 | "source": [ 301 | "with rasterio.open(src_file01) as src01:\n", 302 | " with rasterio.open(src_file02) as src02:\n", 303 | " for i in src01.indexes:\n", 304 | " band_src01 = src01.read(i)\n", 305 | " for i in src02.indexes:\n", 306 | " band_src02 = src02.read(i)\n", 307 | " res = (band_src01 -273.15) - (band_src02 -273.15)\n", 308 | " \n", 309 | " kwargs = src.meta\n", 310 | " if kwargs.get('transform'):\n", 311 | " kwargs.pop('transform')\n", 312 | " \n", 313 | " with rasterio.open(dst_file, 'w', **kwargs) as dst:\n", 314 | " dst.write_band(1, res)" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": 14, 320 | "metadata": { 321 | "collapsed": false 322 | }, 323 | "outputs": [ 324 | { 325 | "name": "stdout", 326 | "output_type": "stream", 327 | "text": [ 328 | "{'count': 1, 'crs': {'b': 6371229, 'a': 6371229, 'no_defs': True, 'proj': 'longlat', 'wktext': True}, 'affine': Affine(0.25, 0.0, -180.0,\n", 329 | " 0.0, -0.2498266296809986, 90.0), 'dtype': 'float32', 'driver': 'GTiff', 'width': 1440, 'nodata': -3.4028234663852886e+38, 'height': 721}\n" 330 | ] 331 | } 332 | ], 333 | "source": [ 334 | "print(kwargs)" 335 | ] 336 | } 337 | ], 338 | "metadata": { 339 | "kernelspec": { 340 | "display_name": "Python 3", 341 | "language": "python", 342 | "name": "python3" 343 | }, 344 | "language_info": { 345 | "codemirror_mode": { 346 | "name": "ipython", 347 | "version": 3 348 | }, 349 | "file_extension": ".py", 350 | "mimetype": "text/x-python", 351 | "name": "python", 352 | "nbconvert_exporter": "python", 353 | "pygments_lexer": "ipython3", 354 | "version": "3.4.3" 355 | } 356 | }, 357 | "nbformat": 4, 358 | "nbformat_minor": 0 359 | } 360 | -------------------------------------------------------------------------------- /notebooks/unit06/unit06_07.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This notebook was prepared by Cayetano Benavent, 2016." 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Reprojecting raster data with Rasterio" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 3, 20 | "metadata": { 21 | "collapsed": true 22 | }, 23 | "outputs": [], 24 | "source": [ 25 | "import rasterio\n", 26 | "from rasterio.warp import calculate_default_transform, reproject, RESAMPLING" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 4, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "my_file = '../../data/mde/h10_1050_2-2/h10_1050_2-2.tif'\n", 38 | "dst_file = '/tmp/mde_reprojected.tif'" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "Define destiny CRS:" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 5, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "dst_crs = 'EPSG:25829'" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": {}, 62 | "source": [ 63 | "Explore source dataset:" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 6, 69 | "metadata": { 70 | "collapsed": false 71 | }, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "{'affine': Affine(10.614757961540635, 0.0, 817664.7315271514,\n", 78 | " 0.0, -10.614757961540635, 4078270.070665097), 'transform': Affine(10.614757961540635, 0.0, 817664.7315271514,\n", 79 | " 0.0, -10.614757961540635, 4078270.070665097), 'width': 761, 'height': 520, 'count': 1, 'nodata': None, 'driver': 'GTiff', 'dtype': 'float32', 'crs': 'EPSG:25829'}\n", 80 | "{'height': 503, 'count': 1, 'nodata': None, 'affine': Affine(10.0, 0.0, 282285.0,\n", 81 | " 0.0, -10.0, 4074645.0), 'driver': 'GTiff', 'dtype': 'float32', 'transform': (282285.0, 10.0, 0.0, 4074645.0, 0.0, -10.0), 'width': 777, 'crs': {'init': 'epsg:3042'}}\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "with rasterio.open(my_file) as src:\n", 87 | "\n", 88 | " affine, width, height = calculate_default_transform(\n", 89 | " src.crs, dst_crs, src.width, src.height, *src.bounds)\n", 90 | " kwargs = src.meta.copy()\n", 91 | " kwargs.update({\n", 92 | " 'crs': dst_crs,\n", 93 | " 'transform': affine,\n", 94 | " 'affine': affine,\n", 95 | " 'width': width,\n", 96 | " 'height': height\n", 97 | " })\n", 98 | " print(kwargs)\n", 99 | " print(src.meta)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": {}, 105 | "source": [ 106 | "Run reprojection:" 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 7, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [], 116 | "source": [ 117 | "with rasterio.open(my_file) as src:\n", 118 | "\n", 119 | " affine, width, height = calculate_default_transform(\n", 120 | " src.crs, dst_crs, src.width, src.height,*src.bounds)\n", 121 | " kwargs = src.meta.copy()\n", 122 | " kwargs.update({\n", 123 | " 'crs': dst_crs,\n", 124 | " 'transform': affine,\n", 125 | " 'affine': affine,\n", 126 | " 'width': width,\n", 127 | " 'height': height\n", 128 | " })\n", 129 | "\n", 130 | " with rasterio.open(dst_file, 'w', **kwargs) as dst:\n", 131 | " reproject(\n", 132 | " source=rasterio.band(src, 1),\n", 133 | " destination=rasterio.band(dst, 1),\n", 134 | " src_transform=src.affine,\n", 135 | " src_crs=src.crs,\n", 136 | " dst_transform=affine,\n", 137 | " dst_crs=dst_crs,\n", 138 | " resampling=RESAMPLING.nearest)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "Explore new dataset:" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 8, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "{'affine': Affine(10.61489795756186, 0.0, 817664.7315271514,\n", 160 | " 0.0, -10.61489795756186, 4078270.25), 'transform': Affine(10.61489795756186, 0.0, 817664.7315271514,\n", 161 | " 0.0, -10.61489795756186, 4078270.25), 'width': 761, 'height': 521, 'count': 1, 'nodata': None, 'driver': 'GTiff', 'dtype': 'float32', 'crs': 'EPSG:25829'}\n", 162 | "{'height': 520, 'count': 1, 'nodata': None, 'affine': Affine(10.614757961540635, 0.0, 817664.7315271514,\n", 163 | " 0.0, -10.614757961540635, 4078270.070665097), 'driver': 'GTiff', 'dtype': 'float32', 'transform': (817664.7315271514, 10.614757961540635, 0.0, 4078270.070665097, 0.0, -10.614757961540635), 'width': 761, 'crs': {'init': 'epsg:25829'}}\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "with rasterio.open(dst_file) as src:\n", 169 | "\n", 170 | " affine, width, height = calculate_default_transform(\n", 171 | " src.crs, dst_crs, src.width, src.height, *src.bounds)\n", 172 | " kwargs = src.meta.copy()\n", 173 | " kwargs.update({\n", 174 | " 'crs': dst_crs,\n", 175 | " 'transform': affine,\n", 176 | " 'affine': affine,\n", 177 | " 'width': width,\n", 178 | " 'height': height\n", 179 | " })\n", 180 | " print(kwargs)\n", 181 | " print(src.meta)" 182 | ] 183 | } 184 | ], 185 | "metadata": { 186 | "kernelspec": { 187 | "display_name": "Python 3", 188 | "language": "python", 189 | "name": "python3" 190 | }, 191 | "language_info": { 192 | "codemirror_mode": { 193 | "name": "ipython", 194 | "version": 3 195 | }, 196 | "file_extension": ".py", 197 | "mimetype": "text/x-python", 198 | "name": "python", 199 | "nbconvert_exporter": "python", 200 | "pygments_lexer": "ipython3", 201 | "version": "3.4.3" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 0 206 | } 207 | -------------------------------------------------------------------------------- /notebooks/unit07/img/bbox_builder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit07/img/bbox_builder.png -------------------------------------------------------------------------------- /notebooks/unit07/img/qgis_shell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit07/img/qgis_shell.png -------------------------------------------------------------------------------- /notebooks/unit07/img/qgis_shell_editor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeographicaGS/geopython-lessons/d7b5c058c6619861e1adfe8715dc7f4ca880566b/notebooks/unit07/img/qgis_shell_editor.png -------------------------------------------------------------------------------- /notebooks/unit07/unit07_01.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# PyQGIS" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## PyQGIS example 1: interactive coding in Python QGIS shell" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "First, we create a layer and add to map view: a bounding box polygon layer from a given coordinates - upper right and lower left corners.\n", 22 | "\n", 23 | "\n", 24 | "__All the code in this example must be executed from Python QGIS shell.__\n", 25 | "\n", 26 | "![QGIS Python shell](img/qgis_shell.png)\n", 27 | "\n", 28 | "Before create bounding box layer we are going to load a vector layer as basemap:\n", 29 | "\n", 30 | "```python\n", 31 | ">>> bslyer_filepath = \"../../data/ne_110m_coastline/ne_110m_coastline.shp\"\n", 32 | ">>> bs_ly = iface.addVectorLayer(bslyer_filepath, \"world countries\", \"ogr\")\n", 33 | "```\n", 34 | "\n", 35 | "Now you can define the layer with QgsVectorLayer (a memory layer):\n", 36 | "\n", 37 | "```python\n", 38 | ">>> ly = QgsVectorLayer(\"Polygon?crs=epsg:4326&field=mytext:int(6)\", \"bbox_tmp\", \"memory\")\n", 39 | ">>> pr = ly.dataProvider()\n", 40 | "```\n", 41 | "\n", 42 | "Creating variables with bounding box corners:\n", 43 | "```python\n", 44 | ">>> llx_pt = -6\n", 45 | ">>> lly_pt = 35\n", 46 | ">>> urx_pt = 0\n", 47 | ">>> ury_pt = 40\n", 48 | "```\n", 49 | "\n", 50 | "Creating a feature with desired geometry:\n", 51 | "```python\n", 52 | ">>> ft = QgsFeature()\n", 53 | ">>> geom_poly = QgsGeometry.fromPolygon([[QgsPoint(llx_pt, lly_pt), \n", 54 | " QgsPoint(llx_pt, ury_pt), \n", 55 | " QgsPoint(urx_pt, ury_pt), \n", 56 | " QgsPoint(urx_pt, lly_pt)]])\n", 57 | ">>> ft.setGeometry(geom_poly)\n", 58 | "```\n", 59 | "\n", 60 | "Adding attributes:\n", 61 | "```python\n", 62 | ">>> ft.setAttributes([1])\n", 63 | ">>> pr.addFeatures([ft])\n", 64 | ">>> ly.updateExtents()\n", 65 | "```\n", 66 | "\n", 67 | "Add new layer to map view:\n", 68 | "```python\n", 69 | ">>> QgsMapLayerRegistry.instance().addMapLayer(ly)\n", 70 | "```" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": null, 76 | "metadata": { 77 | "collapsed": true 78 | }, 79 | "outputs": [], 80 | "source": [] 81 | } 82 | ], 83 | "metadata": { 84 | "kernelspec": { 85 | "display_name": "Python 2", 86 | "language": "python", 87 | "name": "python2" 88 | }, 89 | "language_info": { 90 | "codemirror_mode": { 91 | "name": "ipython", 92 | "version": 2 93 | }, 94 | "file_extension": ".py", 95 | "mimetype": "text/x-python", 96 | "name": "python", 97 | "nbconvert_exporter": "python", 98 | "pygments_lexer": "ipython2", 99 | "version": "2.7.9" 100 | } 101 | }, 102 | "nbformat": 4, 103 | "nbformat_minor": 0 104 | } 105 | -------------------------------------------------------------------------------- /notebooks/unit07/unit07_02.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# PyQGIS" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## PyQGIS example 2: functions in Python QGIS shell" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "Same example as in \"unit07_01\" but now like a function:\n", 22 | "\n", 23 | "\n", 24 | "```python\n", 25 | "def bboxBuild(llx_pt, lly_pt, urx_pt, ury_pt, lyr_name, epsg=4326):\n", 26 | " \"\"\"\n", 27 | " Building a bounding box polygon layer \n", 28 | " urx_pt, ury_pt: upper right corners\n", 29 | " llx_pt, lly_pt: lower left corners\n", 30 | " \"\"\"\n", 31 | " try:\n", 32 | " fields_def=\"field=llx:double&field=lly:double&field=urx:double&field=ury:double\"\n", 33 | " uri_lyr = \"Polygon?crs=epsg:{0}&{1}\".format(epsg, fields_def)\n", 34 | " \n", 35 | " lyr = QgsVectorLayer(uri_lyr, lyr_name, \"memory\")\n", 36 | " prv = lyr.dataProvider()\n", 37 | "\n", 38 | " ft = QgsFeature()\n", 39 | " ft_gm = [[QgsPoint(llx_pt, lly_pt), \n", 40 | " QgsPoint(llx_pt, ury_pt), \n", 41 | " QgsPoint(urx_pt, ury_pt), \n", 42 | " QgsPoint(urx_pt, lly_pt)]]\n", 43 | "\n", 44 | " geom_poly = QgsGeometry.fromPolygon(ft_gm)\n", 45 | " ft.setGeometry(geom_poly)\n", 46 | "\n", 47 | " ft.setAttributes([llx_pt, lly_pt, urx_pt, ury_pt])\n", 48 | " prv.addFeatures([ft])\n", 49 | " lyr.updateExtents()\n", 50 | "\n", 51 | " QgsMapLayerRegistry.instance().addMapLayer(lyr)\n", 52 | "\n", 53 | " except Exception as error:\n", 54 | " print(\"Error creating bbox: {0}\".format(error))\n", 55 | "```" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "Creating variables and executing function:\n", 63 | "\n", 64 | "```python\n", 65 | ">>> llx_pt = -6\n", 66 | ">>> lly_pt = 35\n", 67 | ">>> urx_pt = 0\n", 68 | ">>> ury_pt = 40\n", 69 | "\n", 70 | ">>> bboxBuild(llx_pt, lly_pt, urx_pt, ury_pt, \"my_bbox\")\n", 71 | "```\n", 72 | "\n", 73 | "![QGIS Python shell editor](img/qgis_shell_editor.png)" 74 | ] 75 | } 76 | ], 77 | "metadata": { 78 | "kernelspec": { 79 | "display_name": "Python 2", 80 | "language": "python", 81 | "name": "python2" 82 | }, 83 | "language_info": { 84 | "codemirror_mode": { 85 | "name": "ipython", 86 | "version": 2 87 | }, 88 | "file_extension": ".py", 89 | "mimetype": "text/x-python", 90 | "name": "python", 91 | "nbconvert_exporter": "python", 92 | "pygments_lexer": "ipython2", 93 | "version": "2.7.6" 94 | } 95 | }, 96 | "nbformat": 4, 97 | "nbformat_minor": 0 98 | } 99 | -------------------------------------------------------------------------------- /notebooks/unit07/unit07_03.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# PyQGIS" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## PyQGIS example 3: Python in geoprocessing toolbox\n", 15 | "\n", 16 | "Same example as in \"unit07_02\" but now in geoprocessing toolbox:" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "\n", 24 | "```python\n", 25 | "##Polygons=group\n", 26 | "##BBox builder=name\n", 27 | "##llx_pt=number -6.0\n", 28 | "##lly_pt=number 35.0\n", 29 | "##urx_pt=number 0.0\n", 30 | "##ury_pt=number 40.0\n", 31 | "##lyr_name=string my_bbox\n", 32 | "\n", 33 | "from qgis.core import (QgsVectorLayer, QgsFeature, QgsPoint, \n", 34 | " QgsGeometry, QgsMapLayerRegistry)\n", 35 | "\n", 36 | "\n", 37 | "def bboxBuild(llx_pt, lly_pt, urx_pt, ury_pt, lyr_name, epsg=4326):\n", 38 | " \"\"\"\n", 39 | " Building a bounding box polygon layer \n", 40 | " urx_pt, ury_pt: upper right corners\n", 41 | " llx_pt, lly_pt: lower left corners\n", 42 | " \"\"\"\n", 43 | " try:\n", 44 | " progress.setText(\"BBox corners: {0},{1},{2},{3}\".format(llx_pt, lly_pt, urx_pt, ury_pt))\n", 45 | " \n", 46 | " fields_def = \"field=llx:double&field=lly:double&field=urx:double&field=ury:double\"\n", 47 | " uri_lyr = \"Polygon?crs=epsg:{0}&{1}\".format(epsg, fields_def)\n", 48 | "\n", 49 | " lyr = QgsVectorLayer(uri_lyr, lyr_name, \"memory\")\n", 50 | " prv = lyr.dataProvider()\n", 51 | "\n", 52 | " ft = QgsFeature()\n", 53 | " ft_gm = [[QgsPoint(llx_pt, lly_pt), \n", 54 | " QgsPoint(llx_pt, ury_pt), \n", 55 | " QgsPoint(urx_pt, ury_pt), \n", 56 | " QgsPoint(urx_pt, lly_pt)]]\n", 57 | "\n", 58 | " geom_poly = QgsGeometry.fromPolygon(ft_gm)\n", 59 | " ft.setGeometry(geom_poly)\n", 60 | "\n", 61 | " ft.setAttributes([llx_pt, lly_pt, urx_pt, ury_pt])\n", 62 | " prv.addFeatures([ft])\n", 63 | " lyr.updateExtents()\n", 64 | "\n", 65 | " QgsMapLayerRegistry.instance().addMapLayer(lyr)\n", 66 | " \n", 67 | " progress.setText(\"Successfully builded bounding box!!\")\n", 68 | "\n", 69 | " except Exception as error:\n", 70 | " progress.setText(\"Error creating bbox: {0}\".format(error))\n", 71 | "\n", 72 | "bboxBuild(llx_pt, lly_pt, urx_pt, ury_pt, lyr_name)\n", 73 | "```" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "Below a screenshot from BBox builder GUI:" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "![BBox builder](img/bbox_builder.png)" 88 | ] 89 | } 90 | ], 91 | "metadata": { 92 | "kernelspec": { 93 | "display_name": "Python 2", 94 | "language": "python", 95 | "name": "python2" 96 | }, 97 | "language_info": { 98 | "codemirror_mode": { 99 | "name": "ipython", 100 | "version": 2 101 | }, 102 | "file_extension": ".py", 103 | "mimetype": "text/x-python", 104 | "name": "python", 105 | "nbconvert_exporter": "python", 106 | "pygments_lexer": "ipython2", 107 | "version": "2.7.6" 108 | } 109 | }, 110 | "nbformat": 4, 111 | "nbformat_minor": 0 112 | } 113 | --------------------------------------------------------------------------------