Changes between Initial Version and Version 1 of VirtualSpatialData


Ignore:
Timestamp:
Jan 27, 2009, 3:51:44 PM (15 years ago)
Author:
jmckenna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • VirtualSpatialData

    v1 v1  
     1= Virtual Spatial Data =
     2 
     3This page is intended to describe how to set up spatial data from non-spatially aware datasources such as RDMSs without spatial engines. The basic assumptions are that the datasource is available from an ODBC connection, that the datasource contains two columns that can be mapped to a point (x and y), and that you are not afraid to figure things out.
     4
     5Note that this currently only works for POINT data.
     6
     7The basic components that you will need are! MapServer 4.0 or 4.1 compiled with a recent (as of 2004-01-06) CVS version of GDAL/OGR, a datasource that you have an ODBC driver for, and an ODBC driver manager.
     8
     9The quick steps are (more detailed steps including pitfalls are below):
     10
     11   1. create a DSN (ODBC datasource) for your data source and test it. How you do this is system and driver dependent, you have to either know how or figure it out yourself.
     12   2. test using ogrinfo
     13   3. create a virtual data file using the instructions provided at http://www.remotesensing.org/gdal/ogr/drv_vrt.html
     14   4. test using ogrinfo
     15   5. create a layer of TYPE OGR with a CONNECTION that points to the virtual data file and DATA that is one of the OGRVRTLayer layer names, with appropriate classes for drawing
     16   6. test using mapserver/mapscript
     17
     18== Detailed steps ==
     19
     20=== Create a DSN ===
     21
     22Creating a DSN is system dependent. Some notes follow:
     23
     24==== Windows ====
     25
     26Windows is fairly easy to setup and test DSNs since the Data Source Manager does most of the work for you. However, one gotcha is that OGR seems to need a System DSN. User DSNs do not seem to work. If anyone can figure out why (permissions?) please update this.
     27
     28(ChipHankley - System DSNs are DEFINITELY the way to go on Windows boxes using IIS for web-based apps. This is because all web transactions use a "web-user" that is typically different from the person who is logged in. System DSNs are tied to the system, while User DSNs are tied to a user. For example if person A sets up a User DSN, person B will not have access to it when they're logged in. Consequently, the default web-user will never have access to a User DSN, unless the default web-user - set up in IIS - is the same as the person setting up the User DSN. Bottom line, avoid all of this by using System DSNs.)
     29
     30(JenuAbraham - Setting up the ODBC system DSN for Windows 2003 with MS SQL Server.
     31
     32 Administrative tools -> DataSources(ODBC)-> System DSN -> Add -> SQL Server
     33 -> (Give any datasource name eg:"localserver") -> Select the SQL Server eg:(local)
     34 -> CHECK RadioButton (With SQL Server Authentication) -> CHECK(Connect to SQL Server to obtain default settings)
     35 -> fill out the username and password for SQL Server -> Select the default database where your table exists
     36 -> Finish -> Test Data Source.
     37
     38NOTE: ogrinfo might work with both windowsNT authentication and with SQL server authentication without obtaining the default settings from SQL Server. But mapserver.exe will only work with SQL Server authentication along with the default setting from SQL Server.)
     39
     40==== Linux ====
     41
     42When working on unix platforms, you need to supply your own ODBC driver manager as well as an ODBC driver for the RDBMS being accessed.
     43
     44I found two driver managers: http://www.unixodbc.org and http://www.iodbc.org. GDAL/OGR compiles much more easily against unixodbc, so it is probably a better bet. The driver manager can be built with a GUI tool to allow creation of DSNs, or you can add them through configuration files identical in structure to those used by Windows (odbc.ini and odbcinst.ini).
     45
     46Once the driver manager is installed, you need to reconfigure and make a recent cvs version of GDAL/OGR adding --with-odbc to the configure command. When built, ogrinfo --formats will tell you if odbc is an available driver.
     47
     48ODBC drivers can often be obtained from the RDBMS supplier. MyODBC? is freely available from the MySQL site and worked well in my testing. There are commercially supported drivers for Oracle available from www.easysoft.com (there is supposed to be a free trial) but there is also an open source odbc driver project for oracle that seems to be based on the easysoft one: http://fndapl.fnal.gov/~dbox/oracle/odbc/
     49
     50Once your odbc driver is built, you need the DSN to reference the shared library:
     51
     52e.g.
     53{{{
     54[unixodbctest]
     55Driver = /usr/local/lib/libmyodbc3.so
     56Port = 3306
     57Database = test
     58UID = username
     59PWD = password
     60}}}
     61One tip: compiling an odbc driver usually requires you to have the client libraries for the RDBMS available on your system.
     62
     63If you have experience with alternatives to these or drivers for other common databases, pleas add them.
     64
     65Test using ogrinfo
     66
     67ogrinfo is a binary utility that can be built from the gdal source tree. Once you figure out how to build it, you can use it to test various aspects of an OGR datasource. Details on using ogrinfo can be found at:
     68
     69http://www.remotesensing.org/gdal/ogr/ogr_utilities.html
     70
     71The options you will likely want to use are:
     72
     73 $ ogrinfo ODBC:user/pass@DSN table
     74
     75This tells ogrinfo to use an ODBC datasource named DSN with user name "user" and password "pass", and display all the features in the table called "table". Some notes on this. The username and password should be optional if the DSN is configured to include a user name and password, in which case you would write 'ogrinfo ODBC:DSN table' but I never got this to work correctly. The password can be empty, in which case you would use 'ogrinfo ODBC:user/@DSN table'.
     76
     77You can also use SQL statements with ogrinfo with the -sql option. For instance, "ogrinfo ODBC:user/pass@DSN -sql 'select table.* from table where table.myval > 10'"
     78
     79The latest version of the OGR ODBC driver as of 2004-01-05 passes the SQL directly to the ODBC driver, which is a slight change. Before this version, OGR interpreted the SQL itself and was somewhat limited in its SQL abilities (see http://www.remotesensing.org/gdal/ogr/ogr_sql.html).
     80
     81A note about ogrinfo and debugging. To get more information out of ogrinfo, you can set an environment variable called CPL_DEBUG to ON (set CPL_DEBUG=ON in windows)
     82
     83If you have trouble connecting to the ODBC datasource with OGR, you might want to use a direct ODBC client to verify that the ODBC datasource is working properly. For windows the WinSQL? product (http://www.indus-soft.com/SynametricsWebApp/WinSQL.jsp) is good, and the Lite version is free. On Unix the isql commandline client that comes with unixodbc should be OK.
     84
     85Create a Virtual Data File
     86
     87A virtual data file can be used by OGR to create geometries for non-spatial data sources. For the purpose of this howto, we are working with ODBC data sources but it should work for any OGR data source (but it is only useful if it is a non-spatial source with point data).
     88
     89The format of the virtual data file is described at
     90
     91 http://www.remotesensing.org/gdal/ogr/drv_vrt.html
     92
     93A minimal virtual file could be:
     94
     95<OGRVRTDataSource>
     96    <OGRVRTLayer name="mylayer">
     97        <SrcDataSource>ODBC:user/pass@DSN</SrcDataSource>
     98        <SrcLayer>table</SrcLayer>
     99        <GeometryType>wkbPoint</GeometryType>
     100        <LayerSRS>WGS84</LayerSRS>
     101        <GeometryField encoding="PointFromColumns" x="x" y="y"/>
     102    </OGRVRTLayer>
     103</OGRVRTDataSource>
     104
     105This example assumes that there are columns in the database table called 'x' and 'y' and that these contain spatial locations in latlong projection WGS84 (epsg:4326)
     106
     107The virtual file can be called anything you want and be placed anywhere. I'll assume its called virtual.ovf (OGR Virtual File = .ovf).
     108
     109The <SrcLayer?> parameter is a table name. If you wish to join tables or select only certain columns, you can use an SQL statement by omitting <SrcLayer?> and including <SrcSQL?>select table.name, table.x, table.y from table</SrcSQL?> in its place. Note that in the GeometryField?, you may need to refer to the attributes x and y as table.x and table.y in this case.
     110
     111Testing using ogrinfo
     112
     113To test an ovf file using ogrinfo, simply replace the driver with the path to the file, for instance:
     114
     115 $ ogrinfo virtual.ovf
     116
     117 ERROR 4: Update access not supported for VRT datasources.
     118 Had to open data source read-only.
     119 INFO: Open of `virtual.ovf'
     120 using driver `VRT' successful.
     121 1: virtual (Point)
     122
     123( The last line indicates the table inside the database you are accessing )
     124
     125To list the contents of a layer in the file, use:
     126
     127 $ ogrinfo virtual.ovf mylayer.
     128
     129 ERROR 4: Update access not supported for VRT datasources.
     130 Had to open data source read-only.
     131 INFO: Open of `virtual.ovf'
     132 using driver `VRT' successful.
     133
     134 Layer name: mylayer
     135 Geometry: Point
     136 Feature Count: 15
     137 Extent: (-74.014800, 40.445500) - (-74.014800, 40.445500)
     138 Layer SRS WKT:
     139 GEOGCS["WGS 84",
     140    DATUM["WGS_1984",
     141        SPHEROID["WGS 84",6378137,298.257223563,
     142            AUTHORITY["EPSG","7030"]],
     143        TOWGS84[0,0,0,0,0,0,0],
     144        AUTHORITY["EPSG","6326"]],
     145    PRIMEM["Greenwich",0,
     146        AUTHORITY["EPSG","8901"]],
     147    UNIT["degree",0.0174532925199433,
     148        AUTHORITY["EPSG","9108"]],
     149    AXIS["Lat",NORTH],
     150    AXIS["Long",EAST],
     151    AUTHORITY["EPSG","4326"]]
     152 virtual.name: String (50.0)
     153 virtual.x: Real (0.0)
     154 virtual.y: Real (0.0)
     155 OGRFeature(mylayer):0
     156  virtual.name (String) =  MyPoint1
     157  virtual.x(Real) = -74.01479999999999
     158  virtual.y(Real) = 40.4455
     159  POINT (-74.01480000 40.44550000 0.000)
     160
     161The difference from using ogrinfo directly on the ODBC datasource is that there is now a POINT geometry associated with the feature.
     162
     163Windows 2003-MS SQL Server:
     164If you see "ERROR 1: SetAttributeFilter?() not supported on ExecuteSQL?() results" within the above output while running the above command, then its possible that your mapserver wont run properly too. Check out OgrinfoError1
     165
     166Creating a MapServer layer
     167
     168The final step is to create a layer object in mapserver and have it use this virtual file as its datasource. A (very minimal) layer follows:
     169
     170  LAYER
     171    NAME "MyVirtualLayer"
     172    TYPE POINT
     173    CONNECTIONTYPE OGR
     174    CONNECTION "virtual.ovf" # the name and path to the virtual.ovf file, relative to shapepath may work?
     175    DATA "mylayer" # the name of the OGRVrtLayer
     176    STATUS ON
     177    LABELITEM table.name #may need to use table.name or just name depending on your OVF file.
     178    PROJECTION
     179      "init=epsg:4326"
     180    END
     181    CLASS
     182      SYMBOL 1
     183      SIZE 6
     184      COLOR 255 0 0
     185      LABEL
     186        SIZE MEDIUM
     187        TYPE BITMAP
     188        COLOR 0 0 0
     189      END
     190    END
     191END
     192
     193Testing with MapServer
     194
     195This is up to you ... hopefully you can do this if you've got this far ;)
     196
     197One tip:
     198
     199Some ODBC drivers may need environment variables to be set to function correctly. A symptom of related problems is that ogrinfo can successfully connect to your database from a command shell, but there is an OGR error connecting to the layer's .ovf file through mapserver. With Apache, you can add the necessary environment variables to your httpd.conf file:
     200
     201   1. Enable mod_env
     202   2. add lines like: setenv ORACLE_HOME /path/
     203   3. restart apache
     204
     205Second tip:
     206
     207If can see other layers but not your virtual spatial layer.
     208
     209   1. Check if you have a symbol defination in your map file.
     210   2. If NOT create one like below. (You dont have to use this, just keep it as a dummy defination).
     211
     212   SYMBOL
     213        NAME cir
     214        TYPE ellipse
     215        FILLED true
     216        POINTS
     217                1 1
     218        END
     219   END
     220
     221Third tip:
     222
     223Make sure you have the right signs for the latitudes and longitudes inside the table. x=longitude, y=latitude
     224
     225   1. Western Hemisphere eg: USA - longitude (-)
     226   2. Eastern Hemisphere eg: India - longitude (+)
     227   3. Northern Hemisphere eg: USA - latitude (+)
     228   4. Southern Hemisphere eg: Australia - latitude (-)
     229
     230eg: x= -87.5456 y = 35.4565 (for some place in USA)
     231
     232Dynamic Control of Virtual Sources
     233
     234While the virtual configuration XML is normally maintained as a separate file referenced in the CONNECTION string of the layer, OGR also makes it possible to include the file's contents instead, inline in the mapfile.
     235
     236This can be combined with the mapserver facility for URL subsitution (text in the mapfile surrounded by % delimiters is replaced with matching text in the GET or POST) to create a configuration where the connection details are passed through the incoming URL. An example shows how a time parameter in the url can be used to create an SQL argument at the time of the request:
     237
     238if we have a wms url: http://localhost/cgi-bin/mapserv?map=mymap.map&version=1.1.1&request=getmap&time=2003-12-24%2009:00:00
     239
     240and this string is the CONNECTION parameter in the mapfile:
     241
     242'<OGRVRTDataSource>
     243    <OGRVRTLayer name="mylayer">
     244        <SrcDataSource>ODBC:user/pass@DSN</SrcDataSource>
     245        <SrcSQL>SELECT * FROM table WHERE time LIKE SUBSTR("%time%", 0, 4)</SrcSQL>
     246        <GeometryType>wkbPoint</GeometryType>
     247        <LayerSRS>WGS84</LayerSRS>
     248        <GeometryField encoding="PointFromColumns" x="x" y="y"/>
     249    </OGRVRTLayer>
     250</OGRVRTDataSource>'
     251
     252the result is that the SQL returns all records from table with a date in 2003.
     253
     254TIP: experiment with single and double quotes in situations where mapserver or your ODBC source are having trouble reading the complete string.
     255
     256Using some creative SQL it's possible to do quite a bit!