ncsco / pinemap-dss-help

Issue tracker for PINEMAP DSS
0 stars 0 forks source link

[HOW-TO] Add shapefile layers to OLv3 and get point info. #85

Closed daviswx closed 8 years ago

daviswx commented 8 years ago

(Adding this to give some documentation about how I added the shapefile layer for future reference)

The shapefile we started with, Planting_Years_1980-2070.shp, had a corresponding MapServer file, green_wt.map. However, this shapefile was in WGS84 (EPSG:4326) and the DSS is in spherical mercator (EPSG:3857), so it wouldn't show up on the DSS maps in that format.

To get the shapefile in the correct projection, the GDAL function ogr2ogr was used:

ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:3857 Planting_Years_1980-2070_reproj.shp Planting_Years_1980-2070.shp

The .map file also had to be modified. The PROJECTION field was changed from "proj=latlong" to "init=epsg:3857" for both the file and the individual layers.

With that done, the shapefile could be added to the OpenLayers v3 maps using this code:

 test_layer = new ol.layer.Tile({
  name: 'States',
  source: new ol.source.TileWMS({
   url: 'http://[localhost]?map=layers/green_wt_reproj.map',
   params: {'VERSION': '1.3.0', 'LAYERS': 'GW_2014', 'CRS': 'EPSG:3857', 'format': 'image/png'},
   projection: ol.proj.get('EPSG:3857'),
   crossOrigin: 'anonymous',
   serverType: 'mapserver'
  }),
  visible: true
 });
 map.addLayer(test_layer);

In addition to displaying the data on the map, we wanted to retrieve the value from a clicked point. This can be done with WMS GetFeatureInfo requests.

First, a few other lines had to be added to the new .map file:

LAYER
...
    TEMPLATE "layers/query_layer.html"
    METADATA
     "wms_include_items"  "all"
     "wms_exclude_items"  ""
    END #METADATA
...
END

The template is simply an HTML file. Listing this in the .map file apparently specifies that the shapefile is queryable.

Finally, the GetFeatureInfo request can be used to retrieve data from a clicked point. This can be done using a bit of OpenLayers code, or with a URL string request:

http://[localhost]?map=layers/green_wt_reproj.map&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo&QUERY_LAYERS=GW_2014&LAYERS=GW_2014&CRS=EPSG%3A3857&INFO_FORMAT=text%2Fplain&FEATURE_COUNT=1&STYLES=&I=171&J=25&WIDTH=256&HEIGHT=256&BBOX=-10018754.171394622%2C2504688.542848654%2C-8766409.899970295%2C3757032.814272982

In this case, the &I= and &J= parameters specify which point was clicked.

The output looks like this:

GetFeatureInfo results:

Layer 'GW_2014'
  Feature 9143: 
    HUC12_doub = '31102010105.000000000000'
    Year_1980 = '151.325279366238021'
    Year_1981 = '151.097803618997006'
    Year_1982 = '150.998496127039999'
...

By changing the file name or layer in the URL and parsing through the output, data from clicked points can be quickly retrieved in lieu of using something like an OPenDAP call or a MySQL lookup.