Code Monkey home page Code Monkey logo

geojson-layer-js's Introduction

geojson-layer-js

NOTE: The ArcGIS API for JavaScript 4 now supports a native GeoJSONLayer. Please use GeoJSONLayer moving forward.

An easy way to load GeoJSON resources into your ArcGIS map. This is a simple custom layer that uses Terraformer to convert GeoJSON to ArcGIS JSON. It "should" support all GraphicLayer operations. e.g. popups, rendering...

View demo app live

App

Features

  • Load GeoJSON from a file
  • Load GeoJSON from a server
  • Load GeoJSON data from a FeatureCollection

Usage

// Example 1: Load from a file
var geoJsonLayer1 = new GeoJsonLayer({
    url: "http://www.myCorsEnabledServer.com/canada.json"
});

// Example 2: Load from a server
var geoJsonLayer2 = new GeoJsonLayer({
    url: "http://opendata.dc.gov/datasets/81a9d9885947483aa2088d81b20bfe66_5.geojson"
});

// Example 3: Load from a FeatureCollection
var geoJsonLayer3 = new GeoJsonLayer({
    data: myFeatureCollection 
});

// Add to map
map.addLayer(geoJsonLayer1);
map.addLayer(geoJsonLayer2);
map.addLayer(geoJsonLayer3);
// options:
//      url: String
//          Path to file or server endpoint. Server must be on the same domain or cors enabled. Or use a proxy.
//      data: Object[]
//          Optional: FeatureCollection of GeoJson features. This will override url if both are provided.
//      maxdraw: Number
//          Optional: Limit the maximum graphics to draw. Default is 1000.

Developer Notes

  • All GeoJSON data needs to be in geographic coordinates (wkid 4326).
  • All GeoJSON resources must reside on the same domain as the app unless a CORS enabled server or a proxy is used. NOTE: GitHub gh-pages servers are not CORS enabled!
  • Terraformer does not support dojo require() and must be loaded directly into the page.
<!-- Terraformer -->
<script src="./vendor/terraformer/terraformer.min.js"></script>
<script src="./vendor/terraformer-arcgis-parser/terraformer-arcgis-parser.min.js"></script>

Example

<!DOCTYPE html> 
<html>  
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>ArcGIS GeoJSON Layer</title>  
<!-- ArcGIS API for JavaScript CSS-->
<link rel="stylesheet" href="//js.arcgis.com/3.9/js/esri/css/esri.css">
<!-- Web Framework CSS - Bootstrap (getbootstrap.com) and Bootstrap-map-js (github.com/esri/bootstrap-map-js) -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="//esri.github.io/bootstrap-map-js/src/css/bootstrapmap.css">
<style>
html, body, #mapDiv {
  height: 100%;
  width: 100%;
}
</style>

<!-- ArcGIS API for JavaScript library references -->
<script src="//js.arcgis.com/3.10"></script>

<!-- Terraformer reference -->
<script src="./vendor/terraformer/terraformer.min.js"></script>
<script src="./vendor/terraformer-arcgis-parser/terraformer-arcgis-parser.min.js"></script>

<script>
    require(["esri/map",
        "./src/geojsonlayer",
        "dojo/on",
        "dojo/dom",
        "dojo/domReady!"],
      function (Map, GeoJsonLayer, on, dom) {

        // Create map
        var map = new Map("mapDiv", {
            basemap: "gray",
            center: [-122.5, 45.5],
            zoom: 5
        });

        map.on("load", function () {
            addGeoJsonLayer("./data/dc-schools.json");
        });

        // Add the layer
        function addGeoJsonLayer(url) {
            // Create the layer
            var geoJsonLayer = new GeoJsonLayer({
                url: url
            });
            // Zoom to layer
            geoJsonLayer.on("update-end", function (e) {
                map.setExtent(e.target.extent.expand(1.2));
            });
            // Add to map
            map.addLayer(geoJsonLayer);
        }
    });
</script>
</head>
<body>
    <div id="mapDiv"></div>
</body>
</html>

Requirements

Resources

Issues

Find a bug or want to request a new feature? Please let us know by submitting an issue. Thank you!

Contributing

Anyone and everyone is welcome to contribute. Please see our guidelines for contributing.

Licensing

Copyright 2013 Esri

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

A copy of the license is available in the repository's license.txt file.

[](Esri Tags: ArcGIS Web Mapping GeoJSON Layer Terraformer Projections) [](Esri Language: JavaScript)

geojson-layer-js's People

Contributors

alaframboise avatar bryant1410 avatar btfou avatar jgravois avatar jtroe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

geojson-layer-js's Issues

GeoJsonLayer not defined

I believe I am getting this error due to my inexperience with javascript.
I am trying to inset GeoJsonLayer into a javascript function someone else has written and everytime I run it I get a Referenceerror GeoJsonLayer is not defined

In the html I have:

<script src="../../vendor/terraformer/terraformer.min.js"></script> <script src="../../vendor/terraformer-arcgis-parser/terraformer-arcgis-parser.min.js"></script>

In the javascript file I have
require(["../../src/geojsonlayer.js"]);

and then in the init function I have

jsonUrl ="http://opendata.dc.gov/datasets/81a9d9885947483aa2088d81b20bfe66_5.geojson";
var geoJsonLayer = new GeoJsonLayer( {url : jsonUrl} );
map.addLayer(geoJsonLayer);

The javascript code I am using does not have the function (... at the beginning so I suspect it has something to do with how I set it up rather then an issue with the code you have provided.
If you could provide some thoughts how I could get it to work it I would very much appreciate it.

Thanks

Require is not defined

Below is the code which I got from this repository to display GeoJSON. Im getting it as a service from ArcGIS online. But I'm getting an error, Uncaught Reference error: Require not defined. Can any one help me on this?

<!DOCTYPE html> 
<html>  
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>ArcGIS GeoJSON Layer</title>  
<style>
html, body, #mapDiv {
  height: 100%;
  width: 100%;
}
</style>

<link rel="stylesheet" href="https://js.arcgis.com/3.9/js/esri/css/esri.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" >
<link rel="stylesheet" href="https://esri.github.io/bootstrap-map-js/src/css/bootstrapmap.css">
 <script src="./vendor/terraformer/terraformer.min.js" </script>
  <script src="https://js.arcgis.com/3.9/"></script>
 <script src="./vendor/terraformer-arcgis-parser/terraformer-arcgis-parser.min.js" </script>

 <script src="./src/geojsonlayer.js"</script>  

<script>
require([
        "esri/map",
        "./src/geojsonlayer",
        "dojo/on",
        "dojo/dom",
        "dojo/domReady!"],
      function (Map, geoJsonLayer, on, dom) {

        // Create map
        var map = new Map("mapDiv", {
            basemap: "gray",
            center: [-122.5, 45.5],
            zoom: 5
        });

        map.on("load", function () {
            addGeoJsonLayer("http://services6.arcgis.com/ZHUwet99mNBqTsuu/ArcGIS/rest/services/ABHI/FeatureServer/0/query?where=city%3D%27BENGALURU%27+&objectIds=&time=&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=&units=esriSRUnit_Meter&outFields=&returnGeometry=true&multipatchOption=&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&quantizationParameters=&sqlFormat=none&f=pgeojson&token=");
        });

        // Add the layer
        function addGeoJsonLayer(url) {
            // Create the laye
            var geoJsonLayer = new GeoJSONLayer({
                url: "http://services6.arcgis.com/ZHUwet99mNBqTsuu/ArcGIS/rest/services/ABHI/FeatureServer/0/query?where=city%3D%27BENGALURU%27+&objectIds=&time=&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=&units=esriSRUnit_Meter&outFields=&returnGeometry=true&multipatchOption=&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&quantizationParameters=&sqlFormat=none&f=pgeojson&token="

            });
            // Zoom to layer
            geoJsonLayer.on("update-end", function (e) {
                map.setExtent(e.target.extent.expand(1.2));
            });
            // Add to map
            map.addLayer(geoJsonLayer);
        }
    });
</script>
</head>
<body>
    <div id="mapDiv"></div>
</body>
</html>

Issues on Running the Plugin With ArcGIS JavaScript API 3.21

Using the plugin with ArcGIS JavaScript API 3.21 and following documentation from Usage , I am getting following warning and errors:

error

<script src="https://js.arcgis.com/3.21/"></script>
<script src="geojsonlayer.js"></script>
<script src="terraformer.min.js"></script>
<script src="terraformer-arcgis-parser.js"></script>
<script>
    var map;
    require(["esri/map",
               "geojsonlayer.js",
               "dojo/on",
               "dojo/domReady!"
    ], function(Map, GeoJsonLayer, on) {
        map = new Map("map", {
            center: [-118, 34.5],
            zoom: 2,
            basemap: "topo"
        });

        var geoJsonLayer = new GeoJsonLayer({
            url: "./data/canada.json"
        });

        map.addLayer(geoJsonLayer);
    });
</script>`

Can you please let me know what I am missing?

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

and

Error: multipleDefine

src: dojoLoader

Showing a single Feature

Hello - The code works great for a FeatureCollection, but it apparently doesn't accept a single Feature. E.g. the following is not accepted:

{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
}

Is this by design?
Thks

No labels display in simple renderer

In the geojsonlayer.html file, I see there is commented out code for the simple renderer. I just want to add a label to display with my layer. The below code doesn't seem to work out of the box for the "label" attribute; no label displays. Is there something else I need to do here?
```
//Optional SimpleRenderer if you don't want a random symbol and color
var simpleJson = {
"type": "simple",
"label": "labels",
"description": "A bunch of stuff about layers",
"symbol": {
"color": [210,105,30,191],
"size": 10,
"angle": 0,
"xoffset": 0,
"yoffset": 0,
"type": "esriSLS" // esriSMS, esriSLS
}
};
geoJsonLayer.renderer = new SimpleRenderer(simpleJson);

Point to 4.x GeoJSON Layer in README

The 4.x JS API now has a native GeoJSON layer, can we update the README to point here for the latest and greatest? And maybe flag this as not actively maintained in favor of that API?

It would also offer a solution for a few of the issues here (display over 1K features, handle different projections, etc)

cc @alaframboise

How can I set symbol color by diferent value?

my code is:
var simpleJson = {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [210,105,30,191],
"size": 10,
"angle": 0,
"xoffset": 0,
"yoffset": 0,
"type": "esriSLS" // esriSMS, esriSLS
}
"visualVariables": [{
"type": "color",
"field": "status",
"stops": [
{ "value": 1, "color": [52,176,0,191]},
{ "value": 2, "color":[254,203,0,191]},
{ "value": 3, "color":[223,1,0,191]}
]
}]
};
geoJsonLayer.renderer = new SimpleRenderer(simpleJson);

The problem is that it doesn't work. Can you give me some suggestion?

[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

I am getting a warning message and unwanted behavior that is effected a mouse-over tooltipDialog popup that I am using on the geojson layers.The error is :" [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience." Is this common? I think its related to this function in the geojsonlayer.js file:

_getGeoJsonXhr: function (url) {
// xhr request to get data
var requestHandle = esriRequest({
url: url,
handleAs: "json"
});
requestHandle.then(lang.hitch(this, this._getGeoJson),
lang.hitch(this, this._errorGetGeoJsonXhr));
},

Is there a way to resolve this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.