-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from mborne/issue_11
defaultGeomFieldName - ajout d'une option sur le client
- Loading branch information
Showing
5 changed files
with
169 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
var WKT = require('terraformer-wkt-parser'); | ||
var flip = require('@turf/flip'); | ||
|
||
/* | ||
* WARNING: Despite the use of WGS84, you need to do a flip on the coordinates | ||
*/ | ||
|
||
/** | ||
* Convert a bbox on array with 4 values (split string if required) | ||
* | ||
* @param {string|number[]} bbox | ||
* @returns {number[]} | ||
*/ | ||
function parseBoundingBox(bbox) { | ||
if (typeof bbox !== 'string') { | ||
return bbox; | ||
} | ||
return bbox.replace(/'/g, '').split(','); | ||
} | ||
|
||
/** | ||
* Convert a bbox in cql_filter fragment. | ||
* | ||
* @param {string|number[]} bbox input bbox (CRS:84) | ||
* @param {string} geomFieldName name of the geometry (ex : the_geom) | ||
* @returns {string} | ||
*/ | ||
function bboxToFilter(bbox, geomFieldName) { | ||
bbox = parseBoundingBox(bbox); | ||
var xmin = bbox[1]; | ||
var ymin = bbox[0]; | ||
var xmax = bbox[3]; | ||
var ymax = bbox[2]; | ||
return 'BBOX(' + geomFieldName + ',' + xmin + ',' + ymin + ',' + xmax + ',' + ymax + ')'; | ||
} | ||
|
||
/** | ||
* Build cql_filter parameter for GeoServer according to user params. | ||
* | ||
* @param {object} params | ||
* @param {object} [params.geom] search geometry intersecting the resulting features. | ||
* @param {object} [params.bbox] search bbox intersecting the resulting features. | ||
* @param {string} [geomFieldName="the_geom"] name of the geometry column | ||
* @returns {string} | ||
*/ | ||
function buildCqlFilter(params, geomFieldName) { | ||
geomFieldName = geomFieldName || 'the_geom'; | ||
|
||
var parts = []; | ||
for (var name in params) { | ||
// ignore _limit, _start, etc. | ||
if (name.charAt(0) === '_') { | ||
continue; | ||
} | ||
|
||
if (name == 'bbox') { | ||
parts.push(bboxToFilter(params['bbox'], geomFieldName)); | ||
} else if (name == 'geom') { | ||
var geom = params[name]; | ||
if (typeof geom !== 'object') { | ||
geom = JSON.parse(geom); | ||
} | ||
var wkt = WKT.convert(flip(geom)); | ||
parts.push('INTERSECTS(' + geomFieldName + ',' + wkt + ')'); | ||
} else { | ||
parts.push(name + '=\'' + params[name] + '\''); | ||
} | ||
} | ||
if (parts.length === 0) { | ||
return null; | ||
} | ||
return parts.join(' and '); | ||
}; | ||
|
||
|
||
module.exports = buildCqlFilter; | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const expect = require('chai').expect; | ||
|
||
const Client = require('../src/Client'); | ||
|
||
|
||
|
||
describe('test Client', function () { | ||
|
||
it('should replace apiKey in the generated URL', function () { | ||
let client = new Client({ | ||
apiKey: 'my-api-key' | ||
}); | ||
expect(client.getUrl()).to.equal('https://wxs.ign.fr/my-api-key/geoportail/wfs'); | ||
}); | ||
|
||
it('should use "the_geom" as default value for defaultGeomFieldName', function () { | ||
let client = new Client({ | ||
apiKey: 'my-api-key' | ||
}); | ||
expect(client.defaultGeomFieldName).to.equal('the_geom'); | ||
}); | ||
|
||
it('should use support user value for defaultGeomFieldName', function () { | ||
let client = new Client({ | ||
apiKey: 'my-api-key', | ||
defaultGeomFieldName: 'geom' | ||
}); | ||
expect(client.defaultGeomFieldName).to.equal('geom'); | ||
}); | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters