Coder Social home page Coder Social logo

irishmarineinstitute / erddap-leaflet-velocity-demo Goto Github PK

View Code? Open in Web Editor NEW
7.0 5.0 4.0 191 KB

A demonstration of connecting Erddap to Leaflet Velocity maps

HTML 24.97% JavaScript 75.03%
erddap leaflet leaflet-velocity wind water velocity ocean oceanography current weather

erddap-leaflet-velocity-demo's People

Contributors

adamml avatar sagarkevin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

erddap-leaflet-velocity-demo's Issues

Limited by CORS

While this now uses a JSONP formatted response, it doesn't get around CORS when accessing other ERDDAP servers as it still is using the fetch API.

fetch(
erddapBaseUrl
+ '/erddap/griddap/'
+ datasetID
+ '.json?'
+ uParameter + '[(' + refTime + '):1:(' + refTime + ')][(' + String(minLat) + '):' + String(strideLat) + ':(' + String(maxLat) + ')][(' + String(minLon) + '):'+ String(strideLon) +':(' + String(maxLon) + ')],'
+ vParameter + '[(' + refTime + '):1:(' + refTime + ')][(' + String(minLat) + '):' + String(strideLat)+ ':(' + String(maxLat) + ')][(' + String(minLon) + '):' + String(strideLon) + ':(' + String(maxLon) + ')]'
+ '&.jsonp=erddapData').
then(response => response.text()).
then(txt2json => JSON.parse(txt2json.replace('erddapData(','').slice(0,-1))).

JSONP gets around CORS by loading data within a script tag and when the script finishes loading it fires the JSONP callback function (erddapData) that it has been wrapped in.

Starting with this, this function will work to load JSONP data from ERDDAP and return a Promise, that you can .then( off of and not be limited by CORS.

function jsonp(url) {
    return new Promise(function(resolve, reject) {
        let script = document.createElement('script')
        const name = "_jsonp_" + Math.round(100000 * Math.random());
        //url formatting
        if (url.match(/\?/)) url += "&.jsonp="+name
        else url += "&.jsonp="+name
        script.src = url;

        window[name] = function(data) {
            resolve(data);
            document.body.removeChild(script);
            delete window[name];
        }
        document.body.appendChild(script);
    });
}

I believe a remaining limitation will be when accessing http from https sites as that will be blocked by mixed content.

Re #1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.