-
Notifications
You must be signed in to change notification settings - Fork 17
/
python_gis_part1.html
1053 lines (1043 loc) · 88.8 KB
/
python_gis_part1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.7: http://docutils.sourceforge.net/" />
<meta name="version" content="S5 1.1" />
<title>Developing Geospatial software with Python</title>
<style type="text/css">
.highlight .hll { background-color: #333333 }
.highlight { background: #111111; color: #ffffff }
.highlight .c { color: #008800; font-style: italic; background-color: #0f140f } /* Comment */
.highlight .err { color: #ffffff } /* Error */
.highlight .g { color: #ffffff } /* Generic */
.highlight .k { color: #fb660a; font-weight: bold } /* Keyword */
.highlight .l { color: #ffffff } /* Literal */
.highlight .n { color: #ffffff } /* Name */
.highlight .o { color: #ffffff } /* Operator */
.highlight .x { color: #ffffff } /* Other */
.highlight .p { color: #ffffff } /* Punctuation */
.highlight .cm { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.Multiline */
.highlight .cp { color: #ff0007; font-weight: bold; font-style: italic; background-color: #0f140f } /* Comment.Preproc */
.highlight .c1 { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.Single */
.highlight .cs { color: #008800; font-style: italic; background-color: #0f140f } /* Comment.Special */
.highlight .gd { color: #ffffff } /* Generic.Deleted */
.highlight .ge { color: #ffffff } /* Generic.Emph */
.highlight .gr { color: #ffffff } /* Generic.Error */
.highlight .gh { color: #ffffff; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #ffffff } /* Generic.Inserted */
.highlight .go { color: #444444; background-color: #222222 } /* Generic.Output */
.highlight .gp { color: #ffffff } /* Generic.Prompt */
.highlight .gs { color: #ffffff } /* Generic.Strong */
.highlight .gu { color: #ffffff; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #ffffff } /* Generic.Traceback */
.highlight .kc { color: #fb660a; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #fb660a; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #fb660a; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #fb660a } /* Keyword.Pseudo */
.highlight .kr { color: #fb660a; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #cdcaa9; font-weight: bold } /* Keyword.Type */
.highlight .ld { color: #ffffff } /* Literal.Date */
.highlight .m { color: #0086f7; font-weight: bold } /* Literal.Number */
.highlight .s { color: #0086d2 } /* Literal.String */
.highlight .na { color: #ff0086; font-weight: bold } /* Name.Attribute */
.highlight .nb { color: #ffffff } /* Name.Builtin */
.highlight .nc { color: #ffffff } /* Name.Class */
.highlight .no { color: #0086d2 } /* Name.Constant */
.highlight .nd { color: #ffffff } /* Name.Decorator */
.highlight .ni { color: #ffffff } /* Name.Entity */
.highlight .ne { color: #ffffff } /* Name.Exception */
.highlight .nf { color: #ff0086; font-weight: bold } /* Name.Function */
.highlight .nl { color: #ffffff } /* Name.Label */
.highlight .nn { color: #ffffff } /* Name.Namespace */
.highlight .nx { color: #ffffff } /* Name.Other */
.highlight .py { color: #ffffff } /* Name.Property */
.highlight .nt { color: #fb660a; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #fb660a } /* Name.Variable */
.highlight .ow { color: #ffffff } /* Operator.Word */
.highlight .w { color: #888888 } /* Text.Whitespace */
.highlight .mf { color: #0086f7; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0086f7; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0086f7; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0086f7; font-weight: bold } /* Literal.Number.Oct */
.highlight .sb { color: #0086d2 } /* Literal.String.Backtick */
.highlight .sc { color: #0086d2 } /* Literal.String.Char */
.highlight .sd { color: #0086d2 } /* Literal.String.Doc */
.highlight .s2 { color: #0086d2 } /* Literal.String.Double */
.highlight .se { color: #0086d2 } /* Literal.String.Escape */
.highlight .sh { color: #0086d2 } /* Literal.String.Heredoc */
.highlight .si { color: #0086d2 } /* Literal.String.Interpol */
.highlight .sx { color: #0086d2 } /* Literal.String.Other */
.highlight .sr { color: #0086d2 } /* Literal.String.Regex */
.highlight .s1 { color: #0086d2 } /* Literal.String.Single */
.highlight .ss { color: #0086d2 } /* Literal.String.Symbol */
.highlight .bp { color: #ffffff } /* Name.Builtin.Pseudo */
.highlight .vc { color: #fb660a } /* Name.Variable.Class */
.highlight .vg { color: #fb660a } /* Name.Variable.Global */
.highlight .vi { color: #fb660a } /* Name.Variable.Instance */
.highlight .il { color: #0086f7; font-weight: bold } /* Literal.Number.Integer.Long */
</style>
<!-- configuration parameters -->
<meta name="defaultView" content="slideshow" />
<meta name="controlVis" content="hidden" />
<!-- style sheet links -->
<script src="ui/python/slides.js" type="text/javascript"></script>
<link rel="stylesheet" href="ui/python/slides.css"
type="text/css" media="projection" id="slideProj" />
<link rel="stylesheet" href="ui/python/outline.css"
type="text/css" media="screen" id="outlineStyle" />
<link rel="stylesheet" href="ui/python/print.css"
type="text/css" media="print" id="slidePrint" />
<link rel="stylesheet" href="ui/python/opera.css"
type="text/css" media="projection" id="operaFix" />
<style type="text/css">
#currentSlide {display: none;}
</style>
</head>
<body>
<div class="layout">
<div id="controls"></div>
<div id="currentSlide"></div>
<div id="header">
</div>
<div id="footer">
<h1>Developing Geospatial software with Python, Part 1</h1>
<h2>GFOSS Day, Foligno - 18/19 November 2010</h2>
</div>
</div>
<div class="presentation">
<div class="slide" id="slide0">
<h1 class="title">Developing Geospatial software with Python, Part 1</h1>
<h2 class="subtitle" id="alessandro-pasotti-apasotti-gmail-com-paolo-corti-pcorti-gmail-com">Alessandro Pasotti (<a class="reference external" href="mailto:apasotti@gmail.com">apasotti@gmail.com</a>), Paolo Corti (<a class="reference external" href="mailto:pcorti@gmail.com">pcorti@gmail.com</a>)</h2>
<p>License:
Creative Commons Attribution - Share Alike 3.0</p>
</div>
<div class="slide" id="summary">
<h1>Summary</h1>
<p><strong>Part 1 - Paolo Corti</strong></p>
<ul class="simple">
<li><strong>GDAL/OGR (Python bindings, GeoDjango)</strong></li>
<li><strong>GEOS (GeoDjango, Shapely)</strong></li>
<li><strong>PROJ.4 (Python bindings, GeoDjango)</strong></li>
<li><strong>GeoAlchemy</strong></li>
</ul>
<p>Part 2 - Alessandro Pasotti</p>
<ul class="simple">
<li>OWS (OWSLib, pyWPS)</li>
<li>WebServices (GeoPy, Mapnik, Mapscript)</li>
<li>Desktop (QGIS, GRASS)</li>
</ul>
</div>
<div class="slide" id="building-blocks">
<h1>Building blocks</h1>
<ul>
<li><dl class="first docutils">
<dt><strong>GDAL</strong> Geospatial Data Abstraction Library</dt>
<dd><ul class="first last simple">
<li><strong>OGR</strong> Simple Feature Library</li>
</ul>
</dd>
</dl>
</li>
<li><p class="first"><strong>GEOS</strong> Geometry Engine, Open Source</p>
</li>
<li><p class="first"><strong>PROJ.4</strong> Cartographic Projections Library</p>
</li>
</ul>
</div>
<div class="slide" id="building-blocks-gdal-ogr">
<h1>Building blocks: GDAL/OGR</h1>
<dl class="docutils">
<dt><strong>GDAL</strong> Geospatial Data Abstraction Library</dt>
<dd><strong>OGR</strong> Simple Feature Library</dd>
</dl>
<ul class="simple">
<li>basic library for almost all FOSS4G projects (and often proprietary projects)</li>
<li>library and utilities for reading/writing a plethora of GIS formats</li>
<li>raster/cover (GDAL) and vectorial (OGR)</li>
<li>OGR follows the OGC Simple feature access specifications</li>
<li>written mostly in C++ by Frank Warmerdam</li>
<li>license: <strong>X/MIT</strong></li>
</ul>
</div>
<div class="slide" id="gdal-ogr-large-diffusion">
<h1>GDAL/OGR: large diffusion</h1>
<blockquote>
Large list of sofware using GDAL, here only most important (full list @ GDAL website)</blockquote>
<ul class="simple">
<li><strong>FOSS4G</strong>: GRASS, GeoServer, gvSIG, MapServer, MapGuide, Orfeo Toolbox, OSSIM, QGIS, R</li>
<li><strong>proprietary</strong>: ArcGis, ERDAS, FME, Google Earth</li>
</ul>
</div>
<div class="slide" id="gdal-ogr-vector-formats">
<h1>GDAL/OGR: vector formats</h1>
<blockquote>
<p>Long list too, here only most important (full list @ gdal website)</p>
<p>OGR (Vector) (get the full list with <strong>ogrinfo --formats</strong>):</p>
</blockquote>
<ul class="simple">
<li><strong>FOSS4G</strong> (RW): PostGis, Spatialite, MySQL, csv, GeoJSON, GeoRSS, GML, GPSBabel, GPX, GRASS, KML, WFS</li>
<li><strong>proprietary</strong>: Shapefile, ArcInfo Coverage (R), ArcInfo .E00 (R), AutoCAD DXF, Esri PGDB (R), ArcSde (R), FMEObjects Gateway (R), MapInfo, Microstation DGN, Oracle Spatial, Microsoft MS Spatial</li>
</ul>
</div>
<div class="slide" id="gdal-ogr-raster-formats">
<h1>GDAL/OGR: raster formats</h1>
<blockquote>
GDAL (Raster) (get the full list with <strong>gdalinfo --formats</strong>):</blockquote>
<ul class="simple">
<li><strong>FOSS4G</strong> (RW): GRASS Rasters, WKTRaster, Rasterlite</li>
<li><strong>proprietary</strong>: ArcInfo ASCII Grid, ArcSde Raster (R), ERDAS (R), Oracle Spatial GeoRaster, Intergraph, TIFF/GeoTIFF (Adobe)</li>
</ul>
</div>
<div class="slide" id="gdal-ogr-bindings">
<h1>GDAL/OGR: bindings</h1>
<blockquote>
bindings (based on SWIG) provide the GDAL power to developers using other languages than C/C++</blockquote>
<ul class="simple">
<li><strong>Python</strong></li>
<li>Perl</li>
<li>VB6 (COM) - No SWIG</li>
<li>Ruby</li>
<li>Java</li>
<li>.Net (VB, C#, ...)</li>
<li>R</li>
</ul>
</div>
<div class="slide" id="gdal-ogr-utilities-raster">
<h1>GDAL/OGR: utilities (raster)</h1>
<blockquote>
The power of GDAL/OGR at your fingertips (mostly in c, cpp but some written in python)!</blockquote>
<ul class="simple">
<li>gdalinfo: info about a file</li>
<li>gdal_translate: copy a raster with control on output</li>
<li>gdal_rasterize: rasterize vectors</li>
<li>gdalwarp: warp an image into a new coordiante system</li>
<li>gdaltransform: transform coordinates</li>
<li>gdal_retile.py: build tiled pyramid levels</li>
<li>gdal_grid: create raster from scattered data</li>
<li>gdal_polygonize.py: generate polygons from raster</li>
</ul>
</div>
<div class="slide" id="gdal-ogr-utilities-vector">
<h1>GDAL/OGR: utilities (vector)</h1>
<ul class="simple">
<li>ogrinfo: lists information about an OGR supported data source</li>
<li>ogr2ogr: converts simple features data between file formats</li>
<li>ogrtindex: creates a tileindex</li>
</ul>
</div>
<div class="slide" id="building-blocks-geos">
<h1>Building blocks: GEOS</h1>
<ul class="simple">
<li>it is a C++ port of <strong>JTS</strong> (Java Topology Suite from Vivid Solutions)</li>
<li>originally started from Refractions for PostGIS</li>
<li>provides all the <strong>OGC Simple Feature Access</strong> implementations for SQL spatial predicate functions and spatial operators</li>
<li>license: <strong>LGPL</strong></li>
</ul>
</div>
<div class="slide" id="geos-geometry-engine-open-source">
<h1>GEOS: Geometry Engine, Open Source</h1>
<p><strong>GEOS</strong> Geometry Engine, Open Source</p>
<ul class="simple">
<li><strong>model for geometric objects</strong> (Point, Linestring, Polygon, Multipoint, Multipolygon, GeomCollection)</li>
<li><strong>geometric attributes and methods</strong> (empty, geom_type, num_coords, centroid, area, distance, length, srs, transform...)</li>
<li><strong>representation and interoperation</strong> (ewkt, hex, hexewkb, json, geojson, kml, ogr, wkb, ewkb, wkt)</li>
<li><strong>unary predicates</strong> (has_z, simple, valid...)</li>
<li><strong>binary predicates</strong> (contains, crosses, equals, intersects, touches, within, ...)</li>
<li><strong>constructive spatial analysis methods</strong> (buffer, difference, intersection, simplify, union, envelope, ...)</li>
</ul>
</div>
<div class="slide" id="geos-huge-diffusion">
<h1>GEOS: huge diffusion</h1>
<blockquote>
Large list of sofware using GEOS, here only most important (full list @ GEOS website)</blockquote>
<ul class="simple">
<li><strong>FOSS4G</strong>: PostGIS, Spatialite, MapServer, QGIS, OGR, Shapely, GeoDjango</li>
<li><strong>proprietary</strong>: FME, Autodesk MapGuide Enterprise</li>
</ul>
</div>
<div class="slide" id="geos-bindings">
<h1>GEOS: bindings</h1>
<blockquote>
bindings provide the GEOS power to developers using other languages than C/C++</blockquote>
<ul class="simple">
<li><strong>Python</strong> (not maintained anymore --> <strong>GeoDjango, Shapely</strong>)</li>
<li>Ruby</li>
<li>PHP</li>
</ul>
<p>Java developers of course must use the JTS!</p>
<p>.NET developers can use the .NET JTS port (NetTopologySuite)</p>
</div>
<div class="slide" id="building-blocks-proj-4">
<h1>Building blocks: PROJ.4</h1>
<p><strong>PROJ.4</strong> Cartographic Projections Library</p>
<ul class="simple">
<li>PROJ.4 Cartographic Projections library originally written by Gerald Evenden then of the USGS</li>
<li>written in C/C++</li>
<li>both a <strong>command line</strong> and an <strong>API</strong></li>
<li>used from almost any FOSS4G project</li>
<li>ported to javascript (<strong>Proj4js</strong>) and Java (<strong>Proj4J</strong>)</li>
<li>license: <strong>MIT</strong></li>
</ul>
</div>
<div class="slide" id="proj-4-api">
<h1>PROJ.4: API</h1>
<p>Just 2 methods available:</p>
<ul class="simple">
<li>create a projPJ coordinate system object from the string definition</li>
<li>transform the x/y/z points from the source coordinate system to the destination coordinate system:</li>
</ul>
<div class="highlight"><pre>projPJ pj_init_plus<span class="o">(</span>const char *definition<span class="o">)</span>;
int pj_transform<span class="o">(</span> projPJ srcdefn, projPJ dstdefn, long point_count, int point_offset, double *x, double *y, double *z <span class="o">)</span>;
</pre></div>
</div>
<div class="slide" id="implementations">
<h1>Implementations</h1>
<ul class="simple">
<li><strong>GDAL/OGR bindings</strong>: Python API to GDAL/OGR, PROJ.4 and GEOS (parts of)</li>
<li><strong>GeoDjango</strong>: Python API to GDAL/OGR, PROJ.4 and GEOS plus other goodness</li>
<li><strong>Shapely</strong>: Python API to GEOS</li>
<li><strong>GeoAlchemy</strong>: Python API integrating SQLAlchemy for spatial database support</li>
</ul>
</div>
<div class="slide" id="id1">
<h1>GDAL/OGR bindings</h1>
<ul class="simple">
<li>GDAL/OGR library offers Python bindings generated by <strong>SWIG</strong></li>
<li>GDAL is for raster, OGR for vector</li>
<li>GDAL Python bindings is the <strong>only solution for raster</strong></li>
<li>documentation to be improved</li>
<li>license: <strong>X/MIT</strong></li>
</ul>
</div>
<div class="slide" id="gdal-ogr-bindings-gdal-example-1">
<h1>GDAL/OGR bindings: GDAL example (1)</h1>
<blockquote>
accessing the raster, getting the projection and reading general properties</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">gdal</span>
<span class="o">>>></span> <span class="n">ds</span> <span class="o">=</span> <span class="n">gdal</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">'aster.img'</span><span class="p">,</span> <span class="n">gdal</span><span class="o">.</span><span class="n">GA_ReadOnly</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">ds</span><span class="o">.</span><span class="n">GetProjection</span><span class="p">()</span>
<span class="s">'PROJCS["UTM Zone 12, Northern Hemisphere",...AUTHORITY["EPSG","32612"]]'</span>
<span class="o">>>></span> <span class="k">print</span> <span class="s">'Raster has </span><span class="si">%s</span><span class="s"> cols, </span><span class="si">%s</span><span class="s"> rows'</span> <span class="o">%</span> <span class="p">(</span><span class="n">ds</span><span class="o">.</span><span class="n">RasterXSize</span><span class="p">,</span> <span class="n">ds</span><span class="o">.</span><span class="n">RasterYSize</span><span class="p">)</span>
<span class="n">Raster</span> <span class="n">has</span> <span class="mi">5665</span> <span class="n">cols</span><span class="p">,</span> <span class="mi">5033</span> <span class="n">rows</span>
<span class="o">>>></span> <span class="k">print</span> <span class="s">'Raster has </span><span class="si">%s</span><span class="s"> bands'</span> <span class="o">%</span> <span class="n">ds</span><span class="o">.</span><span class="n">RasterCount</span>
<span class="n">Raster</span> <span class="n">has</span> <span class="mi">3</span> <span class="n">bands</span>
</pre></div>
</div>
<div class="slide" id="gdal-ogr-bindings-gdal-example-2">
<h1>GDAL/OGR bindings: GDAL example (2)</h1>
<blockquote>
accessing the raster geotrasform parameters - aka the georeferencing information</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">geotransform</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">GetGeoTransform</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">geotransform</span>
<span class="p">(</span><span class="mf">419976.5</span><span class="p">,</span> <span class="mf">15.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">4662422.5</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">15.0</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span> <span class="s">'top left x is </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">top</span> <span class="n">left</span> <span class="n">x</span> <span class="ow">is</span> <span class="mf">419976.5</span>
<span class="o">>>></span> <span class="k">print</span> <span class="s">'top left y is </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="n">top</span> <span class="n">left</span> <span class="n">y</span> <span class="ow">is</span> <span class="mf">4662422.5</span>
<span class="o">>>></span> <span class="k">print</span> <span class="s">'pixel width is </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">pixel</span> <span class="n">width</span> <span class="ow">is</span> <span class="mf">15.0</span>
<span class="o">>>></span> <span class="k">print</span> <span class="s">'pixel height is </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
<span class="n">pixel</span> <span class="n">height</span> <span class="ow">is</span> <span class="o">-</span><span class="mf">15.0</span>
<span class="o">>>></span> <span class="k">print</span> <span class="s">'raster rotation is </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">geotransform</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="n">raster</span> <span class="n">rotation</span> <span class="ow">is</span> <span class="mf">0.0</span>
</pre></div>
</div>
<div class="slide" id="gdal-ogr-bindings-gdal-example-3">
<h1>GDAL/OGR bindings: GDAL example (3)</h1>
<blockquote>
reading the value of a cell for a given band (optimization issues, this is just a sample)</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">cols</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">RasterXSize</span>
<span class="o">>>></span> <span class="n">rows</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">RasterYSize</span>
<span class="o">>>></span> <span class="n">band1</span> <span class="o">=</span> <span class="n">ds</span><span class="o">.</span><span class="n">GetRasterBand</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">data</span> <span class="o">=</span> <span class="n">band1</span><span class="o">.</span><span class="n">ReadAsArray</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span> <span class="n">cols</span><span class="p">,</span> <span class="n">rows</span><span class="p">)</span> <span class="o">//</span> <span class="mi">0</span><span class="p">,</span><span class="mi">0</span> <span class="ow">is</span> <span class="n">the</span> <span class="n">offset</span>
<span class="o">>>></span> <span class="n">value</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">2000</span><span class="p">,</span><span class="mi">2000</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">value</span>
<span class="mi">61</span>
</pre></div>
</div>
<div class="slide" id="gdal-ogr-bindings-ogr-example-1">
<h1>GDAL/OGR bindings: OGR example (1)</h1>
<blockquote>
reading a shapefile</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">osgeo</span> <span class="kn">import</span> <span class="n">ogr</span>
<span class="o">>>></span> <span class="n">driver</span> <span class="o">=</span> <span class="n">ogr</span><span class="o">.</span><span class="n">GetDriverByName</span><span class="p">(</span><span class="s">'ESRI Shapefile'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">datasource</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">Open</span><span class="p">(</span><span class="s">'regioni.shp'</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">datasource</span><span class="o">.</span><span class="n">GetLayerCount</span><span class="p">()</span>
<span class="mi">1</span>
<span class="o">>>></span> <span class="n">layer</span> <span class="o">=</span> <span class="n">datasource</span><span class="o">.</span><span class="n">GetLayer</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetFeatureCount</span><span class="p">()</span>
<span class="mi">20</span>
</pre></div>
</div>
<div class="slide" id="gdal-ogr-bindings-ogr-example-2">
<h1>GDAL/OGR bindings: OGR example (2)</h1>
<blockquote>
accessing shapefile metadata</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">srs</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetSpatialRef</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">srs</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span>
<span class="n">PROJCS</span><span class="p">[</span><span class="s">"UTM_Zone_32_Northern_Hemisphere"</span><span class="p">,</span><span class="n">GEOGCS</span><span class="p">[</span><span class="s">"GCS_International 1909 (Hayford)"</span><span class="p">,</span><span class="o">....</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetExtent</span><span class="p">()</span>
<span class="p">(</span><span class="mf">313352.32445650722</span><span class="p">,</span> <span class="mf">1312130.1391031265</span><span class="p">,</span> <span class="mf">3933804.0026830882</span><span class="p">,</span> <span class="mf">5220607.6164360112</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">layerDefn</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetLayerDefn</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">layerDefn</span><span class="o">.</span><span class="n">GetFieldCount</span><span class="p">()</span>
<span class="mi">9</span>
<span class="o">>>></span> <span class="n">layerDefn</span><span class="o">.</span><span class="n">GetGeomType</span><span class="p">()</span>
<span class="mi">3</span>
<span class="o">>>></span> <span class="n">fieldDefn</span> <span class="o">=</span> <span class="n">layerDefn</span><span class="o">.</span><span class="n">GetFieldDefn</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">fieldDefn</span><span class="o">.</span><span class="n">GetName</span><span class="p">()</span>
<span class="s">'REGIONE'</span>
<span class="o">>>></span> <span class="n">fieldDefn</span><span class="o">.</span><span class="n">GetTypeName</span><span class="p">()</span>
<span class="s">'String'</span>
</pre></div>
</div>
<div class="slide" id="gdal-ogr-bindings-ogr-example-3">
<h1>GDAL/OGR bindings: OGR example (3)</h1>
<blockquote>
accessing shapefile features and geometries</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">feature</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="n">GetFeature</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">feature</span><span class="o">.</span><span class="n">GetFID</span><span class="p">()</span>
<span class="mi">0</span>
<span class="o">>>></span> <span class="n">feature</span><span class="o">.</span><span class="n">GetField</span><span class="p">(</span><span class="s">'REGIONE'</span><span class="p">)</span>
<span class="s">'PIEMONTE'</span>
<span class="o">>>></span> <span class="n">geometry</span> <span class="o">=</span> <span class="n">feature</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">geometry</span><span class="o">.</span><span class="n">GetEnvelope</span><span class="p">()</span>
<span class="p">(</span><span class="mf">313352.32445650722</span><span class="p">,</span> <span class="mf">517043.7912779671</span><span class="p">,</span> <span class="mf">4879624.4439933635</span><span class="p">,</span> <span class="mf">5146102.0567664672</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">geometry</span><span class="o">.</span><span class="n">GetGeometryName</span><span class="p">()</span>
<span class="s">'MULTIPOLYGON'</span>
<span class="o">>>></span> <span class="n">geometry</span><span class="o">.</span><span class="n">IsValid</span><span class="p">()</span>
<span class="bp">True</span>
<span class="o">>>></span> <span class="n">geometry</span><span class="o">.</span><span class="n">GetDimension</span><span class="p">()</span>
<span class="mi">2</span>
</pre></div>
</div>
<div class="slide" id="gdal-ogr-bindings-ogr-example-4">
<h1>GDAL/OGR bindings: OGR example (4)</h1>
<blockquote>
accessing shapefile features and geometries</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">geometry</span><span class="o">.</span><span class="n">ExportToWkt</span><span class="p">()</span> <span class="c"># GML, KML, Wkb, Json</span>
<span class="s">'MULTIPOLYGON (((456956.454114792693872 5146065.056706172414124,...</span>
<span class="o">>>></span> <span class="n">geometry</span><span class="o">.</span><span class="n">GetArea</span><span class="p">()</span>
<span class="mf">25390743681.717426</span>
<span class="o">>>></span> <span class="n">poly0</span> <span class="o">=</span> <span class="n">geometry</span><span class="o">.</span><span class="n">GetGeometryRef</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">poly0</span><span class="o">.</span><span class="n">GetArea</span><span class="p">()</span>
<span class="mf">25390649513.408951</span>
<span class="o">>>></span> <span class="n">poly0</span><span class="o">.</span><span class="n">GetGeometryName</span><span class="p">()</span>
<span class="s">'POLYGON'</span>
<span class="o">>>></span> <span class="n">mybuffer</span> <span class="o">=</span> <span class="n">poly0</span><span class="o">.</span><span class="n">Buffer</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">mybuffer</span><span class="o">.</span><span class="n">GetArea</span><span class="p">()</span>
<span class="mf">35462220275.922073</span>
</pre></div>
</div>
<div class="slide" id="gdal-ogr-bindings-resources">
<h1>GDAL/OGR bindings: resources</h1>
<ul class="simple">
<li>samples on svn: <a class="reference external" href="http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/">http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/</a></li>
<li>some GDAL command line utilities</li>
<li>many GDAL regression tests are written in Python: <a class="reference external" href="http://svn.osgeo.org/gdal/trunk/autotest/">http://svn.osgeo.org/gdal/trunk/autotest/</a></li>
<li>Geoprocessing with Python using OpenSource GIS: <a class="reference external" href="http://www.gis.usu.edu/~chrisg/python/2009/">http://www.gis.usu.edu/~chrisg/python/2009/</a></li>
<li>GDAL mailing list: <a class="reference external" href="http://lists.osgeo.org/mailman/listinfo/gdal-dev/">http://lists.osgeo.org/mailman/listinfo/gdal-dev/</a></li>
</ul>
</div>
<div class="slide" id="geodjango">
<h1>GeoDjango</h1>
<ul class="simple">
<li><strong>Django</strong>: The Web framework for perfectionists with deadlines. A <strong>DRY</strong> framework with an <strong>ORM</strong> (object relational mapper), a router, a <strong>MVC</strong> implementation and a great backend application</li>
<li><strong>GeoDjango</strong>: The Geographic Web Framework for perfectionists with deadlines</li>
<li>since Django 1.0 is a <strong>core package</strong></li>
<li>it is a framework including a set of API, utility and tool for developing GIS application with Django</li>
<li>as Django, you may use GeoDajngo both in <strong>web</strong> and <strong>desktop</strong> context</li>
<li>excellent documentation</li>
<li>license: <strong>BSD</strong></li>
</ul>
</div>
<div class="slide" id="geodjango-index">
<h1>GeoDjango: Index</h1>
<ul>
<li><p class="first"><strong>GeoDjango Architecture</strong></p>
</li>
<li><dl class="first docutils">
<dt><strong>GeoDjango main features</strong></dt>
<dd><ul class="first last simple">
<li>GeoDjango Model API</li>
<li>GEOS API</li>
<li>GDAL/OGR API</li>
<li>Measurement Units API</li>
<li>GeoDjango Admin site</li>
<li>Utilities (LayerMapping, OgrInspect)</li>
</ul>
</dd>
</dl>
</li>
</ul>
</div>
<div class="slide" id="geodjango-architecture">
<h1>GeoDjango: Architecture</h1>
<ul>
<li><dl class="first docutils">
<dt><strong>Spatial Database</strong></dt>
<dd><ul class="first last simple">
<li>PostGis</li>
<li>Spatialite</li>
<li>MySql (not OGC-compliant, limited functionality)</li>
<li>Oracle</li>
</ul>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt><strong>GIS Libraries</strong> (Python API via <strong>ctypes</strong>)</dt>
<dd><ul class="first last simple">
<li>GEOS (Geometry Engine Open Source)</li>
<li>GDAL/OGR (Geospatial Data Abstraction Library)</li>
<li>PROJ.4 (Cartographic Projections Library)</li>
<li>GeoIP</li>
</ul>
</dd>
</dl>
</li>
</ul>
</div>
<div class="slide" id="geodjango-model-api-1">
<h1>GeoDjango: Model API (1)</h1>
<blockquote>
<strong>Geometry Field</strong> (django.contrib.gis.db extends django.db)</blockquote>
<ul>
<li><p class="first">PointField, LineStringField, PolygonField</p>
</li>
<li><p class="first">MultiPointField, MultiLineStringField, MultiPolygonField</p>
</li>
<li><p class="first">GeometryCollectionField</p>
</li>
<li><p class="first">GeometryField</p>
<blockquote>
<p>Geometry Field options</p>
</blockquote>
</li>
<li><p class="first"><strong>srid</strong> (default 4326 = WGS84 dd)</p>
</li>
<li><p class="first"><strong>dim</strong> (default 2, 3 will support z)</p>
</li>
<li><p class="first"><strong>spatial_index</strong> (default True, spatial index is built)</p>
</li>
</ul>
</div>
<div class="slide" id="geodjango-model-api-2">
<h1>GeoDjango: Model API (2)</h1>
<blockquote>
In Django models we get <strong>Geometry Field</strong> and <strong>GeoManager</strong></blockquote>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.gis.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="k">class</span> <span class="nc">Site</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
<span class="sd">"""Spatial model for site"""</span>
<span class="n">code</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">()</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
<span class="n">geometry</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">MultiPolygonField</span><span class="p">(</span><span class="n">srid</span><span class="o">=</span><span class="mi">4326</span><span class="p">)</span>
<span class="n">objects</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">GeoManager</span><span class="p">()</span>
</pre></div>
</div>
<div class="slide" id="geodjango-model-api-3">
<h1>GeoDjango: Model API (3)</h1>
<div class="highlight"><pre><span class="nv">$ </span>./manage.py sqlall myapp
</pre></div>
<div class="highlight"><pre><span class="k">BEGIN</span><span class="p">;</span>
<span class="k">CREATE</span> <span class="k">TABLE</span> <span class="ss">"myapp_site"</span> <span class="p">(</span>
<span class="ss">"id"</span> <span class="nb">serial</span> <span class="k">NOT</span> <span class="k">NULL</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
<span class="ss">"code"</span> <span class="nb">integer</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">,</span>
<span class="ss">"name"</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span> <span class="k">NOT</span> <span class="k">NULL</span>
<span class="p">)</span>
<span class="p">;</span>
<span class="k">SELECT</span> <span class="n">AddGeometryColumn</span><span class="p">(</span><span class="s1">'myapp_site'</span><span class="p">,</span> <span class="s1">'geometry'</span><span class="p">,</span> <span class="mi">4326</span><span class="p">,</span> <span class="s1">'MULTIPOLYGON'</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
<span class="k">ALTER</span> <span class="k">TABLE</span> <span class="ss">"myapp_site"</span> <span class="k">ALTER</span> <span class="ss">"geometry"</span> <span class="k">SET</span> <span class="k">NOT</span> <span class="k">NULL</span><span class="p">;</span>
<span class="k">CREATE</span> <span class="k">INDEX</span> <span class="ss">"myapp_site_geometry_id"</span>
<span class="k">ON</span> <span class="ss">"myapp_site"</span> <span class="k">USING</span> <span class="n">GIST</span> <span class="p">(</span> <span class="ss">"geometry"</span> <span class="n">GIST_GEOMETRY_OPS</span> <span class="p">);</span>
<span class="k">COMMIT</span><span class="p">;</span>
</pre></div>
</div>
<div class="slide" id="geodjango-model-api-4">
<h1>GeoDjango: Model API (4)</h1>
<blockquote>
CRUD methods: Create, Update</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="o">*</span>
<span class="o">>>></span> <span class="n">new_point</span> <span class="o">=</span> <span class="n">SandboxLayer</span><span class="p">(</span><span class="n">nome</span><span class="o">=</span><span class="s">'punto 1'</span><span class="p">,</span> <span class="n">geometry</span><span class="o">=</span><span class="s">'POINT(13.8 42.5)'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">new_point</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="p">{</span><span class="s">'time'</span><span class="p">:</span> <span class="s">'0.061'</span><span class="p">,</span> <span class="s">'sql'</span><span class="p">:</span> <span class="s">'INSERT INTO "fauna_sandboxlayer" ("nome", "geometry")</span>
<span class="n">VALUES</span> <span class="p">(</span><span class="n">E</span>\<span class="s">'punto 1</span><span class="se">\'</span><span class="s">, ST_GeomFromEWKB(E</span><span class="se">\'\\\\</span><span class="s">001</span><span class="se">\\\\</span><span class="s">...'</span><span class="p">))</span><span class="s">'}</span>
</pre></div>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">new_point</span> <span class="o">=</span> <span class="n">SandboxLayer</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">nome__contains</span><span class="o">=</span><span class="s">'pun'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">new_point</span><span class="o">.</span><span class="n">nome</span> <span class="o">=</span> <span class="s">'punto 2'</span>
<span class="o">>>></span> <span class="n">new_point</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="p">{</span><span class="s">'time'</span><span class="p">:</span> <span class="s">'0.002'</span><span class="p">,</span> <span class="s">'sql'</span><span class="p">:</span> <span class="s">'UPDATE "fauna_sandboxlayer" SET "nome" = E</span><span class="se">\'</span><span class="s">punto 2</span><span class="se">\'</span><span class="s">,</span>
<span class="s">"geometry"</span> <span class="o">=</span> <span class="n">ST_GeomFromEWKB</span><span class="p">(</span><span class="n">E</span>\<span class="s">'</span><span class="se">\\\\</span><span class="s">001</span><span class="se">\\\\</span><span class="s">...'</span><span class="p">)</span>
<span class="n">WHERE</span> <span class="s">"fauna_sandboxlayer"</span><span class="o">.</span><span class="s">"id"</span> <span class="o">=</span> <span class="mi">1</span> <span class="s">'}</span>
</pre></div>
</div>
<div class="slide" id="geodjango-model-api-5">
<h1>GeoDjango: Model API (5)</h1>
<blockquote>
CRUD methods: Read, Delete</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">avvistamento</span> <span class="o">=</span> <span class="n">Avvistamento</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">regione</span> <span class="o">=</span> <span class="n">Regione</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">geometry__intersects</span><span class="o">=</span><span class="n">avvistamento</span><span class="o">.</span><span class="n">geometry</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">regione</span>
<span class="p">[</span><span class="o"><</span><span class="n">Regione</span><span class="p">:</span> <span class="n">ABRUZZO</span><span class="o">></span><span class="p">]</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="p">{</span><span class="s">'time'</span><span class="p">:</span> <span class="s">'0.187'</span><span class="p">,</span> <span class="s">'sql'</span><span class="p">:</span> <span class="s">'SELECT "fauna_regione"."id", "fauna_regione"."codice",</span>
<span class="s">"fauna_regione"</span><span class="o">.</span><span class="s">"nome"</span><span class="p">,</span> <span class="s">"fauna_regione"</span><span class="o">.</span><span class="s">"geometry"</span>
<span class="n">FROM</span> <span class="s">"fauna_regione"</span> <span class="n">WHERE</span> <span class="n">ST_Intersects</span><span class="p">(</span><span class="s">"fauna_regione"</span><span class="o">.</span><span class="s">"geometry"</span><span class="p">,</span>
<span class="n">ST_GeomFromEWKB</span><span class="p">(</span><span class="n">E</span>\<span class="s">'</span><span class="se">\\\\</span><span class="s">001\...'</span><span class="p">))</span> <span class="n">LIMIT</span> <span class="mi">21</span><span class="s">'}</span>
</pre></div>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">sandfeat</span> <span class="o">=</span> <span class="n">SandboxLayer</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">sandfeat</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="p">{</span><span class="s">'time'</span><span class="p">:</span> <span class="s">'0.002'</span><span class="p">,</span> <span class="s">'sql'</span><span class="p">:</span> <span class="s">'DELETE FROM "fauna_sandboxlayer" WHERE "id" IN (1)'</span><span class="p">}</span>
<span class="o">>>></span> <span class="n">SandboxLayer</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">])</span>
<span class="p">{</span><span class="s">'time'</span><span class="p">:</span> <span class="s">'0.002'</span><span class="p">,</span> <span class="s">'sql'</span><span class="p">:</span> <span class="s">'DELETE FROM "fauna_sandboxlayer" WHERE "id" IN (3, 2)'</span><span class="p">}</span>
</pre></div>
</div>
<div class="slide" id="geodjango-geos-api-1">
<h1>GeoDjango: GEOS API (1)</h1>
<blockquote>
a model for geometric objects (Simple Feature Access)</blockquote>
<ul class="simple">
<li>Point</li>
<li>LineString, LinearRing</li>
<li>Polygon</li>
<li>Geometry Collections (MultiPoint, MultiLineString, MultiPolygon, GeometryCollection)</li>
</ul>
</div>
<div class="slide" id="geodjango-geos-api-2">
<h1>GeoDjango: GEOS API (2)</h1>
<ul class="simple">
<li><strong>geometric attributes and methods</strong> (empty, geom_type, num_coords, centroid, area, distance, length, srs, transform...)</li>
<li><strong>representation and interoperation</strong> (ewkt, hex, hexewkb, json, geojson, kml, ogr, wkb, ewkb, wkt)</li>
<li><strong>unary predicates</strong> (has_z, simple, valid...)</li>
<li><strong>binary predicates</strong> (contains, crosses, equals, intersects, touches, within, ...)</li>
<li><strong>constructive spatial analysis methods</strong> (buffer, difference, intersection, simplify, union, envelope, ...)</li>
</ul>
</div>
<div class="slide" id="geodjango-geos-api-example-1">
<h1>GeoDjango: GEOS API, Example 1</h1>
<blockquote>
geometric objects (point), geometric properties (hasz, geom_type)
and representation and serialization</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Place</span>
<span class="o">>>></span> <span class="n">place</span> <span class="o">=</span> <span class="n">Place</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">point</span> <span class="o">=</span> <span class="n">place</span><span class="o">.</span><span class="n">geometry</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">point</span><span class="o">.</span><span class="n">y</span>
<span class="p">(</span><span class="mf">13.798828125</span><span class="p">,</span> <span class="mf">42.5390625</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">hasz</span>
<span class="bp">False</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">geom_type</span>
<span class="s">'Point'</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">json</span>
<span class="s">'{ "type": "Point", "coordinates": [ 13.798828, 42.539062 ] }'</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">ewkt</span> <span class="c"># extended wkt</span>
<span class="s">'SRID=4326;POINT (13.7988281250000000 42.5390625000000000)'</span>
</pre></div>
</div>
<div class="slide" id="geodjango-geos-api-example-2">
<h1>GeoDjango: GEOS API, Example 2</h1>
<blockquote>
predicates and relationships, transformations (requires GDAL), spatial analysis methods</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="o">*</span>
<span class="o">>>></span> <span class="n">abruzzo</span> <span class="o">=</span> <span class="n">Regione</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">nome</span><span class="o">=</span><span class="s">'ABRUZZO'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">avvistamento</span> <span class="o">=</span> <span class="n">Avvistamento</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">abruzzo</span><span class="o">.</span><span class="n">geometry</span><span class="o">.</span><span class="n">contains</span><span class="p">(</span><span class="n">avvistamento</span><span class="o">.</span><span class="n">geometry</span><span class="p">)</span>
<span class="bp">True</span>
<span class="o">>>></span> <span class="n">avvistamento</span><span class="o">.</span><span class="n">geometry</span><span class="o">.</span><span class="n">ewkt</span>
<span class="s">'SRID=4326;POINT (13.7988281250000000 42.5390625000000000)'</span>
<span class="o">>>></span> <span class="n">transformed_point</span> <span class="o">=</span> <span class="n">avvistamento</span><span class="o">.</span><span class="n">geometry</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="mi">3395</span><span class="p">,</span><span class="n">clone</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">transformed_point</span><span class="o">.</span><span class="n">ewkt</span>
<span class="s">'SRID=3395;POINT (1536078.5204189007636160 5213176.4834084874019027)'</span>
<span class="o">>>></span> <span class="nb">buffer</span> <span class="o">=</span> <span class="n">SandboxLayer</span><span class="p">(</span><span class="n">nome</span><span class="o">=</span><span class="s">'buffer'</span><span class="p">,</span><span class="n">geometry</span><span class="o">=</span><span class="n">transformed_point</span><span class="o">.</span><span class="n">buffer</span><span class="p">(</span><span class="mi">20000</span><span class="p">))</span>
<span class="o">>>></span> <span class="nb">buffer</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
</pre></div>
</div>
<div class="slide" id="geodjango-gdal-ogr-api">
<h1>GeoDjango: GDAL/OGR API</h1>
<blockquote>
excellent alternative to GDAL/OGR Python bindings</blockquote>
<ul class="simple">
<li>not <strong>required</strong> for GeoDjango (required only for srs trasformations and for LayerMapping)</li>
<li>via the <strong>DataSource</strong> class get the access to any <strong>OGR</strong> format, (R/W in many cases)</li>
<li>get access to the GEOS API via geos method on <strong>OGRGeometry</strong> class</li>
<li>get access to other API via interoperation and representation properties (wkt, wkb, json, ...)</li>
</ul>
</div>
<div class="slide" id="geodjango-gdal-ogr-api-example">
<h1>GeoDjango: GDAL/OGR API, Example</h1>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">django.contrib.gis.gdal</span> <span class="kn">import</span> <span class="o">*</span>
<span class="o">>>></span> <span class="n">ds</span> <span class="o">=</span> <span class="n">DataSource</span><span class="p">(</span><span class="s">'data/shapefile/myshape.shp'</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">ds</span><span class="p">)</span>
<span class="n">data</span><span class="o">/</span><span class="n">shapefile</span><span class="o">/</span><span class="n">myshape</span><span class="o">.</span><span class="n">shp</span> <span class="p">(</span><span class="n">ESRI</span> <span class="n">Shapefile</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ds</span><span class="p">))</span>
<span class="mi">1</span>
<span class="o">>>></span> <span class="n">lyr</span> <span class="o">=</span> <span class="n">ds</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">lyr</span><span class="p">)</span>
<span class="n">myshape</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">lyr</span><span class="o">.</span><span class="n">num_feat</span><span class="p">)</span>
<span class="mi">20</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">lyr</span><span class="o">.</span><span class="n">geom_type</span><span class="p">)</span>
<span class="n">Polygon</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">lyr</span><span class="o">.</span><span class="n">srs</span><span class="o">.</span><span class="n">srid</span><span class="p">)</span>
<span class="mi">4326</span>
</pre></div>
</div>
<div class="slide" id="id2">
<h1>GeoDjango: GDAL/OGR API, Example</h1>
<div class="highlight"><pre><span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">lyr</span><span class="o">.</span><span class="n">fields</span><span class="p">)</span>
<span class="p">[</span><span class="s">'gid'</span><span class="p">,</span> <span class="s">'objectid'</span><span class="p">,</span> <span class="s">'code'</span><span class="p">,</span> <span class="s">'name'</span><span class="p">,</span> <span class="s">'shape_area'</span><span class="p">,</span> <span class="s">'shape_len'</span><span class="p">]</span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">feat</span> <span class="ow">in</span> <span class="n">lyr</span><span class="p">:</span>
<span class="o">....</span><span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="n">feat</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'name'</span><span class="p">),</span> <span class="n">feat</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">num_points</span><span class="p">)</span>
<span class="o">....</span><span class="p">:</span>
<span class="n">first_feature</span> <span class="mi">14811</span>
<span class="n">second_feature</span> <span class="mi">3598</span>
<span class="o">...</span>
<span class="n">last_feature</span> <span class="mi">19131</span>
<span class="o">>>></span> <span class="n">feat</span> <span class="o">=</span> <span class="n">lyr</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">feat</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'name'</span><span class="p">))</span>
<span class="n">first_feature</span>
<span class="o">>>></span> <span class="n">geom</span> <span class="o">=</span> <span class="n">feat</span><span class="o">.</span><span class="n">geom</span> <span class="c"># OGRGeometry, non GEOSGeometry</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">geom</span><span class="o">.</span><span class="n">srid</span><span class="p">)</span>
<span class="mi">4326</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">feat</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">wkt</span><span class="p">[:</span><span class="mi">100</span><span class="p">])</span>
<span class="n">MULTIPOLYGON</span> <span class="p">(((</span><span class="mf">8.439415832216145</span> <span class="mf">46.465900481500874</span><span class="p">,</span><span class="mf">8.439484266241374</span> <span class="mf">46.465576832714113</span><span class="p">,</span><span class="mf">8.43950386</span><span class="o">...</span>
</pre></div>
</div>
<div class="slide" id="geodjango-measurement-units-api">
<h1>GeoDjango: Measurement Units API</h1>
<blockquote>
API for measurement units conversion and management</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">django.contrib.gis.measure</span> <span class="kn">import</span> <span class="n">Distance</span>
<span class="o">>>></span> <span class="n">d1</span> <span class="o">=</span> <span class="n">Distance</span><span class="p">(</span><span class="n">km</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">d1</span>
<span class="mf">5.0</span> <span class="n">km</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">d1</span><span class="o">.</span><span class="n">mi</span>
<span class="mf">3.10685596119</span>
<span class="o">>>></span> <span class="n">d2</span> <span class="o">=</span> <span class="n">Distance</span><span class="p">(</span><span class="n">mi</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">d1</span> <span class="o">+</span> <span class="n">d2</span>
<span class="mf">13.04672</span> <span class="n">km</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">d1</span> <span class="o">*</span> <span class="n">d2</span>
<span class="k">print</span> <span class="n">a</span>
<span class="mf">40.2336</span> <span class="n">sq_km</span>
</pre></div>
</div>
<div class="slide" id="geodjango-resources">
<h1>GeoDjango: resources</h1>
<ul class="simple">
<li>excellent documentation: <a class="reference external" href="http://docs.djangoproject.com/en/dev/ref/contrib/gis/">http://docs.djangoproject.com/en/dev/ref/contrib/gis/</a></li>
<li>official tutorial: <a class="reference external" href="http://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/">http://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/</a></li>
<li>GeoDjango Basic Apps: <a class="reference external" href="http://code.google.com/p/geodjango-basic-apps/">http://code.google.com/p/geodjango-basic-apps/</a></li>
<li>Python Geospatial Development, a book from Packt: <a class="reference external" href="https://www.packtpub.com/python-geo-spatial-development/book">https://www.packtpub.com/python-geo-spatial-development/book</a></li>
<li>Justin Bronn at DjangoCon 2008: <a class="reference external" href="http://www.youtube.com/watch?v=zOaimbSe6n8">http://www.youtube.com/watch?v=zOaimbSe6n8</a></li>
<li>mailing list: <a class="reference external" href="http://groups.google.com/group/geodjango">http://groups.google.com/group/geodjango</a></li>
<li>an overview by Dane Springmeyer: <a class="reference external" href="http://www.geowebguru.com/articles/99-overview-geodjango">http://www.geowebguru.com/articles/99-overview-geodjango</a></li>
<li>workshop @ FOSS4G-IT 2010 (Lugano): <a class="reference external" href="https://github.com/capooti/geodjango-tutorial">https://github.com/capooti/geodjango-tutorial</a> (in Italian)</li>
<li>a day with GeoDjango: <a class="reference external" href="http://www.paolocorti.net/2009/04/01/a-day-with-geodjango/">http://www.paolocorti.net/2009/04/01/a-day-with-geodjango/</a></li>
</ul>
</div>
<div class="slide" id="shapely">
<h1>Shapely</h1>
<ul class="simple">
<li>it is a Python binding library to GEOS via ctypes (like the GeoDjango GEOS API)</li>
<li>aims to be <strong>general purpose</strong>, not only for GIS stuff (even if it is a loyal OGC SFA implementation)</li>
<li>excellent documentation (very nice manual)</li>
<li><strong>integration</strong>: via serialization/deserialization with standard well known formats (wkt, wkb)</li>
<li><strong>projections are not supported</strong>, so geometries must be in a unique projected srs</li>
<li>license: <strong>BSD</strong></li>
</ul>
</div>
<div class="slide" id="shapely-features-ogc-sfa-1">
<h1>Shapely features: OGC SFA (1)</h1>
<blockquote>
a model for geometric objects (Simple Feature Access)</blockquote>
<ul class="simple">
<li>Point</li>
<li>LineString, LinearRing</li>
<li>Polygon</li>
<li>Geometry Collections (MultiPoint, MultiLineString, MultiPolygon, GeometryCollection)</li>
<li><strong>Empty features, Linear Referencing</strong></li>
</ul>
</div>
<div class="slide" id="shapely-ogc-sfa-2">
<h1>Shapely: OGC SFA (2)</h1>
<ul class="simple">
<li><strong>general attributes and methods</strong> (area, bounds, length, geom_type, distance, centroid, representative_point, coords, exterior, interiors)</li>
<li><strong>representation and interoperation</strong> (ewkt, hex, hexewkb, json, geojson, kml, ogr, wkb, ewkb, wkt)</li>
<li><strong>unary predicates</strong> (has_z, is_empty, is_ring, is_simple, is_valid)</li>
<li><strong>binary predicates</strong> (contains, crosses, equals, intersects, touches, within, ...)</li>
<li><strong>constructive spatial analysis methods</strong> (buffer, difference, intersection, simplify, union, polygonize, linemerge, ...)</li>
<li><strong>diagnostics</strong> (explain_validity)</li>
</ul>
</div>
<div class="slide" id="shapely-example-1">
<h1>Shapely: Example 1</h1>
<blockquote>
geometric objects (point), general attributes and methods
and representation and interoperation</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">shapely.geometry</span> <span class="kn">import</span> <span class="n">Point</span>
<span class="o">>>></span> <span class="n">point</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">area</span>
<span class="mf">0.0</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">bounds</span>
<span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">point</span><span class="o">.</span><span class="n">y</span>
<span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">area</span>
<span class="mf">0.0</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">length</span>
<span class="mf">0.0</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">geom_type</span>
<span class="s">'Point'</span>
<span class="o">>>></span> <span class="n">point</span><span class="o">.</span><span class="n">wkt</span>
<span class="s">'POINT (0.0000000000000000 0.0000000000000000)'</span>
</pre></div>
</div>
<div class="slide" id="shapely-example-2">
<h1>Shapely: Example 2</h1>
<blockquote>
geometric objects (polygon), general attributes and methods
and representation and interoperation</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">shapely.geometry</span> <span class="kn">import</span> <span class="n">Polygon</span>
<span class="o">>>></span> <span class="n">polygon</span> <span class="o">=</span> <span class="n">Polygon</span><span class="p">([(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">)])</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">area</span>
<span class="mf">2.0</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">length</span>
<span class="mf">6.0</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">bounds</span>
<span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">geom_type</span>
<span class="s">'Polygon'</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">wkt</span>
<span class="s">'POLYGON ((-1.0000000000000000 -1.0000000000000000, ...</span>
<span class="o">>>></span> <span class="nb">list</span><span class="p">(</span><span class="n">polygon</span><span class="o">.</span><span class="n">exterior</span><span class="o">.</span><span class="n">coords</span><span class="p">)</span>
<span class="p">[(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">)]</span>
<span class="o">>>></span> <span class="nb">list</span><span class="p">(</span><span class="n">polygon</span><span class="o">.</span><span class="n">interiors</span><span class="p">)</span>
<span class="p">[]</span>
</pre></div>
</div>
<div class="slide" id="shapely-example-3">
<h1>Shapely: Example 3</h1>
<blockquote>
unary predicates, binary predicates, spatial analysis methods</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">has_z</span>
<span class="bp">False</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">is_empty</span>
<span class="bp">False</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">is_valid</span>
<span class="bp">True</span>
<span class="o">>>></span> <span class="n">polygon</span><span class="o">.</span><span class="n">contains</span><span class="p">(</span><span class="n">point</span><span class="p">)</span>
<span class="bp">False</span>
<span class="o">>>></span> <span class="nb">buffer</span> <span class="o">=</span> <span class="n">polygon</span><span class="o">.</span><span class="n">buffer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="o">>>></span> <span class="nb">buffer</span><span class="o">.</span><span class="n">contains</span><span class="p">(</span><span class="n">point</span><span class="p">)</span>
<span class="bp">True</span>
</pre></div>
</div>
<div class="slide" id="shapely-example-4">
<h1>Shapely: Example 4</h1>
<blockquote>
diagnostics</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">coords</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]</span>
<span class="o">>>></span> <span class="n">p</span> <span class="o">=</span> <span class="n">Polygon</span><span class="p">(</span><span class="n">coords</span><span class="p">)</span>
<span class="o">>>></span> <span class="kn">from</span> <span class="nn">shapely.validation</span> <span class="kn">import</span> <span class="n">explain_validity</span>
<span class="o">>>></span> <span class="n">explain_validity</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="s">'Ring Self-intersection[1 1]'</span>
</pre></div>
</div>
<div class="slide" id="shapely-resources">
<h1>Shapely: resources</h1>
<ul class="simple">
<li>excellent documentation: <a class="reference external" href="http://gispython.org/shapely/docs/1.2/">http://gispython.org/shapely/docs/1.2/</a></li>
<li>GIS Python Lab: <a class="reference external" href="http://trac.gispython.org/lab">http://trac.gispython.org/lab</a></li>
<li>Sean Gillies Blog: <a class="reference external" href="http://sgillies.net/blog/">http://sgillies.net/blog/</a></li>
<li>Python Workshop at FOSS4G 2010: <a class="reference external" href="http://www.mapfish.org/doc/tutorials/python-workshop/geoalchemy.html">http://www.mapfish.org/doc/tutorials/python-workshop/geoalchemy.html</a></li>
<li>mailing list @gispython.org: <a class="reference external" href="http://lists.gispython.org/mailman/listinfo/community">http://lists.gispython.org/mailman/listinfo/community</a></li>
</ul>
</div>
<div class="slide" id="geoalchemy">
<h1>GeoAlchemy</h1>
<blockquote>
GeoAlchemy</blockquote>
<ul class="simple">
<li>it is a spatial extension to <strong>SQLAlchemy</strong></li>
<li>it provides support for Geospatial data types at the ORM layer using SQLAlchemy</li>
<li>it aims to support spatial operations and relations specified by the Open Geospatial Consortium (OGC). The project started under Google Summer of Code Program</li>
<li>differently from other libraries, it does <strong>NOT DEPEND</strong> on other GIS building blocks (GDAL, GEOS...)</li>
<li>still not so mature like GeoDjango</li>
<li>license: <strong>MIT</strong></li>
</ul>
</div>
<div class="slide" id="geoalchemy-sqlalchemy-notes">
<h1>GeoAlchemy: SQLAlchemy notes</h1>
<blockquote>
SQLAlchemy</blockquote>
<ul class="simple">
<li>SQLAlchemy is the most powerful Python SQL Toolkit and ORM</li>
<li>compared to the Django ORM, it has a most powerfull abstraction</li>
<li>supports not only tables (like Django) but also <strong>joins, subqueries, and unions</strong></li>
<li>higly <strong>scalable</strong> (ie: configuration of how many SELECT to emit while loading whole graphs of objects: lazy loading and eager load)</li>
<li>greater set of DB supported if compared to Django</li>
<li>support for <strong>transactions</strong> (i.e. nice rollback mechanism)</li>
<li>excellent documentations</li>
</ul>
</div>
<div class="slide" id="geoalchemy-spatial-database">
<h1>GeoAlchemy: Spatial database</h1>
<blockquote>
Supported spatial database</blockquote>
<ul class="simple">
<li>PostGis</li>
<li>Spatialite</li>
<li>MySQL (not OGC-compliant, limited functionality)</li>
<li>Oracle</li>
<li>MS SQL Server 2008 (Django does not support it)</li>
</ul>
</div>
<div class="slide" id="geoalchemy-use-cases">
<h1>GeoAlchemy: Use cases</h1>
<ul class="simple">
<li>like Django, you may use it in <strong>desktop and web</strong> application</li>
<li>web frameworks that integrates well with SQLAlchemy: TurboGears and Pylons (highly configurable for models, templates and helpers)</li>
<li>Django is not the best SQLAlchemy friend, but you may still use it :D</li>
</ul>
</div>
<div class="slide" id="geoalchemy-features">
<h1>GeoAlchemy: features</h1>
<ul class="simple">
<li><strong>NO</strong> model for geometric objects (Simple Feature Access) :(</li>
<li><strong>geometric attributes and methods</strong> (dimension, srid, geometry_type, num_points, length, area, centroid, transform, coords)</li>
<li><strong>representation and interoperation</strong> (wkt, wkb, svg, gml, kml, geojson)</li>
<li><strong>unary predicates</strong> (has_z, is_valid, is_empty, is_simple, is_closed, is_ring)</li>
<li><strong>binary predicates</strong> (contains, crosses, equals, intersects, touches, within, ...)</li>
<li><strong>constructive spatial analysis methods</strong> [limited] (buffer, boundary, convex_hull)</li>
</ul>
</div>
<div class="slide" id="geoalchemy-example-1">
<h1>GeoAlchemy: Example 1</h1>
<blockquote>
the model</blockquote>
<div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">'postgresql://postgres:postgres@localhost/gis_test'</span><span class="p">,</span> <span class="n">echo</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>
<span class="n">session</span> <span class="o">=</span> <span class="n">Session</span><span class="p">()</span>
<span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">(</span><span class="n">metadata</span><span class="o">=</span><span class="n">metadata</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">Spot</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
<span class="n">__tablename__</span> <span class="o">=</span> <span class="s">'spots'</span>
<span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Unicode</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">height</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span>
<span class="n">created</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>
<span class="n">geom</span> <span class="o">=</span> <span class="n">GeometryColumn</span><span class="p">(</span><span class="n">Point</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
<span class="n">metadata</span><span class="o">.</span><span class="n">drop_all</span><span class="p">()</span> <span class="c"># comment this on first occassion</span>
<span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">()</span>
</pre></div>
</div>
<div class="slide" id="geoalchemy-example-2">
<h1>GeoAlchemy: Example 2</h1>
<blockquote>
data creation</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">wkt_spot1</span> <span class="o">=</span> <span class="s">"POINT(-81.40 38.08)"</span>
<span class="o">>>></span> <span class="n">spot1</span> <span class="o">=</span> <span class="n">Spot</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Gas Station"</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="mf">240.8</span><span class="p">,</span> <span class="n">geom</span><span class="o">=</span><span class="n">WKTSpatialElement</span><span class="p">(</span><span class="n">wkt_spot1</span><span class="p">))</span>
<span class="o">>>></span> <span class="n">geom_spot2</span> <span class="o">=</span> <span class="n">WKTSpatialElement</span><span class="p">(</span><span class="s">'POINT(30250865 -610981)'</span><span class="p">,</span> <span class="mi">2249</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">spot2</span> <span class="o">=</span> <span class="n">Spot</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Park"</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="mf">53.2</span><span class="p">,</span> <span class="n">geom</span><span class="o">=</span><span class="n">geom_spot2</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">add_all</span><span class="p">([</span><span class="n">spot1</span><span class="p">,</span> <span class="n">spot2</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
</pre></div>
</div>
<div class="slide" id="geoalchemy-example-3">
<h1>GeoAlchemy: Example 3</h1>
<blockquote>
data reading and representation</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">s</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Spot</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">wkt</span><span class="p">)</span>
<span class="s">'POINT(-81.42 37.65)'</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">gml</span><span class="p">)</span>
<span class="s">'<gml:Point srsName="EPSG:4326"><gml:coordinates>-81.42,37.65</gml:coordinates></gml:Point>'</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">kml</span><span class="p">)</span>
<span class="s">'<Point><coordinates>-81.42,37.65</coordinates></Point>'</span>
</pre></div>
</div>
<div class="slide" id="geoalchemy-example-4">
<h1>GeoAlchemy: Example 4</h1>
<blockquote>
geometric properties</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">s</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Spot</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">Spot</span><span class="o">.</span><span class="n">height</span> <span class="o">></span> <span class="mi">240</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">geometry_type</span><span class="p">)</span>
<span class="s">'ST_Point'</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">x</span><span class="p">)</span>
<span class="o">-</span><span class="mf">81.420000000000002</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
<span class="mf">37.649999999999999</span>
<span class="o">>>></span> <span class="n">s</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">coords</span><span class="p">(</span><span class="n">session</span><span class="p">)</span>
<span class="p">[</span><span class="o">-</span><span class="mf">81.420000000000002</span><span class="p">,</span> <span class="mf">37.649999999999999</span><span class="p">]</span>
</pre></div>
</div>
<div class="slide" id="geoalchemy-example-5">
<h1>GeoAlchemy: Example 5</h1>
<blockquote>
constructive spatial analysis methods and binary predicates</blockquote>
<div class="highlight"><pre><span class="o">>>></span> <span class="n">r</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Road</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">l</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Lake</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">buffer_geom</span> <span class="o">=</span> <span class="n">DBSpatialElement</span><span class="p">(</span><span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">buffer</span><span class="p">(</span><span class="mf">10.0</span><span class="p">)))</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">scalar</span><span class="p">(</span><span class="n">buffer_geom</span><span class="o">.</span><span class="n">wkt</span><span class="p">)</span>
<span class="s">'POLYGON((-77.4495270615657 28.6622373442108,....</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Road</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">Road</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">intersects</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">geom</span><span class="p">))</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
<span class="il">1L</span>
<span class="o">>>></span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">Lake</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">Lake</span><span class="o">.</span><span class="n">geom</span><span class="o">.</span><span class="n">touches</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">geom</span><span class="p">))</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
<span class="il">0L</span>