Merge featureLayerProvider geometries in the results
See original GitHub issueI have a case where searching for certain ZIP codes returns 2 separate polygons (because the ZIP code spans 2 counties). I need to be able to merge them to return a single polygon. Before I switched to this geocoder I returned the features and used L.esri.Util.arcgisToGeojson to push the geometries into a feature collection and then merged them using turf.js.
getCityZipBoundary: function(data){
var df = $q.defer();
var features = data.features;
var featureCollection = {
type: 'featureCollection',
features: []
};
for (var i = features.length - 1; i >= 0; i--) {
var feature = L.esri.Util.arcgisToGeojson(features[i]);
featureCollection.features.push(feature);
}
var mergedPolygon = turf.merge(featureCollection);
df.resolve(mergedPolygon);
return df.promise;
}
Now that I’m using this geocoder plugin I’m not sure how I would be able to merge the geometries.
What I was going to try next but I’m extremely hesitant: It looks like I would need to add code to the suggestions function that checks to see if the input text matches suggestions.text and if they do then change the magicKey value to null.
suggestions: function(text, bounds, callback) {
var query = this.query().where(this._buildQuery(text)).returnGeometry(true);
if (bounds) {
query.intersects(bounds)
}
if (this.options.idField) {
query.fields([this.options.idField].concat(this.options.searchFields))
}
var request = query.run(function(error, results, raw) {
if (error) {
callback(error, [])
} else {
this.options.idField = raw.objectIdFieldName;
var suggestions = [];
var count = Math.min(results.features.length, this.options.maxResults);
for (var i = 0; i < count; i++) {
var feature = results.features[i];
suggestions.push({
text: this.options.formatSuggestion.call(this, feature),
magicKey: feature.id
})
}
<<<Insert code here to change magicKey value to null?>>>
callback(error, suggestions.slice(0, this.options.maxResults).reverse())
}
}, this);
return request
}
Then, in results function, since you wouldn’t have a key it would make a generic search and then I could merge the geometries the way I did before.
results: function(text, key, bounds, callback) {
var query = this.query();
if (key) {
query.featureIds([key])
} else {
query.where(this._buildQuery(text))
}
if (bounds) {
query.within(bounds)
}
return query.run(L.Util.bind(function(error, features) {
var results = [];
<<<Insert code here to merge features?>>>
for (var i = 0; i < features.features.length; i++) {
var feature = features.features[i];
if (feature) {
var bounds = this._featureBounds(feature);
var result = {
latlng: bounds.getCenter(),
bounds: bounds,
text: this.options.formatSuggestion.call(this, feature),
properties: feature.properties,
geojson: feature
};
results.push(result)
}
}
callback(error, results)
}, this))
},
Am I on the right track or is this a terrible idea or am I completely missing the obvious solution?
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
I’ve tested and can confirm it solves my problem. Thank you!
awesome. thanks!