一、编写相交查询函数
/**
* @param {String} url Arcgis地图服务查询地址
* @param {Object} [param] 查询参数
* @param {string} [param.where='1=1']
* @param {string} [param.text]
* @param {string} [param.objectIds]
* @param {string} [param.time]
* @param {string} [param.geometry]
* @param {string} [param.geometryType='esriGeometryEnvelope']
* @param {string} [param.inSR]
* @param {string} [param.spatialRel='esriSpatialRelIntersects']
* @param {string} [param.relationParam]
* @param {string} [param.outFields]
* @param {boolean} [param.returnGeometry=true]
* @param {boolean} [param.returnTrueCurves=false]
* @param {string} [param.maxAllowableOffset]
* @param {string} [param.geometryPrecision]
* @param {string} [param.outSR='4326']
* @param {boolean} [param.returnIdsOnly=false]
* @param {boolean} [param.returnCountOnly=false]
* @param {string} [param.orderByFields]
* @param {string} [param.groupByFieldsForStatistics]
* @param {string} [param.outStatistics]
* @param {boolean} [param.returnZ=false]
* @param {boolean} [param.returnM=false]
* @param {string} [param.gdbVersion]
* @param {boolean} [param.returnDistinctValues=false]
* @param {string} [param.resultOffset]
* @param {string} [param.resultRecordCount]
* @param {string} [param.f='pjson']
* @param {boolean} [toFeature=false]
* @returns {Promise<ResponseS>}
*/
export function arcgisQuery(url, param, toFeature = false) {
const _default = {
where: '1=1',
text: '',
objectIds: '',
time: '',
geometry: '',
geometryType: 'esriGeometryEnvelope',
inSR: '',
spatialRel: 'esriSpatialRelIntersects',
relationParam: '',
outFields: '*',
returnGeometry: true,
returnTrueCurves: false,
maxAllowableOffset: '',
geometryPrecision: '',
outSR: '4326',
returnIdsOnly: false,
returnCountOnly: false,
orderByFields: '',
groupByFieldsForStatistics: '',
outStatistics: '',
returnZ: false,
returnM: false,
gdbVersion: '',
returnDistinctValues: false,
resultOffset: '',
resultRecordCount: '',
f: 'pjson'
}
Object.assign(_default, param)
const data = new FormData()
for (const key in _default) {
data.set(key, _default[key])
}
let loadingInstance = Loading.service({ fullscreen: true });
return axios.post(url, data).then(({ data: resp }) => {
loadingInstance.close();
if (resp.error) {
const { details, message } = resp.error
return Promise.reject([message, ...details].join(';') || 'Error')
}
if (toFeature) {
resp.features = new EsriJSON().readFeatures(resp)
}
return resp
})
}
二、获取查询要素
function getFeatures(url,whereClause ){
let formData = new FormData();
formData.append("f", "json");
formData.append("where", whereClause);
formData.append("returnGeometry", true);
formData.append("outFields", "*");
formData.append("outSR", "4527");
axios.post(url, formData).then(e => {
if (e.data.features && e.data.features.length > 0) {
let features = EsriJsonFormatter.readFeatureFromObject(
e.data.features[0]
);
return features;
} else {
return null;
}
});
}
三、进行相交查询
function doIntersect(queryUrl,features){
arcgisQuery(queryUrl, {
geometry: toArcGIS(features[0].getGeometry(), true),
geometryType: 'esriGeometryPolygon',
outSR: getSrid(this.map),
where
}, true)
.then(({ features }) => {
if (features && features.length > 0) {
let feature = EsriJsonFormatter.readFeatureFromObject(
features[0]
);
this.map.overLayer.clear();
this.map.overLayer.addFeature(feature);
this.map.getView().fit(feature.getGeometry(), {
duration: 500,
padding: [1000, 1000, 1000, 1000],
maxZoom: 14
});
}
})
}
const ESRIJSON_FORMAT = new EsriJSON()
function toArcGIS(param, stringify = false, opt_options) {
if (param instanceof Feature) {
return ESRIJSON_FORMAT[stringify ? 'writeFeature' : 'writeFeatureObject'](param, opt_options)
} else if (param instanceof Geometry) {
return ESRIJSON_FORMAT[stringify ? 'writeGeometry' : 'writeGeometryObject'](param, opt_options)
} else if (param instanceof Array) {
return ESRIJSON_FORMAT[stringify ? 'writeFeatures' : 'writeFeaturesObject'](param, opt_options)
}
}