Home
%3CLINGO-SUB%20id%3D%22lingo-sub-1124288%22%20slang%3D%22en-US%22%3ETake%20your%20spatial%20data%20analysis%20to%20the%20next%20level%20with%20PostGIS%20at%20Azure%20PostgreSQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1124288%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3ESpatial%20data%20comes%20in%20many%20shapes%20(sic)%20and%20sizes.%20Location%20information%20can%20be%20something%20more%20abstract%20that%20doesn%E2%80%99t%20look%20like%20spatial%20data%20at%20first%20(an%20IP-address%2C%20a%20placename%20inside%20a%20string)%20or%20it%20can%20be%20something%20more%20concrete%20(an%20address%2C%20coordinates)%20that%20is%20quite%20straightforward%20to%20turn%20in%20to%20geographic%20objects.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ETo%20conduct%20spatial%20analysis%20with%20PostgreSQL%20you%20need%20to%20be%20able%20to%20handle%20geographic%20objects%20and%20to%20be%20able%20to%20handle%20geographic%20objects%2C%20you%20need%20the%20PostGIS%20extension.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20you%20set%20up%20a%20hosted%20PostgreSQL%20database%20on%20Azure%20and%20run%20the%20following%20query%3A%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CI%3E%3CSPAN%3ESELECT%20*%20FROM%20pg_available_extensions%3B%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EThis%20will%20return%20a%20list%20of%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.citusdata.com%2Fblog%2F2018%2F11%2F27%2Fpostgres-more-than-a-relational-database%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3E39%20extensions%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Band%20among%20those%20are%20multiple%20spatial%20extensions.%20Among%20those%20is%20PostGIS.%20To%20install%20PostGIS%20to%20your%20database%2C%20you%20only%20need%20the%20following%20query%3A%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CI%3E%3CSPAN%3ECREATE%20EXTENSION%20postgis%3B%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EAfter%20a%20few%20seconds%20this%20enables%20PostGIS%20and%20you%20might%20notice%20that%20a%20table%26nbsp%3B%3C%2FSPAN%3E%3CI%3E%3CSPAN%3Espatial_ref_sys%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%26nbsp%3Bappears%20to%20your%20public%20schema.%20This%20table%20lists%20over%203000%20known%20spatial%20reference%20systems%20and%20details%20needed%20to%20transform%20and%20reproject%20between%20them.%20Because%20the%20Earth%20is%20not%20flat%20(sorry%20to%20break%20it%20to%20you%20here)%2C%20we%20have%20to%20deal%20with%20various%20coordinate%20systems%20to%20project%20things%20happening%20on%20this%20somewhat%20round%20planet%20to%20flat%20surfaces%20like%20your%20computer%20screen.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSPAN%3EWorking%20with%20spatial%20data%20in%20PostGIS%26nbsp%3B%3C%2FSPAN%3E%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20a%20disclaimer%2C%20I%20have%20to%20note%20at%20this%20point%20that%20working%20with%20spatial%20data%20is%20not%20always%20very%20straightforward.%20That%20is%20why%20there%20are%20people%20(like%20me)%20specialized%20in%20that.%20Traditionally%20the%20field%20has%20been%20called%20GIS%20(see%2C%20Post%3C%2FSPAN%3E%3CSTRONG%3E%3CSPAN%3EGIS!%3C%2FSPAN%3E%3C%2FSTRONG%3E%3CSPAN%3E).%20Nowadays%20GIS%20(Geographic%20Information%20Systems)%20is%20not%20just%26nbsp%3B%3C%2FSPAN%3E%3CI%3E%3CSPAN%3Einformation%20systems%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%2C%20but%20it%20has%20evolved%20to%20things%20like%20location%20intelligence%2C%20spatial%20data%20analytics%20or%20spatial%20data%20science.%20Understanding%20the%20basics%20of%20spatial%20data%20is%20becoming%20more%20important%20in%20all%20subdomains%20of%20data%20analysis.%20However%20you%20might%20label%20it%2C%20we%20are%20still%20talking%20about%20the%20same%20key%20question%3A%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%3CSPAN%3Ewhere%26nbsp%3B%3C%2FSPAN%3E%3C%2FSTRONG%3E%3CSPAN%3Esomething%20is%20happening%20and%20what%20does%20that%20mean%3F%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EBasic%20formats%20of%20spatial%20data%20in%20vector%20format%20are%20points%2C%20lines%20and%20polygons.%20You%20can%20read%20a%20very%20basic%20level%20explanation%20of%20data%20formats%20and%20GIS%20in%20general%20from%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fmapschool.io%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Emapschool.io%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bor%20if%20you%20wish%20to%20dive%20deeper%20into%20the%20topic%2C%20I%20strongly%20recommend%20getting%20familiar%20with%20a%20basic%20GIS%20book.%20Points%20are%20formed%20from%20a%20pair%20of%20coordinates%2C%20lines%20are%20made%20from%20connected%20points%20and%20polygons%20are%20made%20from%20connected%20lines.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F166715i7B3B8902B43FA19C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EPoints%2C%20lines%20and%20polygons.%20That%20is%20a%20good%20starting%20point%20when%20trying%20to%20understand%20spatial%20data.%26nbsp%3B%20Credit%3A%20mapschool.io%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20PostGIS%20points%20(POINT%20type)%2C%20lines%20(LINESTRING)%20and%20polygons%20(POLYGON)%20can%20also%20be%20MULTI-type%20with%20multiple%20geometries%20in%20one%2C%20or%20a%20geometry%20collection%20that%20can%20contain%20different%20geometry%20types%20inside%20one%20cell.%20The%20coordinate%20system%20of%20the%20data%20is%20defined%20by%20spatial%20reference%20identifier%20(SRID).%20In%20addition%20to%20vector%20data%2C%20PostGIS%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Finfo.crunchydata.com%2Fblog%2Fwaiting-for-postgis-3-separate-raster-extension%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Ecan%26nbsp%3B%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3Ealso%20store%20and%20analyze%20raster%20data.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20basis%20for%20the%20normal%20PostGIS%20geometry%20type%20is%20a%20plane%20and%20the%20shortest%20path%20between%20two%20points%20on%20the%20plane%20is%20a%20straight%20line.%20That%20means%20calculations%20on%20geometries%20(areas%2C%20distances%2C%20lengths%2C%20intersections%2C%20etc)%20can%20be%20calculated%20using%20cartesian%20mathematics%20and%20straight%20line%20vectors.%20PostGIS%20also%20has%20a%20geography%20data%20type%2C%20which%20provides%20native%20support%20for%20spatial%20features%20represented%20on%20%22geographic%22%20coordinates%20(sometimes%20called%20%22geodetic%22%20coordinates%2C%20or%20%22lat%2Flon%22%2C%20or%20%22lon%2Flat%22).%20But%20you%20can%20also%20work%20with%20%E2%80%9Clat%2Flon%E2%80%9D%20data%20using%20the%20normal%20geometry%20data%20type.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20OpenGIS%20specification%20which%20PostGIS%20is%20based%20on%2C%20defines%20two%20standard%20ways%20of%20expressing%20spatial%20objects%3A%20the%20Well-Known%20Text%20(WKT)%20form%20and%20the%20Well-Known%20Binary%20(WKB)%20form.%20Both%20of%20these%20include%20information%20about%20the%20type%20of%20the%20object%20and%20the%20coordinates%20which%20form%20the%20object.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESo%20in%20a%20PostGIS%20database%20a%20point%20geometry%20could%20be%20represented%20in%20multiple%20ways.%20If%20I%20run%20the%20following%20query%20in%20my%20PostGIS%20database%20that%20has%20a%20spatial%20table%20with%20cities%3A%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CI%3E%3CSPAN%3ESELECT%20geom%20FROM%20ne_50m_populated_places%20WHERE%20name%20%3D%26nbsp%3B%20'Seattle'%3C%2FSPAN%3E%3C%2FI%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIt%20returns%20the%20following%20binary%20representation%20for%20the%20point%20that%20is%20the%20location%20of%20Seattle%3A%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CSPAN%3E0104000020E6100000010000000101000000EC1FEA31E2955EC0E0AAD59635C94740%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3EBut%20if%20I%20use%20the%20function%20ST_AsEWKT%2C%20the%20same%20point%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CSPAN%3ESRID%3D4326%3BMULTIPOINT(-122.341930845868%2047.5719479125307)%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%3ESo%20the%20SRID%204326%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fepsg.io%2F4326%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eshows%20us%26nbsp%3B%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3Ethat%20the%20data%20is%20in%20coordinate%20system%20called%20WGS84%20-%20World%20Geodetic%20System%201984%2C%20used%20also%20in%20GPS.%20MULTIPOINT%20means%20that%20the%20data%20type%20is%20a%20multi-type%20although%20there%20is%20only%20one%20point%20and%20the%20numbers%20in%20parenthesis%20are%20latitude%20and%20longitude%20of%20Seattle.%20This%20particular%20point%20has%20way%20too%20many%20decimals%20in%20the%20coordinates%20when%20you%20think%20about%20the%20fact%20that%20it%20is%20showing%20where%20Seattle%20is.%20Understanding%20the%20concepts%20of%20scale%20and%20accuracy%20are%20key%20when%20working%20with%20spatial%20data.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F166717i94E3EAC2B212BDEB%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22clipboard_image_1.png%22%20title%3D%22clipboard_image_1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYou%20will%20find%20this%20funny%20at%20some%20point.%20I%20promise.%20Credit%3A%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fxkcd.com%2F2170%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Exkcd%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ESpatial%20indexes%20are%20also%20one%20thing%20that%20works%20slightly%20differently%20with%20spatial%20data%20than%20other%20PostgreSQL%20data%20types.%20Without%20an%20index%20any%20search%20for%20a%20feature%20would%20require%20a%20sequential%20scan%20of%20every%20record%20in%20the%20database%20and%20this%20also%20applies%20for%20spatial%20data.%20Indexing%20speeds%20up%20searching%20by%20organizing%20the%20data%20into%20a%20search%20tree%20which%20can%20be%20quickly%20traversed%20to%20find%20a%20particular%20record.%20Whereas%20standard%20database%20indexes%20create%20a%20hierarchical%20tree%20based%20on%20the%20values%20of%20the%20column%20being%20indexed%2C%20spatial%20indexes%20are%20unable%20to%20index%20the%20geometric%20features%20themselves%20and%20instead%20index%20the%20bounding%20boxes%20of%20the%20features.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EPostGIS%20uses%20R-Tree%20spatial%20index%20structure.%20R-Trees%20break%20up%20data%20into%20rectangles%20(bounding%20boxes)%2C%20and%20sub-rectangles%2C%20and%20sub-sub%20rectangles%2C%20etc.%20It%20is%20a%20self-tuning%20index%20structure%20that%20automatically%20handles%20variable%20data%20density%20and%20object%20size.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThese%20are%20just%20a%20few%20examples%20that%20are%20specific%20when%20working%20with%20spatial%20data.%20In%20the%20next%20blog%20post%20I%20will%20go%20through%20where%20you%20can%20find%20spatial%20data%20if%20you%20don%E2%80%99t%20already%20happen%20to%20have%20some%20in%20your%20database%20and%20how%20you%20can%20load%20it%20to%20PostGIS.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSPAN%3EWhat%20can%20you%20do%20with%20PostGIS%3F%3C%2FSPAN%3E%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EWhereas%20normal%20PostgreSQL%20offers%20you%20functions%20that%20work%20for%20example%20with%20math%20(sqrt()%2C%20round())%2C%20strings%20(char_length()%2C%20substring())%20or%20data%20types%2C%20PostGIS%20supplements%20those%20with%20hundreds%20of%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2Fdocs%2FPostGIS_Special_Functions_Index.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Espatial%20functions%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E.%20These%20functions%20make%20PostGIS%20a%20real%20data%20analysis%20powerhouse%20and%20can%20enable%20you%20to%20do%20everything%20from%20point%20data%20clustering%20to%20geometry%20manipulation.%20PostGIS%20uses%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDE-9IM%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EDE-9IM%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bmodel%20to%20describe%20the%20spatial%20relations%20of%20geographic%20objects.%26nbsp%3B%20I%20will%20introduce%20some%20of%20those%20in%20later%20blog%20posts%20of%20this%20series.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOne%20really%20powerful%20feature%20of%20PostGIS%20are%20spatial%20joins.%20From%20normal%20SQL%20you%20are%20probably%20familiar%20with%20the%20process%20of%20joining%20tables%2C%20but%20there%20are%20cases%20where%20the%20only%20common%20nominator%20of%20two%20tables%20is%20their%20location.%20As%20an%20example%20you%20might%20have%20a%20table%20with%20customer%20locations%20as%20points%20and%20another%20table%20with%20shop%20locations%20as%20areas.%20With%20a%20spatial%20join%20you%20can%20add%20new%20information%20to%20the%20customer%20data%20about%20their%20closest%20shop.%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fcarto.com%2Fblog%2Flateral-joins%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EInnovative%20use%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bof%20spatial%20joins%20can%20help%20you%20to%26nbsp%3B%20answer%20very%20complex%20questions.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ERouting%20is%20one%20really%20traditional%20GIS%20problem.%20With%20another%20PostgreSQL%2FPostGIS%20extension%20(also%20available%20on%20Azure%20PostgreSQL)%20pgRouting%20and%20network%20data%20you%20can%20find%20optimal%20routes%20and%20do%20different%20network%20analytics.%20Networks%20can%20be%20any%20kind%20of%20topologically%20connected%20vector%20data%2C%20but%20most%20typically%20you%20work%20with%20road%20geometries.%20You%20can%20find%20very%20good%20pgRouting%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fworkshop.pgrouting.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Eworkshop%20materials%20online.%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20700px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F166719iD978BFA749EE53F4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22train_voronoi.gif%22%20title%3D%22train_voronoi.gif%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EIn%20one%20project%20I%20combined%20real-time%20train%20GPS%20location%20data%20in%20PostGIS%20ST_Voronoi%20analysis.%20Thus%20the%20areas%20sow%20the%20nearest%20train%20in%20each%20location%20around%20Finland.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20one%20project%20I%20combined%20real-time%20train%20GPS%20location%20data%20in%20PostGIS%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2Fdocs%2FST_VoronoiPolygons.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EST_Voronoi%3C%2FA%3E%20function.%20Th%3C%2FSPAN%3E%3CSPAN%3Eus%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Ethe%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eareas%20sow%20the%20nearest%20tr%3C%2FSPAN%3E%3CSPAN%3Eain%20in%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eeach%20location%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3Baround%20Finland.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSPAN%3EWhere%20to%20learn%20more%20and%20how%20to%20get%20started%3F%3C%2FSPAN%3E%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ECurrently%20Azure%20PostgreSQL%20is%20offering%20PostGIS%20version%202.5%20but%20just%20recently%20PostGIS%20version%203.0%20was%20released.%20I%20recommend%20trying%20it%20out%20and%20if%20you%20are%20familiar%20with%20PostgreSQL%20or%20SQL%20you%20will%20be%20up%20and%20running%20in%20no%20time.%20After%20a%20while%20you%20will%20also%20be%20able%20to%20distinguish%20your%20EPSG%3A3857%20from%20EPSG%3A4326%20and%20know%20the%20difference%20between%20ST_Contains%20and%20ST_Intersects.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3CBR%20%2F%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20have%20found%20the%20PostGIS%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2Fdocs%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Edocumentation%26nbsp%3B%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3Eto%20be%20a%20very%20good%20resource%20in%20general.%20You%20can%20check%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpostgis.net%2Fworkshops%2Fpostgis-intro%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Ethis%26nbsp%3B%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3Eextensive%20workshop%20material%20to%20get%20a%20good%20overview%20of%20PostGIS%20in%20action.%26nbsp%3B%20If%20you%20want%20to%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eimprove%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3Eyour%20spatial%20SQL%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3Bskills%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3Bin%20practice%2C%20I%20recommend%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Flearn.crunchydata.com%2Fpostgis%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Ethis%26nbsp%3B%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3Einteractive%20playground%20by%20Crunchy%20Data.%20I%20have%20written%20a%20longer%20blog%20post%20about%20PostGIS%20on%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fmedium.com%2F%40tjukanov%2Fwhy-should-you-care-about-postgis-a-gentle-introduction-to-spatial-databases-9eccd26bc42b%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EMedium%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bdirected%20more%20towards%20people%20coming%20from%20GIS%20background%20with%20no%20RDBMS%20experience.%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20personally%20work%20with%20PostGIS%20and%20FOSS4G%20(%3D%20Free%20Open%20Source%20Software%20for%20Geospatial)%20at%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.gispo.fi%2Fen%2Fhome%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3EGispo%20Ltd%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bin%20Finland.%20I%20will%20write%20here%20a%20few%20blogs%20about%20PostGIS%20from%20different%20perspectives%2C%20so%20stay%20tuned!%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1124288%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22TextRun%20SCXW48480182%20BCX0%22%3E%3CSPAN%20class%3D%22NormalTextRun%20SCXW48480182%20BCX0%22%3EYour%20PostgreSQL%20database%20might%20have%20spatial%20data%20in%20it%20.You%20just%20might%20not%20know%20it%20yet.%26nbsp%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22EOP%20SCXW48480182%20BCX0%22%3E%26nbsp%3BPostGIS%2C%20which%20is%20an%20open%20source%20spatial%20database%20extender%20for%20PostgreSQL%2C%20is%20a%20really%20powerful%20tool%20for%20spatial%20data%20analysis.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1124288%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Egeo%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Egeospatial%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Egis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgres%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgresql%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Regular Visitor

Spatial data comes in many shapes (sic) and sizes. Location information can be something more abstract that doesn’t look like spatial data at first (an IP-address, a placename inside a string) or it can be something more concrete (an address, coordinates) that is quite straightforward to turn in to geographic objects.  

 

To conduct spatial analysis with PostgreSQL you need to be able to handle geographic objects and to be able to handle geographic objects, you need the PostGIS extension.  

 

If you set up a hosted PostgreSQL database on Azure and run the following query: 
 

SELECT * FROM pg_available_extensions;

This will return a list of 39 extensions and among those are multiple spatial extensions. Among those is PostGIS. To install PostGIS to your database, you only need the following query: 

 

CREATE EXTENSION postgis;

After a few seconds this enables PostGIS and you might notice that a table spatial_ref_sys appears to your public schema. This table lists over 3000 known spatial reference systems and details needed to transform and reproject between them. Because the Earth is not flat (sorry to break it to you here), we have to deal with various coordinate systems to project things happening on this somewhat round planet to flat surfaces like your computer screen. 

 

Working with spatial data in PostGIS  

 

As a disclaimer, I have to note at this point that working with spatial data is not always very straightforward. That is why there are people (like me) specialized in that. Traditionally the field has been called GIS (see, PostGIS!). Nowadays GIS (Geographic Information Systems) is not just information systems, but it has evolved to things like location intelligence, spatial data analytics or spatial data science. Understanding the basics of spatial data is becoming more important in all subdomains of data analysis. However you might label it, we are still talking about the same key question: where something is happening and what does that mean? 
 

Basic formats of spatial data in vector format are points, lines and polygons. You can read a very basic level explanation of data formats and GIS in general from mapschool.io or if you wish to dive deeper into the topic, I strongly recommend getting familiar with a basic GIS book. Points are formed from a pair of coordinates, lines are made from connected points and polygons are made from connected lines.  

clipboard_image_0.png

 

Points, lines and polygons. That is a good starting point when trying to understand spatial data.  Credit: mapschool.io 

 

In PostGIS points (POINT type), lines (LINESTRING) and polygons (POLYGON) can also be MULTI-type with multiple geometries in one, or a geometry collection that can contain different geometry types inside one cell. The coordinate system of the data is defined by spatial reference identifier (SRID). In addition to vector data, PostGIS can also store and analyze raster data.  

 

The basis for the normal PostGIS geometry type is a plane and the shortest path between two points on the plane is a straight line. That means calculations on geometries (areas, distances, lengths, intersections, etc) can be calculated using cartesian mathematics and straight line vectors. PostGIS also has a geography data type, which provides native support for spatial features represented on "geographic" coordinates (sometimes called "geodetic" coordinates, or "lat/lon", or "lon/lat"). But you can also work with “lat/lon” data using the normal geometry data type.  
 

The OpenGIS specification which PostGIS is based on, defines two standard ways of expressing spatial objects: the Well-Known Text (WKT) form and the Well-Known Binary (WKB) form. Both of these include information about the type of the object and the coordinates which form the object. 

 

So in a PostGIS database a point geometry could be represented in multiple ways. If I run the following query in my PostGIS database that has a spatial table with cities: 

 

SELECT geom FROM ne_50m_populated_places WHERE name =  'Seattle' 

 

It returns the following binary representation for the point that is the location of Seattle: 

0104000020E6100000010000000101000000EC1FEA31E2955EC0E0AAD59635C94740

But if I use the function ST_AsEWKT, the same point  

SRID=4326;MULTIPOINT(-122.341930845868 47.5719479125307)

So the SRID 4326 shows us that the data is in coordinate system called WGS84 - World Geodetic System 1984, used also in GPS. MULTIPOINT means that the data type is a multi-type although there is only one point and the numbers in parenthesis are latitude and longitude of Seattle. This particular point has way too many decimals in the coordinates when you think about the fact that it is showing where Seattle is. Understanding the concepts of scale and accuracy are key when working with spatial data. 

 

clipboard_image_1.png

 

You will find this funny at some point. I promise. Credit: xkcd 
 

Spatial indexes are also one thing that works slightly differently with spatial data than other PostgreSQL data types. Without an index any search for a feature would require a sequential scan of every record in the database and this also applies for spatial data. Indexing speeds up searching by organizing the data into a search tree which can be quickly traversed to find a particular record. Whereas standard database indexes create a hierarchical tree based on the values of the column being indexed, spatial indexes are unable to index the geometric features themselves and instead index the bounding boxes of the features. 

 

PostGIS uses R-Tree spatial index structure. R-Trees break up data into rectangles (bounding boxes), and sub-rectangles, and sub-sub rectangles, etc. It is a self-tuning index structure that automatically handles variable data density and object size. 
 

These are just a few examples that are specific when working with spatial data. In the next blog post I will go through where you can find spatial data if you don’t already happen to have some in your database and how you can load it to PostGIS.  

 

What can you do with PostGIS? 

 

Whereas normal PostgreSQL offers you functions that work for example with math (sqrt(), round()), strings (char_length(), substring()) or data types, PostGIS supplements those with hundreds of spatial functions. These functions make PostGIS a real data analysis powerhouse and can enable you to do everything from point data clustering to geometry manipulation. PostGIS uses DE-9IM model to describe the spatial relations of geographic objects.  I will introduce some of those in later blog posts of this series.  
 

One really powerful feature of PostGIS are spatial joins. From normal SQL you are probably familiar with the process of joining tables, but there are cases where the only common nominator of two tables is their location. As an example you might have a table with customer locations as points and another table with shop locations as areas. With a spatial join you can add new information to the customer data about their closest shop. Innovative use of spatial joins can help you to  answer very complex questions. 

 

Routing is one really traditional GIS problem. With another PostgreSQL/PostGIS extension (also available on Azure PostgreSQL) pgRouting and network data you can find optimal routes and do different network analytics. Networks can be any kind of topologically connected vector data, but most typically you work with road geometries. You can find very good pgRouting workshop materials online. 

 

In one project I combined real-time train GPS location data in PostGIS ST_Voronoi analysis. Thus the areas sow the nearest train in each location around Finland.In one project I combined real-time train GPS location data in PostGIS ST_Voronoi analysis. Thus the areas sow the nearest train in each location around Finland.

 

In one project I combined real-time train GPS location data in PostGIS ST_Voronoi function. Thus the areas sow the nearest train in each location around Finland. 
 

Where to learn more and how to get started? 
 

Currently Azure PostgreSQL is offering PostGIS version 2.5 but just recently PostGIS version 3.0 was released. I recommend trying it out and if you are familiar with PostgreSQL or SQL you will be up and running in no time. After a while you will also be able to distinguish your EPSG:3857 from EPSG:4326 and know the difference between ST_Contains and ST_Intersects.  
 

I have found the PostGIS documentation to be a very good resource in general. You can check this extensive workshop material to get a good overview of PostGIS in action.  If you want to improve your spatial SQL skills in practice, I recommend this interactive playground by Crunchy Data. I have written a longer blog post about PostGIS on Medium directed more towards people coming from GIS background with no RDBMS experience. 

 

I personally work with PostGIS and FOSS4G (= Free Open Source Software for Geospatial) at Gispo Ltd in Finland. I will write here a few blogs about PostGIS from different perspectives, so stay tuned!