Skip to content

Commit

Permalink
add oid for toEsri (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Fenton committed Jun 15, 2017
1 parent e3004ce commit 0f1e281
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 117 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Detect field types if they are not passed in with `geojson.metadata`
- Translate ISO Date Strings to Unix Timestamps when `options.toEsri` is true
- Add Object IDs if there is no ID field set with `options.toEsri` is true

## [1.9.0] - 05-24-2017
### Added
Expand Down
58 changes: 35 additions & 23 deletions src/index.js
Expand Up @@ -70,25 +70,7 @@ Winnow.prepareSql = function (statement) {
}
}

function finishQuery (features, options) {
if (options.groupBy) {
return features
} else if (options.aggregates) {
return features[0]
} else if (options.collection) {
const collection = options.collection
collection.features = features
return collection
} else {
return features
}
}

function aggregateQuery (features, query, options) {
const params = Query.params(features, options)
const filtered = sql(query, params)
return finishQuery(filtered, options)
}
// TODO move these functions to a new file

function limitQuery (features, query, options) {
const filtered = []
Expand All @@ -102,23 +84,53 @@ function limitQuery (features, query, options) {

function standardQuery (features, query, options) {
const filtered = features.reduce((filteredFeatures, feature, i) => {
// TODO used passed in fields if available
const result = processQuery(feature, query, options)
const result = processQuery(feature, query, options, i)
if (result) filteredFeatures.push(result)
return filteredFeatures
}, [])
return finishQuery(filtered, options)
}

function processQuery (feature, query, options) {
function aggregateQuery (features, query, options) {
const params = Query.params(features, options)
const filtered = sql(query, params)
return finishQuery(filtered, options)
}

function processQuery (feature, query, options, i) {
const params = Query.params([feature], options)
const result = sql(query, params)[0]
if (options.dateFields.length && options.toEsri) {

if (result && options.toEsri) return esriFy(result, options, i)
else return result
}

function esriFy (result, options, i) {
if (options.dateFields.length) {
options.dateFields.forEach(field => {
result.attributes[field] = new Date(result.attributes[field]).getTime()
})
}

const metadata = (options.collection && options.collection.metadata) || {}
if (!metadata.idField) {
result.attributes.OBJECTID = i
}
return result
}

function finishQuery (features, options) {
if (options.groupBy) {
return features
} else if (options.aggregates) {
return features[0]
} else if (options.collection) {
const collection = options.collection
collection.features = features
return collection
} else {
return features
}
}

module.exports = Winnow

0 comments on commit 0f1e281

Please sign in to comment.