Skip to content

Instantly share code, notes, and snippets.

@doneill
Created September 14, 2012 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save doneill/3724785 to your computer and use it in GitHub Desktop.
Save doneill/3724785 to your computer and use it in GitHub Desktop.
Query features from ArcGIS Feature Layer in Android
/*
* Query features from the feature layer
*/
private void queryFeatureLayer(){
// create a Query
Query query = new Query();
// set spatial reference
SpatialReference sr = SpatialReference.create(102100);
// set the geometry to the sketch polygon
query.setGeometry(selection);
// query features that are completely contained by selection
query.setSpatialRelationship(SpatialRelationship.CONTAINS);
// set query in/out spatial ref
query.setInSpatialReference(sr);
query.setOutSpatialReference(sr);
// return all features
query.setOutFields(new String[]{"*"});
// include geometry in result set
query.setReturnGeometry(true);
// run query on FeatureLayer off UI thread
featureLayer.queryFeatures(query, new CallbackListener<FeatureSet>() {
// an error occurred, log exception
@Override
public void onError(Throwable e) {
Log.e("Test", "Unable to perform query", e);
}
// create json from resulting FeatureSet
@Override
public void onCallback(FeatureSet result) {
if(result != null){
FileOutputStream outstream = null;
try {
// create feature set as json string
String fsstring = FeatureSet.toJson(result);
// create fully qualified path for json file
path = createJsonFile();
// create a File from json fully qualified path
File outfile = new File(path);
// create output stream to write to json file
outstream = new FileOutputStream(outfile);
outstream.write(fsstring.getBytes());
// close output stream
outstream.close();
// create new Runnable to be added to message queue
handler.post(new MyRunnable());
} catch (Exception e) {
e.printStackTrace();
if(outstream!=null){
try {
outstream.close();
handler.post(new MyRunnable());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}});
}
/*
* Create the json file location and name
* structure
*/
public String createJsonFile(){
StringBuilder sb = new StringBuilder();
sb.append(demoDataFile.getAbsolutePath());
sb.append(File.separator);
sb.append(offlineDataSDCardDirName);
sb.append(File.separator);
sb.append(filename);
sb.append(OFFLINE_FILE_EXTENSION);
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment