Commit e7840869 authored by Kaitlyn's avatar Kaitlyn
Browse files

Added WFS overlay to jupyter and updated JS to use new mapserver

parent db371c36
Loading
Loading
Loading
Loading
+34 −8
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ export default L.LayerCollection = L.Class.extend({
    this._overlays = {};
    this._defaultLayerIndex = 0;
    this._wfsLayer = null;
    this._wfsJSON = null;
    L.LayerCollection.layerControl = null;

    let layers = this.parseJSON();
@@ -50,6 +51,7 @@ export default L.LayerCollection = L.Class.extend({
      let currentTarget = targets[i];

      if (currentTarget["name"].toLowerCase() == this._target.toLowerCase()) {
        this._targetJSON = currentTarget["webmap"];
        let jsonLayers = currentTarget["webmap"];
        for (let j = 0; j < jsonLayers.length; j++) {
          let currentLayer = jsonLayers[j];
@@ -73,6 +75,7 @@ export default L.LayerCollection = L.Class.extend({
            }
          } else {
            layers["wfs"].push(currentLayer);
            this._wfsJSON = currentLayer;
          }
        }
      }
@@ -123,8 +126,8 @@ export default L.LayerCollection = L.Class.extend({

    this._wfsLayer = new L.GeoJSON(null, {
      onEachFeature: function(feature, layer) {
        if (feature.properties && feature.properties.name) {
          layer.bindPopup(feature.properties.name);
        if (feature.properties && feature.properties.clean_feature) {
          layer.bindPopup(feature.properties.clean_feature);
        }
      },
      pointToLayer: function(feature, latlng) {
@@ -182,17 +185,15 @@ export default L.LayerCollection = L.Class.extend({
   * @param  {AstroMap} map - The AstroMap to add the GeoJSON layer to.
   */
  loadWFS: function(map) {
    let geoJsonUrl =
      "https://astrocloud.wr.usgs.gov/dataset/data/nomenclature/" +
      map.target().toUpperCase() +
      "/WFS";
    let geoJsonUrl = "https://wms.wr.usgs.gov/cgi-bin/mapserv";

    let defaultParameters = {
      map: this._wfsJSON["map"],
      service: "WFS",
      version: "1.1.0",
      request: "GetFeature",
      outputFormat: "application/json",
      srsName: "EPSG:4326"
      typename: map.target().toUpperCase() + "_POLY",
      outputFormat: "application/json; subtype=geojson"
    };

    let customParams = {
@@ -207,6 +208,31 @@ export default L.LayerCollection = L.Class.extend({
      url: geoJsonUrl + L.Util.getParamString(parameters),
      dataType: "json",
      timeout: 30000,

      // MapServer is having problems returning a JSON when requesting polygon features.
      // Adds XML at the end for some reason.
      error: function(response) {
        let lines = response.responseText.split("\n");

        // Remove lines in XML format
        let numLines = lines.length;
        let lineCount = 0;
        while (lineCount < numLines) {
          if (lines[lineCount].includes("Content-type")) {
            break;
          }
          lineCount++;
        }
        lines.splice(lineCount, numLines - 1);
        let jsonString = lines.join("\n");

        let data = $.parseJSON(jsonString);
        let sortedFeatures = thisContext.sortFeatures(data["features"]);
        data["features"] = sortedFeatures;
        thisContext._wfsLayer.clearLayers();
        thisContext._wfsLayer.addData(data);
      },

      success: function(data) {
        let sortedFeatures = thisContext.sortFeatures(data["features"]);
        data["features"] = sortedFeatures;
+45 −1
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
# This is used to install all the correct packages using pip
import sys
!{sys.executable} -m pip install ipywidgets
!{sys.executable} -m pip install ipyleaflet
!{sys.executable} -m pip install geojson
!{sys.executable} -m pip install shapely
```

%% Cell type:code id: tags:

``` python
import os
import sys
module_path = os.path.abspath(os.path.join('../src/'))
if module_path not in sys.path:
    sys.path.append(module_path)
```

%% Cell type:code id: tags:

``` python
import CartoCosmos as l
import urllib.request
import json
import ipyleaflet

map = l.planetary_maps('mercury')
map = l.planetary_maps('mars')

# map.add_wkt("POLYGON ((-187.03125 22.851563, -187.03125 35.15625, -167.34375 35.15625, -167.34375 22.851563, -187.03125 22.851563))")

map.display_map()

# geoJsonUrl = "https://astrocloud.wr.usgs.gov/dataset/data/nomenclature/" + "MARS" + "/WFS"

def hover_handler(event=None, feature=None, id=None, properties=None):
    label = ipyleaflet.Label(layout=ipyleaflet.Layout(width='100%'))
    label.value = properties['name']

geoJsonUrl = "https://astrocloud.wr.usgs.gov/dataset/data/nomenclature/MERCURY/WFS?service=WFS&version=1.1.0&request=GetFeature&outputFormat=application%2Fjson&srsName=EPSG%3A4326&MAXFEATURES=2"
with urllib.request.urlopen(geoJsonUrl) as url:
    jsonp = json.loads(url.read())
    geo_json = ipyleaflet.GeoJSON(data=jsonp)
    geo_json.on_click(hover_handler)
    map.planet_map.add_layer(geo_json)
```

%% Output





    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-21-71051d1a3868> in hover_handler(event, feature, id, properties)
         13
         14 def hover_handler(event=None, feature=None, id=None, properties=None):
    ---> 15     label = ipyleaflet.Label(layout=ipyleaflet.Layout(width='100%'))
         16     label.value = properties['name']
         17
    AttributeError: module 'ipyleaflet' has no attribute 'Label'

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    TypeError: hover_handler() got an unexpected keyword argument 'type'

    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-21-71051d1a3868> in hover_handler(event, feature, id, properties)
         13
         14 def hover_handler(event=None, feature=None, id=None, properties=None):
    ---> 15     label = ipyleaflet.Label(layout=ipyleaflet.Layout(width='100%'))
         16     label.value = properties['name']
         17
    AttributeError: module 'ipyleaflet' has no attribute 'Label'

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    TypeError: hover_handler() got an unexpected keyword argument 'type'

%% Cell type:code id: tags:

``` python
```