Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

M|18 Building Location-Based Services with Geospatial Data


Published on

M|18 Building Location-Based Services with Geospatial Data

Published in: Data & Analytics
  • Be the first to comment

  • Be the first to like this

M|18 Building Location-Based Services with Geospatial Data

  1. 1. Building Location Based Services What about adding a new dimension to your data? Anders Karlsson Principal Sales Engineer, MariaDB
  2. 2. Location based services and GIS Do you know all there is to know about your business from your data? What if you could add another dimension to that data? What if that dimension could be used to bring a new way to use and view your data? And what if that dimension could come more or less free?
  3. 3. Agenda ● GIS concepts explained ● GIS data in Databases ○ Datatypes ○ Functions ○ Indexing ● Practical examples of using Location Data in Databases
  4. 4. An introduction to GIS
  5. 5. GIS Systems and GIS Data ● GIS systems are systems that specialized in GIS applications ○ The leader in this field is ESRI ○ Research ○ Mapping ○ Exploration ● Today, with GPS receivers in almost every car and cellphone this is changing ○ Navigation ○ Mapping objects on maps ○ GIS data is become an important components of social networks
  6. 6. GIS objects ● A GIS object is something that is mapped in coordinates on some kind of map or grid ○ A point ○ A line ○ A polygon ■ There are numerous specialized polygons of course. Square, rectangle etc. ○ Other geometric shapes
  7. 7. GIS Surfaces ● The surface the GIS objects are mapped on can be anything ○ A simple 2d map ○ The earth ● The above are the most common uses ○ Of course a map is a subset of the earth, but many maps reflects such a small part of the earth so it can be simplified as being flat ○ Note: If your earth is flat, many assumptions made on this and the following slides are largely incorrect
  8. 8. Latitude and Longitude Latitude 0 Longitude 0 40.715088, -74.015289
  9. 9. GIS Terms
  10. 10. GIS Terms – WKB and WKT ● WKT and WKB are standard GIS formats for representing GIS shapes (up to 4 dimensions) ● WKT – Well Known Text ● WKB – Well Known Binary ● Both are standardized by the Open GIS Consortium (OGC) and is used by most database systems to represent GIS data
  11. 11. GIS Terms - MBR ● MBR (Minimum Bounding Rectangle) - A rectangular shape that is the smallest one to encompass some other shape ● In the illustration to the right ○ Point A is within the Polygon and within the MBR of the Polygon ○ Point B is within the MBR of the Polygon but not within the Polygon itself MBR POLYGON A B
  12. 12. GIS Terms - SRID ● SRID (Spatial Reference Identifier) is what defines the Grid where we place our shapes ○ In other words, the SRID defines how we map our flat map onto a curved reality, such as the earth ○ There are several different LAT/LONG mappings ● There is also a “flat” or Euclidian SRID ○ This is what is used by MariaDB
  13. 13. GIS in databases
  14. 14. GIS Database types ● The supported shapes are arranged in a hierarchy ○ Top level is GEOMETRY ● There is also support for collections of shapes ○ MULTIPOLYGON for example
  15. 15. GIS Database types ● GIS datatypes are organized in a hierarchy (see below) ● To store any kind of GIS data, the type to use is GEOMETRY ● To create an index of a GIS datatype column, you create a SPATIAL INDEX ○ Note that you can also use BTREE indexes on GIS data, but these are usually only good for exact matches and are far from as powerful as a SPATIAL index GEOMETRY POINTPOLYGONLINESTRING COLLECTION MULTILINESTRING MULTIPOLYGON MULTIPOINT
  16. 16. GIS data indexing ● Rtree (Rectange Tree) - A way to index GIS shapes by creating a tree-like hierarchy using the MBR of the shape ● Without Rtree indexes, we would be limited to exact location searches for GIS data in databases ● Using Rtree indexes we can index operations such as ○ Find all shapes that are contained within another shape ○ Find all shapes that overlaps a given shape ○ Find all lines that crosses a given polygon R2 R1 R6 R3 R4 R7 R5 R1 R2 R3 R4 R5 R6 R7
  17. 17. Creating a data tables containing GIS data CREATE TABLE state(state VARCHAR(2) NOT NULL PRIMARY KEY, name VARCHAR(25) NOT NULL, state_fips VARCHAR(2) NOT NULL, shape GEOMETRY NOT NULL, UNIQUE(state_fips), SPATIAL INDEX(shape)); CREATE TABLE road(id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(120) NOT NULL, state VARCHAR(2) NOT NULL, shape GEOMETRY NOT NULL, length DOUBLE, SPATIAL INDEX(shape), FOREIGN KEY(state) REFERENCES state(state));
  18. 18. GIS Functions ● There are three groups of GIS functions ○ Constructors, for example ■ POINT to create a POINT ■ POINTFROMTEXT to create a POINT from a textstring ○ Geometry relations, for example ■ CONTAINS to determine of one geometry contain another ■ OVERLAPS to determine of one geometry overlaps another ○ Geometry attributes, for example ■ GEOMETRYN – Get a specific geometry from a GEOMETRYCOLLECTION ■ AREA – Get the area of a POLYGON ■ CENTROID – Get the mathematical center of a POLYGON or MULTIPOLYGON
  19. 19. GIS Data – Getting practical
  20. 20. GIS Data goes to work ● Mapping ○ Showing locations on maps ○ Showing venues given some condition and a current location ● Locating ○ Locating people ○ Finding the location of a car accident ○ Finding houses for sale in the current area ● Big data analytics, Visualization ○ Find trends in geographies ○ Where do my customers come from ○ Where do my customers NOT come from ● The use cases are many more than used to be the case ○ Cellphones have GPS receivers ○ GPS Location can be retrieved by a an application
  21. 21. Populating GIS Data ● There is a wealth of free GIS data available ○ Government ○ Research ○ Other sources ● The most data you find is find is, regrettably, in shape or .shp files ● The good thing is that the shape format is, for the most part, open and documented ○ ESRI, the leader in Geospatial applications, created and develops the shp format ○ For Linux, you will find GDAL (Geospatial Data Abstraction Library) real helpful ○ GDAL is available in Linux repositories and contains libraries and tools ○ Among them is ogr2ogr, a Geospatial data format converter ○ Note that you need all the files in a set, not just the shp file! $ ogr2ogr -f MySQL MySQL: "db,host=h1,user=anka,port=3306" -nln roads -a_srs "EPSG:4326" roads.shp -lco ENGINE=InnoDB
  22. 22. Querying GIS data MariaDB> SELECT, s.state_name FROM roads r JOIN states s ON CROSSES(r.shape, s.shape) WHERE LIKE 'US Route 101%' GROUP BY, s.state_name ORDER BY MIN(Y(CENTROID(ENVELOPE(r.shape)))); +-----------------------------+------------+ | name | state_name | +-----------------------------+------------+ | US Route 101, State Route 1 | California | | US Route 101 | California | | US Route 101 | Oregon | | US Route 101 | Washington | +-----------------------------+------------+ Get all states that US 101 passes through, ordered from south to north
  23. 23. Querying GIS data MariaDB> SELECT s.state_name, SUM(r.length) length FROM roads r JOIN states s ON CROSSES(r.shape, s.shape) WHERE LIKE 'US Route 101%' GROUP BY s.state_name ORDER BY MIN(Y(CENTROID(ENVELOPE(r.shape)))); +------------+--------+ | state_name | length | +------------+--------+ | California | 1.770 | | Oregon | 2.276 | | Washington | 3.196 | +------------+--------+ Get the length of US 101 in all the states that is passes through, ordered from south to north
  24. 24. Querying GIS data MariaDB> SELECT state_name FROM states WHERE contains(shape, PointFromText('POINT(-73.985170 40.758902)')); +------------+ | state_name | +------------+ | New York | +------------+ In which state is Times Square?
  25. 25. Conclusion ● GIS data adds a new dimension to your data ● Combining GIS data with your business data provides valuable insights ● Using GIS data provides a base for new ways of visualize data ● There is a lot of free GIS data out there, for you to use ● GIS data fits nicely into a relational structure and is fully supported by MariaDB ○ Functions ○ Data types ○ Indexing ○ GeoJSON
  26. 26. Resources and further reading ● MariaDB GIS Features reference ○ Knowledge base: ○ Future plans: ● Open GIS Consortium (OGC) – Simple Features Access SQL ○ Reference: ● R-Tree indexes ○ Wikipedia: ○ MariaDB Knowledge Base: ● MariaDB GIS Datatypes ○ MariaDB Knowledge Base: ● Google maps API ○ Google:
  27. 27. Thank you! Building Location Based Services Anders Karlsson Principal Sales Engineer, MariaDB anders