In my business (weather) we use lots map based (Geo) information. Almost every table has latitude and longitude. Working with this kind of data can be exciting and frustrating. This should give you a quick start into GIS with MySQL.
“A geographic information system (GIS), or geographical information system, is any system that captures, stores, analyzes, manages, and presents data that are linked to location. In the simplest terms, GIS is the merging of cartography, statistical analysis, and database technology. GIS systems are used in cartography, remote sensing, land surveying, utility management, natural resource management, photogrammetry, geography, urban planning, emergency management, navigation, and localized search engines.” – Wikipedia
GIS / Mapping Systems work with both text data and graphical data. Applications and utilities often blur the lines between the two types and make understanding difficult. Map servers blend raster images, with point or polygon data, and bitmap images to make complete images to display in the user’s client application. For this post I will concentrate on the text type “data”. The type we can index in a MySQL database.
THE SEARCH
After months of reading, [1] I’m writing this post to describes what I have learned about how to get started using GEO coding data as quickly as possible. I found very little piratical information on GIS and MySQL. The MySQL manual covers the functions but doesn’t supply much practical information on GEO. Anders Karlsson wrote a nice and short story about GIS that give me a good start.
The best information has be written by Florin Duroiu in his post titled “Political boundaries overlay in Google maps”. A good part of my post is based on his work.
STEP BY STEP
Below are the detailed needed to to produce a MySQL database with the Points of Interest (POI). This is based on CentOS 5.5 with MySQL 5.1.
yum install gdal mkdir geo mkdir data mkdir data/Oklahoma cd geo/data/Oklahoma wget http://downloads.cloudmade.com/north_america/united_states/oklahoma/oklahoma.shapefiles.zip unzip oklahoma.shapefiles.zip mysql -e 'create database geo' ogr2ogr -f "MySQL" MySQL:"geo,user=root,host=localhost,password=" -nln oklahoma_poi -lco engine=MYISAM oklahoma_poi.shp mysql geo -e 'desc oklahoma_poi' +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | OGR_FID | int(11) | NO | PRI | NULL | auto_increment | | SHAPE | geometry | NO | MUL | NULL | | | category | varchar(30) | YES | | NULL | | | name | varchar(113) | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+ mysql geo -e "select name, category, Y(SHAPE) as lat, X(SHAPE) as lng from oklahoma_poi where NAME like 'School:Putnam%'" +-------------------------------------+--------------------------------+------------+-------------+ | name | category | lat | lng | +-------------------------------------+--------------------------------+------------+-------------+ | School:Putnam City West High School | Government and Public Services | 35.492557 | -97.6605975 | | School:Putnam City North School | Government and Public Services | 35.5892209 | -97.6372648 | | School:Putnam City School | Government and Public Services | 35.5122794 | -97.6142079 | | School:Putnam High School | Government and Public Services | 35.5214459 | -97.6086523 | | School:Putnam Heights Academy | Government and Public Services | 35.5081143 | -97.5397619 | +-------------------------------------+--------------------------------+------------+-------------+
In a coming set of post I’ll go over:
- The “Data” types your will find and how to convert between them.
- What data is available and where can you find it?
- More examples on what you can do with GIS data.
- Viewing our GIS data.
- How to collect your own GIS data.
- Good and bad examples of searching GIS data.
- Optimizing MySQL GIS. Is it really worth using?
[1] Books: GIS for Dummies – Author: Michael N. DeMers – John Wiley & Sons (2009) – ISBN: 0470236825
Open Source GIS: A GRASS GIS Approach. Third Edition.- Author: Markus Neteler and Helena Mitasova – ISBN: 978-0-38735767-6
Web Mapping Illustrated: Using Open Source GIS Toolkits – Author: Tyler Mitchell – ISBN: 9780596008659
Tweet
Rob Wultsch wrote:
Why do you suggest using MySQL over PostGIS?
Link | August 24th, 2010 at 5:45 pm
mark wrote:
I’m not suggesting using MySQL over PostGIS. For now and for some time to come, PostGIS is the king. But if you have data in MySQL, why not use the GIS functions it has. Trying to find and sort data on latitude and longitude columns may not be the way to go.
I’m working on quantifying what MySQL’s GIS functions are good for and when and how they should be used.
Link | August 25th, 2010 at 10:05 am
Jason wrote:
hi mr mark, I trying to do something
I have some polygons how I can determine if a point of coordinates x, y is inside the polygon?
a table with a field polygon = “[GEOMETRY - 97B]” (coordinates are in a database format)
and a car with coordinates x = lat, long and
how to know if this is in a polygon?
Link | April 3rd, 2012 at 5:14 pm
Geo Coding on the Cheap | MySQL Fanboy wrote:
[...] then LatLng “point data”, it can also describe geometric shapes. All of these can be encoded into (spatial reference system identifier (SRID)) a MySQL database [...]
Link | September 18th, 2012 at 4:34 pm