Tag Archives: JavaScript

Feature layers can generalize geometries on the fly

Last week, we discussed vector tiling for improving performance of feature layers. While awesome in its own right, if you’re trying to push twenty megabytes of features over the wire, vector tiling isn’t going to save you. Your app will be slow.

How should complex geometries be handled/served? The intuitive approach is to use multiple layers in a service and, depending on the map’s scale, display different layers with varying levels of geometric detail. And why not? You have the tools and you know the API will let you easily craft a solution; it’ll just take a little time to code the thing.

Well, frankly, that’s a lot of work. Feature generalization is a common enough scenario that you would think the API should just know how to do this type of thing. Thankfully, it does.

When you create a Feature Layer, one of the options is maxAllowableOffset. You might recognize this parameter name from the ArcGIS Desktop generalization tools, where it’s been used for years. For more technical details, please refer to the Wikipedia page for the Douglas–Peucker algorithm. If this is new terminology, go check out the ArcGIS Desktop Help for the Generalize tool.

Another bonus is that the FeatureLayer comes with a setter method setMaxAllowableOffset that you can use to simplify features on the fly. Since the web APIs fire an event when the map’s extent/zoom level changes, you can listen for this event and use setMaxAllowableOffset to indicate an appropriate value.

OK, so now all we have to do is figure out a value for maxAllowableOffset. This can take some trial and error, but let me suggest a method: a feature’s geometry should not display more than one vertex per pixel. After all, a pixel is the smallest unit for our displays, so displaying more than one vertex per pixel is wasted effort. If we want to display no more than one vertex per pixel, we can take the map’s width in coordinate space divided by the map’s width in pixels and we have a reasonable value for maxAllowableOffset. With this method, you are telling your Feature Layer to make sure simplified features do differ from the original by more than the width of a pixel. In fact, this is exactly what happens in the Generalize Features JavaScript API sample.

But wait, there’s more! Because this is such a useful idea, ArcGIS.com does it for you automatically. Whenever you add a feature layer to an ArcGIS Online map, maxAllowableOffset is calculated and set appropriately. This results in a better performing map and feature layers that load faster than if complex geometries were sent across the network.

Finally, one caveat. You can’t specify maxAllowableOffset on layers that you allow users to edit. If you’re editing a feature, you need to see the true representation of that feature, including all its participating vertices. Editing a generalized feature and then sending it back to the server could wipe out detailed information that was meticulously created and maintained. We wouldn’t want that, so you can’t specify maxAllowableOffset on Feature Layers that are editable.

Contributed by Derek Swingley of the ArcGIS API for JavaScript development team

Posted in Services | Tagged , , | 2 Comments

Out of the box vector tiling using feature layers

Feature Layers arrived with ArcGIS 10 and the 2.0 release of the ArcGIS web APIs. They provide a flexible way to retrieve (and optionally edit) vector features from a web service. To highlight some of the advantages and innovative aspects of feature layers, we’ll be sharing a series of blog posts about them in the weeks to come. The focus of this post (if you haven’t already guessed): vector tiling.

Vector tiling is a relatively new topic in the realm of web maps. Perhaps you’ve used the raster tiles that you can create en masse with ArcGIS Server to speed up your web app. Vector tiles are a little different. The underlying idea is that a static grid is used to issue a series of requests to retrieve vector features. The features are added to the map, but they are also stored in your browser cache.

What’s the motivation behind vector tiling? Some benefits include:

  • Dynamic interaction with individual features to change feature symbols, make selections, or work with feature attributes without going back to the server
  • Features are retrieved from the browser cache when the same request (URL) is issued more than once (think HTTP status code 304)
  • Only features that are in the map’s current extent are automatically retrieved
  • Improved user experience and performance by using multiple queries to retrieve features in reasonable numbers rather than a single query to retrieve all features

One key thing to point out, especially for veteran uses of ArcGIS Server that are familiar with traditional map caches, is that vector tiles are generated on the fly and they are not persisted on the server. For vector tiles, there’s no cache building and no additional server maintenance required. Vector tiles are only cached on the client and are used on a session-by-session basis.

As the title of this post says, you get vector tiling out of the box with ArcGIS Server and Feature Layers. To take advantage of a Feature Layer’s vector tiling capabilities, the key is to use ONDEMAND mode. The code to set this up can be as simple as:

var featureLayer = new esri.layers.FeatureLayer(layer_url ,{
  mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
  outFields: ["*"]
});

Let’s look at a sample: JavaScript API Feature Layer ONDEMAND. If you look for it, you can see hydro features being drawn in chunks as you move around the map. These are the tile-based queries doing their thing. To explore further, pop open Firebug or the Chrome Developer Tools and take a look at the Net tab or the Network pane. Pan to get the feature layer to make some requests back to the server. Initially, you’ll see requests to the server to retrieve features. If the map ends up somewhere it’s been before, you’ll start seeing HTTP 304s being returned:

304 errors

What does a 304 mean? In a nutshell, it’s the server saying it’s already responded to this request (URL) and that the browser should use the previous response. When this happens, the server doesn’t send any features over the wire and the browser uses the previous response by pulling it out of the browser cache. Pretty cool. Because the Feature Layer uses a grid to generate query URLs, and the browser caches responses to these URLs, the server doesn’t need to send the same features over the wire multiple times.

One issue that vector tiling doesn’t directly address is generalizing features. As a map scale changes, the amount of detail required for each feature changes. If you’re at a small scale, you need more generalized features. At larger scales, features need to show more detail. Feature Layers can handle this efficiently but that is a topic for another post. For those who can’t wait and want to explore on their own, have a look at the maxAllowableOffset property on the FeatureLayer.

Contributed by Derek Swingley of the ArcGIS API for JavaScript development team

Posted in Services | Tagged , , | 3 Comments

Using ArcGIS Online hosted applications

ArcGIS Online includes a variety of application templates that are designed for you to download, tweak the source code to suit your needs, and publish the app with your map from your own site. While these are designed with customization in mind, they can also be used “as-is” as ArcGIS Online free hosted applications. Here’s how to use them as hosted apps.

First, find a map you’ve shared publicly, or any other publicly shared map. For this example we’ll choose one from the ArcGIS.com gallery showing USA active floods:

 

Once you’ve opened the map in the ArcGIS.com map viewer, choose share:

And then choose Make a Web Application:

 

We’ll want to preview the map in one of the available app templates. Below we’ve chosen the Contemporary Green template. You’ll see the Preview option underneath each template thumbnail.

When you click Preview your map will open in the template you’ve chosen. Shown below is the USA Active Floods map in the Contemporary Green template. To use the map and template as a complete hosted application, just preview your map in the template and copy the URL you see in your browser. You can paste the URL into an email, add it as a link on your website, or share it via your ArcGIS Online account.

Here’s a link to the USA Active Floods in the Contemporary Green template or click the image below:

 

If you want to modify the app or add additional widgets, choose the Download option (rather than Preview as we did above) to get the source JavaScript code.

If you want to publish your customized template you’ll have to do that from your own server or web hosting service. But if you just want a quick map in a different template, leveraging the already hosted ArcGIS Online applications provides a quick and easy solution that you can use with any map.

Posted in ArcGIS Online, Services | Tagged , , , , | 1 Comment

Geolocation HTML5

With the recent release of Internet Explorer 9 (IE9), I decided to create and test a sample application using the geolocation API in all four major browsers (Safari 5, Firefox 4, Chrome 10, and IE9) using HTML5 and JavaScript. Although there was some support for geolocation functionality prior to HTML5, there was no standardization.

How does it work?

The geolocation API is published through a geolocation object, which is a child object of the navigator object. If the object exists, geolocation services are available. Sounds simple, huh? As a matter fact, here is how to quickly test for geolocation:


if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition

}

else {

alert(“Geolocation not supported on your browser.”);

}

So what information is being sent when you visit an application with geolocation support? The browser will ask if you want to share your location; if the user allows the application to acquire your location, then the browser will send your computer’s IP address and a collection of information of your surrounding Wi-Fi access points to their default location service provider (e.g., Google Location Services, Microsoft Location Services). This information includes MAC address, SSID, age, (how long ago in milliseconds since the Wi-Fi node was detected), and signal strength of all the Wi-Fi access points around you.

The location service then checks this information against a database, finds the location of all the access points provided, and using triangulation, returns your location. If no Wi-Fi access points are in range, or your computer does not have Wi-Fi, it may only use your computer’s IP to get an approximate location. The accuracy of geolocation varies, and in some cases, it may not be able to provide a location at all. More importantly, you will see different results across browsers.

The first thing that came to my mind about these location services was how they know where all these access points are located. Do you remember those Google cars driving around collecting Street View data? Well that was one way. Another company collecting data is Skyhook, which also has drivers driving around collecting data. Another way data is collected is cross-referencing Wi-Fi data with mobile cell towers and GPS data available from smartphones.

Test Results

First of all, I used our ArcGIS API for JavaScript version 2.3, which is an early preview of our API and is only for testing purposes. The reason why I used this version is that it will support IE9, which was released in early March along with our 2.2 version. All of my tests were done with a hard connection; they were Wi-Fi enabled but not connected to a wireless network.

IE9

My first test was with IE9, which was pretty accurate. My office is a little more left to the edge of the building but this location works for me.

Firefox

My next test was with Firefox and still on the same row but slightly more to the right.


Chrome

Here is where it got interesting: using Chrome, it located me outside the building. Just a minute ago I used Firefox, which uses Google Location Services just like Chrome. I expected them to be identical in my location.

Safari

Last but not least, I used Safari, and if I am not mistaken, I think they use a different location service than the others. As you can see my location is still fairly close but not as accurate as IE9 or Firefox.

I had some of my colleagues test as well, who are located a couple of offices down the hall and they didn’t yield the same results as I did. In the image below I used our topographic map to show the detail of our campus location along with a blue dot representing my location. The geolocation marker is approximately 0.6 miles away. Since they did not receive the exact location as me, I decided to have them try it while connected to a wireless network. Below you can see they had better results when connected to a wireless network.

Hard Connection

Wireless Connection

In conclusion we learned the following:

  • Geolocation API works in all four browsers that support HTML5.
  • Location requests with Wi-Fi data are more accurate that those based on just an IP address alone.
  • Result will vary across browsers, users, and I am sure where location data is scarce.
  • The location becomes cached, which will improve the location services.

Although I did not touch upon mobile devices in this post, I did try this code with an iPhone and an iPad, and they both located me in the parking area of our building. I also tried testing it on my Motorola Droid x running Android 2.2 and Windows Phone Samsung, but they don’t have an HTML5 browser. However, once they do have full support for HTML5, it will be nice to write in HTML5 and JavaScript for mobile applications that can deliver features based on location services, for example, a map that can center based on where you actually are and display relevant data based on your surroundings.

Here is a geolocation sample that you can test for yourself, and if you have accurate or inaccurate results or discover something we didn’t, please feel free to share.

 

David Martinez

EDN Team

Posted in Developer | Tagged , , , , | 3 Comments

IE 9 support coming in version 2.3 of the ArcGIS API for JavaScript

Version 2.2 of the ArcGIS API for JavaScript was released in early March. . . .right around the time that Microsoft announced that Internet Explorer 9.0 was final and Dojo announced that version 1.6 was available. Version 2.2 does not support either of these, but version 2.3, planned for early May, will support them. We’ve put together an early version of 2.3 to demonstrate how the IE9 enhancements lead to a better mapping experience.

Internet Explorer 9

Internet Explorer 9.0 provides many new features including improved support for CSS3 and HTML5 and dramatic improvements in graphics performance. In earlier versions of Internet Explorer, drawing 1000+ graphics performed poorly compared to browsers like Firefox and Chrome. At IE9, graphics drawing performance is much improved over earlier versions of IE and is comparable to other browsers.

We created a simple test case that draws 1000 graphics on the map to test the performance improvements in Internet Explorer. This table shows our test results, in milliseconds. You can run the test on your machine using the code here. Note that your results may vary depending on the specs of the test machine.

 

Internet Explorer 9 now supports CSS3 behaviors like rounded corners, media queries, transforms and more. The image below shows a sample application in Internet Explorer 8. The css for this application specifies that the corners should be rounded and the header should have a box-shadow applied. However, IE 8 does not support the border-radius or box-shadow properties and they are ignored.

View Application in IE 

 

 

 

 

 

 

 

Here’s the same sample running in IE9, note that the rounded corners and box shadow are now applied.

 View application in Ie

HTML5 support was also enhanced, so now you can take advantage of capabilities like the Canvas Element and Geolocation in IE 9.

  • The HTML5 Canvas Element is a space on the page you can use to render images on the fly. Click here to view a sample that demonstrates using the Canvas Element to draw a raster image.
  • The Geolocation API which can be used to find the user’s current location is now supported across all major browsers. You can test this using the Geolocation sample.

For more details about CSS3 and HTML5 support in IE9 visit caniuse.com.

Dojo 1.6

Enhancements to Dojo 1.6 include Dojox.Mobile enhancements and a growing set of tutorials on various aspects of working with Dojo and Dojox.Mobile. Version 2.3 of the ArcGIS API for JavaScript will use Dojo 1.6.1 which is due out any day now and is scheduled to include several bug fixes for IE9.

Contributed by Kelly Hutchins of the ArcGIS API for JavaScript development team

Posted in Services | Tagged , , | Leave a comment

ArcGIS API for JavaScript Tips-n-Tricks: Parsing GeoRSS with jQuery

There are quite a few reasons why GeoRSS feeds can be tricky to parse. The reasons include confusing child-parent tag relationships, and some parsers simply don’t work with namespaces (e.g. <geo:lat>) depending on which browser you are using. This is where jQuery comes in very handy, especially if you want your app to function well across the major browsers. The pattern that you can use looks like this:

//Look for the tag <geo:lat> 
$(data).find(“[nodeName='geo:lat']“).text()

Feel free to click here to see a live example and view the source code. Or just download the source and try it out on your machine.

References:

http://api.jquery.com/find/
http://api.jquery.com/jQuery.ajax/
http://docs.jquery.com/Tutorials:How_to_Get_Anything_You_Want

Posted in Developer | Tagged , , , , , | 2 Comments

Experimenting with HTML5 and the ArcGIS API for JavaScript

We had a wonderful time getting to know many of you this
week at the Esri
Developer Summit
in Palm Springs, and especially enjoyed seeing the cool
applications you’re building with the ArcGIS API for JavaScript. Thank you also
for the plentiful feedback on the API.

During our JavaScript technical sessions, we showed several
applications that take advantage of HTML5 functionality like local storage,
native drag and drop, file access, offline storage and more. These
samples are still in an experimental stage and may not work on all browsers,
but we wanted to share them with you so you can start planning and prototyping
interesting applications that combine HTML5, CSS3 and the ArcGIS API for
JavaScript.

Experimental folder in JavaScript samplesTo view the samples, visit the ArcGIS API for JavaScript Resource
Center Samples
Overview
page and expand the Experimental folder. To determine the level of support in various browsers, visit caniuse.com,
a web site with compatibility tables for HTML5 and CSS3 support.

For those of you who missed the conference, the
technical sessions were recorded and the videos will be available in a few
weeks on proceedings.esri.com.
We look forward to seeing you in Palm Springs at next year’s Developer Summit.

Contributed by Kelly Hutchins of the ArcGIS API for JavaScript development team

Posted in Services | Tagged , , | 1 Comment

Version 2.2 of the ArcGIS API for JavaScript released

Version 2.2 of the ArcGIS API for JavaScript is now available. To begin using the new version, update the script and style sheet tags in your applications to point to version 2.2  or version 2.2compact.

Version 2.2 introduces many oft-requested features that will improve the usability of the Web apps you build. These include:

  • Continuous pan across the dateline – If your map uses the WGS 1984 geographic coordinate system or the WGS 1984 Web Mercator projected coordinate system (including the Auxiliary Sphere option), you can pan across the 180 longitude line without encountering the “edge of the earth”. 

    Continuous Pan

  • Support for ArcGIS.com map notes and popups - The next release of the ArcGIS.com Map Viewer, slated for release within a matter of days, will allow you to define popups and save map notes inside the maps you build. When you reference an ArcGIS.com map ID in your JavaScript application, the popups and notes are immediately accessible. This allows you to design your popups interactively in ArcGIS.com instead of through code.

    ArcGIS.com Popup

  • Enhanced Mobile Support – Version 2.2 adds support for the Blackberry Torch (6.0) native browser. You’ll also notice that widgets such as the zoom level slider and time slider are now easier to use on touch screen devices. Explore the samples and code gallery on your iOS, Android or Blackberry Torch smart phone using the mobile site.
  • Customizable Attribute Inspector – The Attribute Inspector now allows you to define which fields in the dataset are editable. You can show the values of the fields while still preventing edits. You can also set custom labels and tooltips on attributes, and even write custom widgets for supplying field values.               

    Attribute Inspector

  • Customizable info window content – You can now use functions to define info window content, allowing you to more easily include a variety of widgets, media, and other content inside your info windows.               

    Info Window

  • Extensible info window using the InfoWindowBase class – A new API architecture for info windows allows you to more easily customize info window look and behavior.                                     

    Info Window

               

Visit What’s New in Version 2.2 for more details and links to specific samples.

Posted in Services | Tagged , | 5 Comments

ArcGIS API for JavaScript samples page supports mobile devices

ArcGIS API for JavaScript samples are now mobile-friendlyWith the growing popularity of mobile devices, the JavaScript team decided to build a mobile version of the ArcGIS JavaScript API Resource Center sample page. The new site allows you to browse and test the JavaScript samples and Code Gallery from your mobile device. To access the mobile site, simply browse to http://links.esri.com/javascript from your mobile device and you will be redirected to the mobile page. Links from the mobile site provide access back to the full version if you want access to functionality not available through the mobile page such as forums, API reference or conceptual documentation.

To build the new site we used jQueryMobile, a framework that simplifies the process of building Web applications that work across multiple mobile device platforms. jQueryMobile 1.0 Alpha 2 was released late last year and according to the jQuery mobile framework blog the final 1.0 version is due in January. Despite the current Alpha status, I found the framework easy to learn due to the active community forum and excellent documentation.

You might be asking at this point, can I use jQueryMobile to build mapping applications, too? This is absolutely possible using the new compact build of the ArcGIS API for JavaScript. Here’s a sneak peek at an application using jQueryMobile that will soon be added to the Samples page. To learn more about building Web applications with mobile frameworks visit us at the ESRI Developer Summit in March.

Contributed by Kelly Hutchins of the ArcGIS API for JavaScript development team

Posted in Services | Tagged , , | Leave a comment

Changing the display of image services dynamically in a Web API

Last week, we posted up a sample web application to navigate and explore an image service. This week we will examine some of the Silverlight code that was used in the sample.

Web clients can dynamically change the display of image services, such as format, band composition, interpolation, compression quality, nodata value, projection, and (starting from ArcGIS Server 10) clients can also specify a raster function to be applied by ArcGIS Server.


Band Combination

Changing band composition on-the-fly can be particularly useful when service has more than 3 bands, such as a Landsat service. This give you the ability to change to true color or false color composition.
isLayer.BandIds = new int[]{2,1,0}; //0-based; shown in Silverlight code


Format

You can change format of the returned service on-the-fly. For instance, you may use the new “JPGPNG” format, which returns a PNG when the request extent contains transparent pixels, and returns a JPG when there are no transparent pixels. JPGPNG is the default format in Web API and is recommended for web developers.
isLayer.ImageFormat = ArcGISImageServiceLayer.ImageServiceImageFormat.JPGPNG;


Compression Quality

The compression quality is a useful way to transmit the service faster when limited bandwidth is an issue. A lower compression quality transmits a lower quality image, but the image can be transmitted faster. Compression quality only applies when the JPG format is used. Compression quality ranges from 1 (low quality) to 100 (high quality).
isLayer.CompressionQuality = 50;


Interpolation
The display resampling method can be customized for your service. You should use bilinear or cubic for continuous data such as elevation of imagery. You should use nearest neighbor or majority for discrete data such as land use classification.
isLayer.Interpolation = ArcGISImageServiceLayer.ImageServiceInterpolation.BilinearInterpolation;

NoData

You can define a nodata value for any pixels that should not have a pixel value, such as the background border of a satellite image. When you display Nodata as a PNG format, these pixels will be displayed as transparent.
isLayer.NoData = 0;

Raster Functions
At ArcGIS 10, client can specify a rendering rule and ask ArcGIS Server to apply it on top of image service. This is done through a number of raster functions, 8 raster functions are supported through REST: Hillshade, ShadedRelief, Slope, Aspect, Colormap, Statistics, Stretch, NDVI. Web clients can leverage these raster functions to achieve different visualization goals. In many cases, you may want to apply a different stretch on an RGB service, or a color map on a single band service, and a shaded relief on an elevation service.

RenderingRule renderingRule = new RenderingRule();
renderingRule.RasterFunctionName = “Hillshade”; //Refer function name in REST SDK
functionArguments.Add(“Azimuth”, 315.0); //Refer arguments in REST SDK
functionArguments.Add(“Altitude”, 45.0);
functionArguments.Add(“ZFactor”, 1.0);
renderingRule.RasterFunctionArguments = functionArguments;
isLayer.RenderingRule = renderingRule;
isLayer.Refresh();

You can view live samples online: JavaScript, Flex, Silverlight.
As described above, image service web API provides a number of different ways that you can change the appearance of image service on the fly. You can expose them in web applications so users can visualize image services on demand. This means that you can use fewer services to serve multiple purposes. For example, one Landsat service is enough for true-color, false-color, and NDVI display; one elevation service is enough for slope, aspect and shaded relief visualization.  Note: Image services are automatically projected to the map’s coordinate system when used in Web APIs.

Submitted by: Wenxue Ju


Posted in Imagery, Services | Tagged , , , , , , , , | 4 Comments