Skip to content

Instantly share code, notes, and snippets.

@jseppi
Last active April 6, 2019 17:54
Show Gist options
  • Save jseppi/6515235 to your computer and use it in GitHub Desktop.
Save jseppi/6515235 to your computer and use it in GitHub Desktop.
Leaflet layer that retrieves tiles from an ArcGIS REST Map Service.
# Leaflet layer that retrieves tiles from an ArcGIS REST Map Service.
#
# The 'typical' way to use the ESRI/ArcGIS Server REST API is to request
# a single map image for the entire screen (as seen in the esri-leaflet.js
# DynamicMapLayer class https://github.com/Esri/esri-leaflet).
# That method is slow and the esri-leaflet implementation uses L.ImageOverlay,
# which doesn't mix well with Leaflet TileLayers.
# The implementation here instead is based on L.TileLayer and uses the
# ESRI REST API "export" method to get multiple images as tiles.
L.EsriRestTileLayer = L.TileLayer.extend(
_defaultParams: {
format: 'png8'
transparent: true
f: 'image'
bboxSR: 102100
imageSR: 102100
layers: '' #can specify "hide:layerName1,layerName2" to only show those layers
opacity: 1
}
# See http://resources.esri.com/help/9.3/arcgisserver/apis/rest/ for options
initialize: (url, options) ->
# Trim and add a trailing slash to the url
url = L.Util.trim(url)
if url[url.length-1] isnt "/" then url += "/"
# Make sure url ends with "export/"
if url.indexOf("export/", url.length - "export/".length) is -1
url += "export/"
@_serviceParams = L.Util.extend({}, @_defaultParams)
for opt in options
if !@options.hasOwnProperty(opt) and opt isnt 'crs'
@_serviceParams[opt] = options[opt]
L.TileLayer.prototype.initialize.call(this, url, options)
return
onAdd: (map) ->
@_crs = map.options.crs
L.TileLayer.prototype.onAdd.call(this, map)
return
getTileUrl: (tilePoint) ->
tileSize = @options.tileSize
zoom = @_map.getZoom()
nwPoint = tilePoint.multiplyBy(tileSize)
sePoint = nwPoint.add([tileSize, tileSize])
nw = @_crs.project(@_map.unproject(nwPoint, zoom))
se = @_crs.project(@_map.unproject(sePoint, zoom))
bbox = [nw.x, se.y, se.x, nw.y].join(',')
size = "#{tileSize},#{tileSize}"
params = L.Util.extend({BBOX: bbox, SIZE: size},
@_serviceParams)
url = @_url
return url + L.Util.getParamString(params, url, true)
)
L.esriRestTileLayer = (url, options) -> new L.EsriRestTileLayer(url, options)
@tmamedzadeh
Copy link

What is the difference between this method and L.esri.TiledMapLayer?
https://esri.github.io/esri-leaflet/api-reference/layers/tiled-map-layer.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment