Pins Polygons and Perspectives - WordPress.com · Pins Polygons & Perspectives About Accenture...
Transcript of Pins Polygons and Perspectives - WordPress.com · Pins Polygons & Perspectives About Accenture...
Pins Polygons & Perspectives1
Pins Polygons and Perspectives Visualizing Geographic Data in APEX
Christoph Ruepprich Accenture Enkitec Group
Pins Polygons & Perspectives
Who Am I?
● Dad & Husband ● Consultant @ Accenture an Enkitec Group ● DBA/Developer ● Fitness ● Bass player ● Board gamer
2
ruepprich.wordpress.com
@CRuepprich
cruepprich
Pins Polygons & Perspectives
About Accenture Enkitec Group
● Result of acquisition of Enkitec by Accenture in May 2014 ● Global systems integrator focused on the
Oracle platform ● Headquartered in Dallas, Texas ● Consultants average 15+ years of Oracle
experience ● ~20 Oracle ACE members on staff
3
Pins Polygons & Perspectives
What is covered?
● Geocoding ● Mapping APIs ● Interacting with database ● Pins and Polygons ● Third party data
44
Pins Polygons & Perspectives
What’s Covered
● Geolocation ● Map APIa ● Rendering Maps ● Third Party Data ● Relationships
5
Pins Polygons & Perspectives
First we need…
6
Data!Turn addresses into geographic coordinates
Pins Polygons & Perspectives
Coordinates
7
Pins Polygons & Perspectives
Layers use Coordinates
● Pins ● Polygons ● Lines ● Routes ● etc.
8
Pins Polygons & Perspectives
Geocoding Webservices
● Find coordinates from and address
9
Reverse Geocoding● Find address from coordinates
Pins Polygons & Perspectives10
Geocoding Webservices
Pins Polygons & Perspectives
Geocoding with
● Uses SQL!
11
https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="1400 6th Ave, Seattle, WA 98101"https://query.yahooapis.com/v1/public/yql?q=select * from geo.placefinder where text="1400 6th Ave, Seattle, WA 98101"
● Output XML or JSONhttps://query.yahooapis.com/v1/public/yql?format=json&q=select * from geo.placefinder where text="1400 6th Ave, Seattle, WA 98101"
https://query.yahooapis.com/v1/public/yql?format=json&q=select * from geo.placefinder where text="1400 6th Ave, Seattle, WA 98101"
https://developer.yahoo.com/yql/guide/usage_info_limits.html
Pins Polygons & Perspectives
Geocoding with
12
Sample XML result
Pins Polygons & Perspectives
Geocoding with
13
● Other queries
select * from local.search where zip='76051' and query='hair cut'
select * from weather.forecast where woeid=2502265
Pins Polygons & Perspectives
Geocoding with
● Use maps.googleapis.com
14
http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=1400 6th Ave, Seattle, WA 98101http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address=1400 6th Ave, Seattle, WA 98101
json?
Pins Polygons & Perspectives
Geocoding with
15
Sample result:
Pins Polygons & Perspectives
Reverse Geocoding with
● Use maps.googleapis.com
16
http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452
json?
Pins Polygons & Perspectives
ReverseGeocoding with
17
Pins Polygons & Perspectives
Geocoding with
● Apex Plug-in ● Creates collection ● Results in report on collection ● Good with PL/SQL
18
Pins Polygons & Perspectives
Which geocoding service do I use?
● Many other services available ● Check cost and usage limits ● Check response times ● Bulk processing ● Verify results
19
Pins Polygons & Perspectives20
Processing the Results
Pins Polygons & Perspectives
What to do with the results
● Store XML/JSON result in clob ● Parse result and loop through arrays ● Store results in table or collection
and/orRender on map
21
Basic Steps:
Pins Polygons & Perspectives
What to do with the results
● XML - Use Oracle’s built in XML functions ● Good when processing results with PL/SQL
● JSON - ● Good with JavaScript ● PLJSON package (sourceforge)
http://sourceforge.net/projects/pljson/My Blog: http://wp.me/pRyVI-k1
22
Pins Polygons & Perspectives23
We have coordinates.
Pins Polygons & Perspectives
Mapping with● Optional Free API Key - Get usage stats ● Extensible ● Usage Limit: 25k map loads per day
(don’t quote me on that) ● Excellent Documentation ● API Reference ● Examples
24
https://developers.google.com/maps/
Pins Polygons & Perspectives
Mapping with● You need
● Javascript Library ● API Key (optional)
25
Pins Polygons & Perspectives
Mapping with● Requires Application Key (Free) ● Extensible ● Good Documentation / Examples
26
https://developer.yahoo.com/maps/flash/jsGettingStarted.html
https://developer.yahoo.com/maps/ajax/v2start.html
Pins Polygons & Perspectives
Mapping with● You need
● Javascript Library ● API Key
27
Pins Polygons & Perspectives
Mapping with
● Sample Geolocation Showcase (Apex 4.2.5) ● Mapping through Plug-Ins ● See Carsten Czarski’s Presentation:
Location Data for Everyone - in APEX 4.2.5. What the Sample Geolocation Showcase can do for you. ODTUG Webinars
28
Pins Polygons & Perspectives
Mapping with
● You Need ● Plug-Ins
29
Pins Polygons & Perspectives
Mapping with● Free API ● Lightweight ● Good for Mobile ● Use other tile servers ● Extensible ● Concise coding
30
http://leafletjs.com/
Pins Polygons & Perspectives
Mapping with● You need
● JS Library ● CSS Style Sheet
31
Pins Polygons & Perspectives
Tile Layers with
32
http://leaflet-extras.github.io/leaflet-providers/preview/
Pins Polygons & Perspectives
API Comparisons
● http://robinlovelace.net/software/2014/03/05/webmap-test.html
33
Pins Polygons & Perspectives
How to display Javascript maps
● HTML Region with <div> <div id=“map” style=“min-height:500px;”>!
● Javascript ● Include libraries ● Renders map in div
34
Pins Polygons & Perspectives
How to display Oracle maps
● Map Region Plugin ● Marker Plugins
35
Pins Polygons & Perspectives36
Demo Simple Map
Pins Polygons & Perspectives37
Interacting with Map and Database
Pins Polygons & Perspectives
Interacting with the Database
38
Pins Polygons & Perspectives
Data Example
39
ID Brand Latitude Longitude
1 Shell 32.34321 -97.398834
2 Valero 32.15534 -97.19984
3 Exxon 33.92234 -94.88223
Gas Stations
Pins Polygons & Perspectives
Convert to JSON
40
ID Brand Latitude Longitude
1 Shell 32.34321 -97.398834
2 Valero 32.15534 -97.19984
3 Exxon 33.92234 -94.88223
Gas Stations
{ "stations":[ { "id":"1", "brand":"Shell", "lat":32.343214, "lng":-97.398834 }, { "id":"2", "brand":"Valero", "lat":32.15534, "lng":-97.19984 }, { "id":"3", "brand":"Exxon", "lat":33.92234, "lng":-94.88223 } ] }P110_JSON
Page Item:
Pins Polygons & Perspectives
Convert to JSON with PL/SQL Loop
41
!begin for c1 in (select NAME, LAT, LNG from gas_stations) loop - -Build JSON string … end loop; - - Store string in page item :P110_JSON := l_json; — - For On Demand Process use - - htp.p(l_json); end;
• Page Process • On Demand Process
Store large JSON objects in collection CLOB.
Pins Polygons & Perspectives
Loop Through JSON
42
var json = JSON.parse($v('P110_JSON')); //json contains “stations” object var gs = json.stations;
for (var i=0;i<gs.length;i++){ var latlng = new L.LatLng(gs[i].lat ,gs[i].lng); var marker = L.marker(latlng); marker.addTo(map); markersArr.push(marker); }
Pins Polygons & Perspectives
Keep Track of Markers/Polygons
43
markersArr[0].lat = 123 markersArr[0].lng = 456
markersArr[1].lat = 123 markersArr[1].lng = 456
Pins Polygons & Perspectives
Remove Markers/Polygons
44
for (var i=0;i<markersArr.length;i++){ //remove markers to the map using map.removeLayer(markersArr[i]); }
• Loop through markers array and remove each one from the map
Pins Polygons & Perspectives
Add Popup
● Add ‘on-click’ event listener to each marker
45
markersArr[0].bindPopup(‘Some html text’);
Pins Polygons & Perspectives
Circles
● Lat/Lng to define center ● Radius
46
Pins Polygons & Perspectives
Polygons
● Use arrays ● Add events ● Multiple points
47
var corners =[]; corners[0] = new L.LatLng(47.65521295468833 ,-122.41790771484375); corners[1] = new L.LatLng(47.663537612601345,-122.1734619140624); corners[2] = new L.LatLng(47.56170075451973 ,-122.18719482421874); corners[3] = new L.LatLng(47.56077405523748 ,-122.43301391601562); ! polygon = L.polygon(corners); ! polygon.addTo(map);
Pins Polygons & Perspectives48
Demo Gas Stations
Pins Polygons & Perspectives49
Third Party Data
Pins Polygons & Perspectives
WKT - Well Known Text
● Polygons can have thousands of points ● Complex geometries from 3rd party providers
50
Pins Polygons & Perspectives
WKT - Well Known Text
51
http://en.wikipedia.org/wiki/Well-known_text
Pins Polygons & Perspectives
WKT - Well Known Text
52
http://en.wikipedia.org/wiki/Well-known_text
Pins Polygons & Perspectives
Sample Data
● 177 Zip Codes ● 792 Polygons ● 52,060 Coordinates ● in under 2 seconds!
53
Pins Polygons & Perspectives54
Demo Zip Codes
Pins Polygons & Perspectives55
Finding Relationships
Pins Polygons & Perspectives
Oracle Locator/Spatial● Built into Oracle Database ● Functions and procedures to handle geometric
data ● SDO_GEOMETRY object Not supported with locator ● Advanced functions:
● Difference ● Intersection ● Union ● Etc.
56
Pins Polygons & Perspectives
Using third party WKT
Steps: ● Populate sdo_geometry column ● Set SRID (Spatial Reference ID) ● Set Metadata (USER_SDO_GEOM_METADATA) ● Create Index (MDSYS.SPATIAL_INDEX)
57
Pins Polygons & Perspectives
Convert WKT to SDO Geometry
58
WKT SDO Geometry
sdo_util.from_wktgeometry()
Pins Polygons & Perspectives
Zip Codes Table
59
ZIP WKT GEOM
76051 MULTIPOLYGON ((( MDSYS.SDO_GEOMETRY(2001,8307,MDSYS.SDO_POINT_TYPE(-97.094171,32.934568,NULL),NULL,NULL)
90210 MULTIPOLYGON ((( MDSYS.SDO_GEOMETRY(…
98101 MULTIPOLYGON ((( MDSYS.SDO_GEOMETRY(…
Pins Polygons & Perspectives
SDO_GEOMETRY
60
SDO_GEOMETRY( 2001 -- SDO_GTYPE: point ,4326 -- SDO_SRID (coordinate system) ,MDSYS.SDO_POINT_TYPE(lon, lat, NULL) -- POINT_TYPE ,NULL -- SDO_ELEM_INFO_ARRAY ,NULL -- SDO_ORDINATE_ARRAY )
● GTYPE: Geometry types (Points,Lines,Polygons, etc.)
● SRID: (MDSYS.CS_SRS) Coordinate system (2D,3D, Locale, etc.)
● POINT_TYPE: (x,y,z) ● ELEM_INFO_ARRAY: How to interpret ORDINATE_ARRAY. ● ORDINATE_ARRAY: Array of points
Pins Polygons & Perspectives
Calculate Distance
61
select SDO_GEOM.SDO_DISTANCE( MDSYS.SDO_GEOMETRY( 2001 , 4326 , MDSYS.SDO_POINT_TYPE(1, 1, NULL) , NULL , NULL ), MDSYS.SDO_GEOMETRY( 2001 , 4326 , MDSYS.SDO_POINT_TYPE(2, 2, NULL) , NULL , NULL ) ,1 -- within 1m tolerance ,'unit=m' -- meters ) AS dist_in_meters from dual; !
Pins Polygons & Perspectives
● Gas Stations within 5km ● SDO_WITHIN_DISTANCE
Calculate Distance
62
SELECT * FROM gas_stations g WHERE SDO_WITHIN_DISTANCE( g.geom , MDSYS.SDO_GEOMETRY( 2001 , 4326 , MDSYS.SDO_POINT_TYPE(- 97.078686, 32.939251, NULL) , NULL , NULL ) ,'distance = 5 unit=km' ) = 'TRUE'
Observer Point
Pins Polygons & Perspectives
Calculate Area
63
select SDO_GEOM.SDO_AREA( SDO_GEOMETRY( 2003 -- GTYPE: two-dimensional polygon ,4326 -- SRID ,NULL -- POINT TYPE ,SDO_ELEM_INFO_ARRAY(1,1003,1) ,SDO_ORDINATE_ARRAY(3,3, 6,3, 6,5, 4,5, 3,3) ) ,1) AREA from dual;
Pins Polygons & Perspectives
Contains
64
sdo_contains(polygon.geom,point.geom) = ‘TRUE’
Pins Polygons & Perspectives65
Demo Contains
Pins Polygons & Perspectives
Q&A
66
Q & A