I have some features published on ArcGIS online. I'm accessing it as a feature service. I need to use that feature service as a GeoJSON and display it on the map. Is there any sandbox example on how to display GeoJSON on to the map or can any one share an example code on it using ArcGIS javascript API?
[GIS] Accessing GeoJSON from ArcGIS online rest API
arcgis-javascript-apigeojson
Related Solutions
I've checked requests which were sent in ArcGIS Online to add Feature Service with Chrome dev tools and found that it uses 4 requests:
- Create Service - create feature service with REST API without layers, see http://resources.arcgis.com/en/help/arcgis-rest-api/#/Create_Service/02r30000027r000000/
- update Service item - fix service name, see http://resources.arcgis.com/en/help/arcgis-rest-api/#/Update_Item/02r30000009s000000/
- addToDefinition request for admin part, see http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Add_To_Definition_Feature_Service/02r300000230000000/ - this is the request to add layer definition into empty service
- Last request to updateDefinition of the service (http://resources.arcgis.com/en/help/arcgis-rest-api/#/Update_Definition_Feature_Service/02r30000021z000000/).
Finally I have the answer for my Query: Here is the solution... (I am posting this; it might help someone) Thank You for your Help and Support.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Copy data from one service to another using REST API</title>
<script src="http://js.arcgis.com/3.9/"></script>
<script>
var objPD = [{ "attributes": { "OBJECTID": 1, "SAM_NO": 0, "PIN": "1010027", "C_SRC": " ", "TRANS_NO": 100, "D_IN_DATE": null, "I_WORKORDER_ID": null, "TAIN_NO": null, "TAIN_YEAR": null, "PAR_AREA": 174804 }, "geometry": null }];
require([
"esri/tasks/query", "esri/tasks/QueryTask",
"dojo/dom", "dojo/on", "dojo/dom-class", "dojo/_base/json",
"esri/urlUtils", "esri/config", "esri/request", "dojo/domReady!"],
function (Query, QueryTask, dom, on, domClass, dojoJson, urlUtils, esriConfig, esriRequest) {
esriConfig.defaults.io.proxyUrl = "http://localhost:52896/proxy.ashx";
var queryTask = new QueryTask("http://xxx/arcgis/rest/services/xxx/xxx/MapServer/1");
var query = new Query();
query.returnGeometry = true;
query.outFields = [
"PIN", "PDAREA "
];
dom.byId("info").innerHTML = "Processing...";
on(this, "load", execute);
function execute() {
query.where = "PIN in (1010027)";
queryTask.execute(query, showResults);
}
function showResults(results) {
var resultItems = [];
var resultCount = results.features.length;
var today = new Date();
for (var i = 0; i < resultCount; i++) {
objPD[0].geometry = results.features[i].geometry;
objPD[0].attributes.PIN = results.features[i].attributes.PIN;
objPD[0].attributes.PAR_AREA = results.features[i].attributes.PDAREA;
newAtt = JSON.stringify(objPD);
dom.byId("info").innerHTML = "Copying PIN: " + objPD[0].attributes.PIN;
var requestHandle = esriRequest({
"url": "http://xxxx/arcgis/rest/services/xxxx/xxxx/FeatureServer/2/addFeatures",
"content": {
"features": newAtt,
"f": "json"}}, {"usePost": true});
requestHandle.then(requestSucceeded, requestFailed);
}
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
function requestSucceeded(response, io) {
dom.byId("info").innerHTML = dojoJson.toJson(response, true);
}
function requestFailed(error, io) {
dom.byId("info").innerHTML = dojoJson.toJson(error, true);
}
});
</script>
</head>
<body>
<div id="info" style="padding: 5px; margin: 5px; background-color: #eee;">
</div>
</body>
</html>
Best Answer
You submit a query to the ArcGIS REST API interface of your ArcGIS Online organization. There is one AGOL organization that is publicly available that is used by Esri for demos.
http://services1.arcgis.com/hLJbHVT9ZrDIzK0I/ArcGIS/rest/services
Here is a feature service with a layer:
http://services1.arcgis.com/hLJbHVT9ZrDIzK0I/ArcGIS/rest/services/EQMagGt4/FeatureServer/0
It is just about submitting a query. In here, I select all earthquake points with a magnitude larger than 5.5:
http://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/EQMagGt4/FeatureServer/0/query?where=MAG+%3E+5.5&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=&units=esriSRUnit_Meter&outFields=*&returnGeometry=true&multipatchOption=&maxAllowableOffset=&geometryPrecision=&outSR=4326&returnIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&quantizationParameters=&sqlFormat=none&f=pgeojson&token=
Remember to set up the proper output spatial reference as well as to choose GeoJSON for your output data format.
Here is a GeoJSON saved as a gist on GitHub.
Your two friends are geojsonlint for validating GeoJSON and geojsonio for interactive review of the GeoJSON and map plotting.
There are cool tools built by Esri, available on GitHub for plotting GeoJSON with JS API.