Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hhkaos committed Jan 9, 2020
1 parent f56b8b1 commit 08ed968
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 32 deletions.
56 changes: 34 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
{
"name": "koop-provider-public-google-sheets",
"version": "0.1.0",
"description": "An opinionated provider template for koop-cli",
"main": "src/index.js",
"scripts": {
"test": "mocha 'test/**/*.test.js'",
"start": "koop serve"
},
"dependencies": {
"config": "^3.1.0"
},
"devDependencies": {
"@koopjs/cli": "^0.4.0",
"chai": "^4.2.0",
"mocha": "^6.1.4"
},
"repository": "",
"keywords": [
"koop",
"plugin",
"provider"
]
"name": "koop-provider-public-google-sheets",
"version": "0.1.0",
"description": "A Koop provider to transform public Google Spreadsheets to a Feature Service",
"main": "src/index.js",
"scripts": {
"test": "mocha 'test/**/*.test.js'",
"start": "koop serve"
},
"dependencies": {
"config": "^3.1.0",
"request": "^2.79.0"
},
"devDependencies": {
"@koopjs/cli": "^0.4.0",
"chai": "^4.2.0",
"mocha": "^6.1.4"
},
"repository": {
"type": "git",
"url": "[email protected]:esri-es/koop-provider-public-google-sheets.git"
},
"keywords": [
"koop",
"plugin",
"provider",
"google",
"spreadsheets"
],
"author": "Raul Jimenez <[email protected]>",
"homepage": "https://github.com/esri-es/koop-provider-public-google-sheets",
"bugs": {
"url": "https://github.com/esri-es/koop-provider-public-google-sheets/issues"
},
"license": "Apache-2.0"
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// e.g. /sample/FeatureServer/0/query
const provider = {
type: 'provider',
name: 'koop-cli-new-provider',
name: 'koop-provider-public-google-sheets',
version: '0.1.0',
hosts: false, // if true, also adds disableIdParam
hosts: true, // if true, also adds disableIdParam
disableIdParam: true, // if true, adds to path and req.params
Model: require('./model')
}
Expand Down
101 changes: 93 additions & 8 deletions src/model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
model.js
model.js
This file is required. It must export a class with at least one public function called `getData`
This file is required. It must export a class with at least one public function called `getData`
Documentation: https://koopjs.github.io/docs/usage/provider
Documentation: https://koopjs.github.io/docs/usage/provider
*/

function Model (koop) {}
Expand All @@ -19,13 +19,98 @@ function Model (koop) {}
// req.params.id (if index.js:disableIdParam false)
// req.params.layer
// req.params.method
const request = require('request').defaults({gzip: true, json: true});
const latNames = ['gsx$lat', 'gsx$latitude', 'gsx$y', 'gsx$latitud'];
const lonNames = ['gsx$lon', 'gsx$long', 'gsx$longitude', 'gsx$x', 'gsx$longitud'];

Model.prototype.getData = function (req, callback) {
const geojson = {
type: 'FeatureCollection',
features: []
}

callback(null, geojson)
let spreadsheetId = req.params.host;
let tab = req.params.layer;
let url = `https://spreadsheets.google.com/feeds/list/${spreadsheetId}/${tab}/public/values\?alt\=json`;
//console.log("URL = ", url);
request(`${url}`, (err, res, body) => {
if (err) return callback(err)

//const geojson = body
let geojson = {
type: 'FeatureCollection',
features: []
}



if(body.feed && body.feed.entry){
var features = body.feed.entry.map((elem, i) => {
var feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"properties": {}
};

var properties = {};

for(var p in elem) {
//console.log(`${p} = p.substring(0,3) = ${p.substring(0,3)}`)
if(p.substring(0,4) === "gsx$"){
//console.log("Found!")
newP = p.substring(4,p.length);
let propValue = elem[p].$t
if(!isNaN(elem[p].$t)){
// If it is a number
if(propValue.indexOf('.') != -1){
properties[newP] = parseFloat(propValue);
}else{
properties[newP] = parseInt(propValue);
}
}else{
properties[newP] = propValue
}

if(latNames.indexOf(p.toLowerCase()) != -1){
feature.geometry.coordinates[1] = properties[newP];
}else if(lonNames.indexOf(p.toLowerCase()) != -1){
feature.geometry.coordinates[0] = properties[newP];
}

properties['OBJECTID'] = i;
}
}

feature.properties = properties

return feature;
});

geojson.features = features;
}else{
//console.log("Error, body = ", body);
}

geojson.features = geojson.features.filter(f => {
//console.log("f.geometry.coordinates=",f.geometry.coordinates)
const coords = f.geometry.coordinates;
if(isNaN(coords[0]) && isNaN(coords[1])){
return false;
}
return true;
});

geojson.metadata = {
title: `Google spreedsheets tab`,
name: `Google spreedsheets tab`,
idField: 'OBJECTID',
description: `Generated from ${url}`,
}

callback(null, geojson)
});



}

module.exports = Model

0 comments on commit 08ed968

Please sign in to comment.