{"id":665,"date":"2010-01-29T14:54:23","date_gmt":"2010-01-29T13:54:23","guid":{"rendered":"http:\/\/sickel.net\/blogg\/?p=665"},"modified":"2010-01-30T12:23:50","modified_gmt":"2010-01-30T11:23:50","slug":"setting-up-a-mysql-database-for-spatial-data","status":"publish","type":"post","link":"https:\/\/sickel.net\/blogg\/?p=665","title":{"rendered":"Setting up a mysql database for spatial data"},"content":{"rendered":"<p>I would prefer to use <a href=\"http:\/\/postgis.refractions.net\/\">postgis<\/a>, but have to resign to mysql. I want to create a table with spatial information and be able to access it from mapserver. As I am going to store a lot of datapoints, they may be stored just as two columns with x and y values, but as the dataset grows, that starts to be a bit slow. <\/p>\n<p>I started of with a table with more than 20 columns holding point measurements and a lot of metadata related to those measurements. No, it is not practical to split that table up. I tried to add a point column to that table<\/p>\n<p>alter table mobile_rawdata add column coord point <\/p>\n<p>and then I copied the coordinates into that column<\/p>\n<p>update rawdata set coord=point(lon,lat) where not lat is null;<\/p>\n<p>again fine and dandy, but when I tried to put a spatial index on that column, I was told that that was not possible for a column with null values. <\/p>\n<p>So I made a new table<\/p>\n<p>create table mobile_spatial(<br \/>\nOGR_FID serial primary key,<br \/>\nSHAPE geometry not null,<br \/>\nrawdataid integer not null,<br \/>\naddtime timestamp default current_timestamp);<\/p>\n<p>note that the shape column is to be of type geometry, if it is set to another spatial type, ogr will not recognice it. The column names OGR_FID and SHAPE are used as those are default values for ogr. Rawdataid pointing to the id of the old mobile_rawdata table. <\/p>\n<p>Then I tested it with  <\/p>\n<p>ogrinfo -so MySQL:radioecology_9,user=user,password=pass mobile_spatial<\/p>\n<p>but ogrinfo still did not understand that this was supposed to be a table with geometry. Reading up a bit more, I found that I needed two more tables in the database: SPATIAL_REF_SYS to store the, well, spatial reference systems and geometry_columns to store information on what kind of geometry the tables had.<\/p>\n<p>I didn&#8217;t manage to find a description for those tables but according to <a href=\"http:\/\/www.gdal.org\/ogr\/drv_mysql.html\">gdal.org<\/a> they will be created when a shape file is imported using ogr2ogr. Trying to import a shape file, I had them created:<\/p>\n<p>CREATE TABLE  geometry_columns (<br \/>\n  `F_TABLE_CATALOG` varchar(256) DEFAULT NULL,<br \/>\n  `F_TABLE_SCHEMA` varchar(256) DEFAULT NULL,<br \/>\n  `F_TABLE_NAME` varchar(256) NOT NULL,<br \/>\n  `F_GEOMETRY_COLUMN` varchar(256) NOT NULL,<br \/>\n  `COORD_DIMENSION` int(11) DEFAULT NULL,<br \/>\n  `SRID` int(11) DEFAULT NULL,<br \/>\n  `TYPE` varchar(256) NOT NULL<br \/>\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;<\/p>\n<p> and<\/p>\n<p>CREATE TABLE  spatial_ref_sys (<br \/>\n  `SRID` int(11) NOT NULL,<br \/>\n  `AUTH_NAME` varchar(256) DEFAULT NULL,<br \/>\n  `AUTH_SRID` int(11) DEFAULT NULL,<br \/>\n  `SRTEXT` varchar(2048) DEFAULT NULL<br \/>\n) ENGINE=InnoDB DEFAULT CHARSET=latin1;<\/p>\n<p>the important column in the latter is the SRTEXT, eg for standard WGS84 lat lon, that is <\/p>\n<p>GEOGCS[&#8220;WGS 84&#8221;, DATUM[&#8220;WGS_1984&#8221;,SPHEROID[&#8220;WGS 84&#8221;, 6378137, 298.257223563, AUTHORITY[&#8220;EPSG&#8221;,&#8221;7030&#8243;]], AUTHORITY[&#8220;EPSG&#8221;,&#8221;6326&#8243;]], PRIMEM[&#8220;Greenwich&#8221;,0,AUTHORITY[&#8220;EPSG&#8221;,&#8221;8901&#8243;]], UNIT[&#8220;degree&#8221;, 0.01745329251994328,<br \/>\nAUTHORITY[&#8220;EPSG&#8221;,&#8221;9122&#8243;]], AUTHORITY[&#8220;EPSG&#8221;,&#8221;4326&#8243;]]<\/p>\n<p>the SRID field is to be pointed to by the SRID in the geometry_column table. <\/p>\n<p>The geometry_column table has to have the following values:<br \/>\nF_TABLE_NAME : Name of table<br \/>\nF_GEOMETRY_COLUMN: Column in table that holds the geometrical information<br \/>\nCOORD_DIMENTION: Dimentionallity of dataset, usually 2<br \/>\nSRID : pointer to the spatial_ref_sys table<br \/>\nTYPE: Type of geometry in the geometry colum, e.g. POINT or POLYGON<br \/>\nthe last to columns may be blank.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I would prefer to use postgis, but have to resign to mysql. I want to create a table with spatial information and be able to access it from mapserver. As I am going to store a lot of datapoints, they &hellip; <a href=\"https:\/\/sickel.net\/blogg\/?p=665\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20],"tags":[],"class_list":["post-665","post","type-post","status-publish","format-standard","hentry","category-mapserver"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnVtD-aJ","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=665"}],"version-history":[{"count":7,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/665\/revisions"}],"predecessor-version":[{"id":672,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/665\/revisions\/672"}],"wp:attachment":[{"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}