3

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?

3

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.

  • Thanks Alex I will try it out. Can you also suggest me on how to convert ArcGIS GeoJSON to GeoJSON format? I need to use it in Turf.js as it takes the GeoJSON as input. But this ArcGIS format will give me problem. – Bruce Aug 9 '16 at 12:22
  • The GeoJSON you get back from ArcGIS REST API is valid GeoJSON which can be used without any further adjustment. Verify the output coordinate system is 4326 (WGS 84) as I remember I had some difficulties plotting non DD based coordinates. – Alex Tereshenkov Aug 9 '16 at 12:49
  • There may be confusion about ESRI JSON and GeoJSON. The query presented in the answer with f=jsonreturns ESRI version. As stated, "choose GeoJSON for your output data format". – user30184 Aug 9 '16 at 13:00
  • 1
    Sorry for unclear wording. I meant "The query in the answer has f=pgeojson and it returns GeoJSON. If parameter f=pjson is used it will return the ESRI variant, so remember to choose GeoJSON for your output data format." – user30184 Aug 9 '16 at 13:11
  • 1
    Thanks for your suggestions guys!. I need to point out one thing here. When you get GeoJSON from ArcGIS online of type=" Multipoint" it is not recognized as a valid GeoJSON in GeoJSON lint as p is in small case. It should be in caps (type="MultiPoint").Now I'm having problem here! – Bruce Aug 10 '16 at 5:06
1

If you plan on writing a custom application using our JSAPI, it makes more sense to just ask ArcGIS Services for JSON than GeoJSON because that is what the API has been designed to leverage.

That said, if you want to draw features in another API like Leaflet that is oriented toward GeoJSON, we've written open source plugins to simplify the process of leveraging native geojson from services too.

http://esri.github.io/esri-leaflet

on a side note, we will make sure and start returning 'MultiPoint' instead of 'Multipoint' in our next ArcGIS Online release in September.

0

ArcGIS now supports GeoJSON

Now ArcGIS Online has GeoJSON through ArcGIS Rest API URL. All you need to do is set f=geojson in the URL and configure the service. Be aware, by default, ArcGIS online will not allow GeoJSON export until you explicit permit other output formats.

Here is how to enable the export:

  1. Log into arcgis online
  2. click the feature layers,
  3. click the setting tab
  4. check this box that says

    Export Data

    Allow others to export to different formats.

  5. Save and wait a few minutes.

In the query page, you should see the output format dropdown list with the GeoJSON option. The old was called json.

**if you can not config output format of arcserver, means by default, arcgis server rest api always output arcgis json format **

for example http://...query?f=pjson...

You must convert arcgis json to geojson in browser

Converting ArcGIS JSON to GeoJSONThere in pure Browser

there are 2 ways you can do

1) TERRAFORMER

Note: use in node.js and use in browser are different, details see link

2) Esri/arcgis-to-geojson-utils

Esri does not provide the bundle.js, so you can NOT use this lib in browser directly

As of today, you can not load module in browser.

You can only use ESIR lib in node.js, not browser.

To use this esri lib in browser, you should use some bundler, please follow:

a) You need to compile all the module source file into a single bundle.js

rollup.js install by

npm install --global rollup

then go to your js lib root folder, find the entry point js file, in this case it is index.js

$ rollup index.js --format umd --name "esri_arcgis_to_geojson" --file bundle.js

You should find a new file bundle.js in your root directory.

Now in your browser html file, include this bundle.js file

<script src='.../.../.../bundle.js'>

You can use it now by

  // parse ArcGIS JSON, convert it to GeoJSON
  var geojson = esri_arcgis_to_geojson.arcgisToGeoJSON({
                            "x":-122.6764,
                            "y":45.5165,
                            "spatialReference": {
                              "wkid": 4326
                            }
                            });

 // take GeoJSON and convert it to ArcGIS JSON
  var arcgis = esri_arcgis_to_geojson.geojsonToArcGIS({
                            "type": "Point",
                              "coordinates": [45.5165, -122.6764]
                            });enter code here

Remember esri_arcgis_to_geojson is the name you named the lib

This becomes the global variable name, available in browser.

The trick is, bundle process add instant implement function like (function xx {}) here is the top part from bundle.js

  (function (global, factory) {
     typeof exports === 'object' && typeof module !== 'undefined' ? 
        factory(exports) :
      typeof define === 'function' && define.amd ? define(['exports'], 
         factory) :
      (factory((global.arcgis_to_geojson = {})));
      }(this, (function (exports) { 'use strict';

         ***ORIGINAL SOURCE CODE OF JS MODULE***

     })));

arcgis-to-geojson-utils

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.