Skip to content

Instantly share code, notes, and snippets.

@hhkaos
Last active December 17, 2022 16:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhkaos/d842a8a30626e0cf48e3834017879f42 to your computer and use it in GitHub Desktop.
Save hhkaos/d842a8a30626e0cf48e3834017879f42 to your computer and use it in GitHub Desktop.
Esri DevSummit Berlin 2019 - https://bit.ly/DevSummitEU19

Esri DevSummit Berlin

Demo 1: Build and share your own Koop provider from scratch

Step 1)

Create a new Github repo. Name it: koop-provider-remote-geojson

Step 2)

We will be using Koop-CLI

# Install Koop-CLI if needed
npm install -g @koopjs/cli

# Start a new provider
koop new provider koop-provider-remote-geojson
cd koop-provider-remote-geojson/

# Link this folder with the repo
git remote add origin git@github.com:esri-es/koop-provider-remote-geojson.git
git add -A
git commit -m "Initial commit"

# Open your preferred editor
atom .

Step 3)

Change package.json: add metadata and dependencies

  "description": "Provider to recover any static GeoJSON from any server a serve it as a feature service",
  "dependencies": {
  	"config": "^3.1.0",
  	"atob": "^2.1.2",
  	"request": "^2.79.0"
  },
  "repository": {
   "type": "git",
   "url": "git@github.com:esri-es/koop-provider-remote-geojson.git"
  },
  "keywords": [
  	"koop",
  	"plugin",
  	"provider",
  	"carto",
  	"etl"
  ],
  
  "author": "Raul Jimenez <raul.jimenez@esri.es>",
  "homepage": "https://github.com/esri-es/koop-provider-remote-geojson",
  "bugs": {
  	"url": "https://github.com/esri-es/koop-provider-remote-geojson/issues"
  },
  "license": "Apache-2.0"

Step 4)

Change the default provider name src/index.js file

  ...
  name: 'koop-provider-remote-geojson'
  hosts: true # enable one param: request.params.host
  ...

Step 5)

Add the business logic at src/model.js:

// Before getData function 

const request = require('request').defaults({gzip: true, json: true});
const atob = require('atob');

String.prototype.replaceAll = function(search, replacement) {
    let target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

And within getData function:

  // Descompress base64 string 
  let url = req.params.host.replaceAll('_', '/'); // After the URL compression '/' were manually replaced by '_'
  url = atob(url);

  request(`${url}`, (err, res, body) => {
      if (err) return callback(err)

      const geojson = body

      callback(null, geojson)
  });

Step 6)

Now let's use Koop-CLI to start a development server which will allow us to test the provider:

koop serve

Step 7)

Now we will add the HTML page to help anyone to encode plain URLs the string our provider is expecting.

mkdir docs && curl https://gist.githubusercontent.com/hhkaos/69760af30d9bba4106460513f79a6a67/raw/6e7e53a18c72e5d5ce3a90312b3b6931cd218c9c/encodebase64.html -o "docs/index.html"

Step 8)

Launch a web server, open the URL encoder and test it:

http-server -p 9095

open http://localhost:9095/docs

Use the "Preview" links that shows up after pressing "Encode URL".

You can even try to:

  • Change the output to GeoJSON adding appending ?f=geojson (preview)
  • Or filter the features adding ?where=direccion = 'SIERRA NEVADA, 8' (but encoded using encodeURIComponent() function) (preview)

`

Step 9)

Optionally you can deploy the provider using now.sh, just run:

now

It use to take take some time, but eventually it should provide a server ready to use like this: https://-.now.sh

Step 10)

Now it's time to write some nice documentation with link to the live demo if you did the previous step.

curl https://gist.githubusercontent.com/hhkaos/8e9aa354b4b6ba9ede5f3f41d7c3281b/raw/1770e00392967b84f0aef3864f71ea9dc365bbe3/koop-provider-remote-geojson-README.md -o README.md

And whenever you are ready push everything to the repo:

git add -A
git commit -m "Provider 0.1.0 version"
git push -u origin master

Step 11)

Now you should be ready to publish your provider to npmjs.com running: npm publish

You will need an account and be logged (in the command line).

Step 12)

If everything is nice and working do a pull request to the Koop docs and add your plugin to the providers built by Third parties

Demo 2: Install and secure a pass-through provider


# Create a new Koop app
koop new app demo-app
cd demo-app

# Add this Auth plugin
koop add provider @koopjs/auth-direct-file

# Create a file with credentials
echo \[\\n\\t{ \"username\": \"admin\", \"password\": \"admin\" }\\n\] > src/user-store.json

# Open src/plugins.js and replace:
# This line: 		const authDirectFile = require('@koopjs/auth-direct-file')
# For this line: 	const authDirectFile = require('@koopjs/auth-direct-file')('AnyRadomString', `${__dirname}/user-store.json`, { useHttp: true });

# Install any provider
koop add provider koop-provider-foursquare

# For the foursquare provider you will need to create a file within node_modules/koop-provider-foursquare/config/default.json
# and add your Foursquare "client_id" and "client_secret" among other things (check the default_sample.json in the same folder)

# Run the koop server
koop serve

# Try to access server (it will fail <- require an access token)
http://localhost:8080/koop-provider-foursquare/Berlin/FeatureServer/0/query

# Generate token using any of the credentials you placed at src/user-store.json
http://localhost:8080/koop-provider-foursquare/tokens?username=admin&password=admin

# Now retry to access service but adding the generated token
http://localhost:8080/koop-provider-foursquare/Berlin/FeatureServer/0/query?token=GENERATED_TOKEN

# That's it! you are done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment