Using Flex and the ArcGIS for Flex API to write once and deploy everywhere

We developed a map application for one of our clients recently that needed to run in a web browser and also as a stand-alone windows application on a kiosk machine. After tossing a few different ideas around, we quickly settled on Adobe Flex and the ArcGIS for Flex API since the Flash player would allow the application to run in a browser, and Adobe AIR would allow the same application to function as an installed desktop application.

While the applications are 95% similar, there were some key differences. For example, on the kiosk version, the UI elements needed to be larger (e.g. touch screen friendly) and we wanted to be able to print directly to the thermal kiosk printer without displaying a print dialog. This lead to a decision to utilize a shared library to contain much of the business logic, web service delegates, and presentation logic while creating distinct target-specific application projects which would override or extend the shared library code when necessary.

With the advent of Mobile AIR, this concept can be taken one step further. I gave a talk at the 2012 ESRI Mid Atlantic User Group which expands on this concept to utilize a shared code base to power web, desktop, iPhone, and Android clients. In the video below, notice how the web application displays identify results in an Info Window, but the mobile application displays the results as a new view in the view stack – yet, they use the same code to retrieve the data from the server, store that data on the client tier, and render that data on screen.

If you want to dive further into this concept, feel free to fork this (https://github.com/ghoti143/Flex-Multiscreen-Demo) on GitHub.

Towards a High Performance Line of Sight Tool

We have a number of ArcObjects-based applications that require thousands to tens of thousands of Line of Sight Analyses to run to obtain the desired output.  As part of a Research and Development project I was looking for alternate ways of achieving the same result but allow us to optimize performance and have access to finer grained control of the underlying analysis. The following is a description of what I found with the ArcObjects code and the beginning of an alternative solution.

ArcObjects is thread-safe, but some of ArcObject’s operations lock out concurrent processing to ensure thread safety.

I discovered this fact while working on an algorithm that performs several calls to ISurface::GetLineOfSight.  After profiling the algorithm, I discovered that the line of sight operation was the algorithm’s bottleneck.  To get more speed, I multi-threaded the algorithm.  The result was unexpected: The algorithm took twice as long to process and required twice as much processing power.

I came to the conclusion that I needed to write my own line of sight algorithm.

After a bit of research, I decided to use a Triangle-Ray Intersection technique (used by some ray tracing methods).  The method requires converting the surface to a collection of 3D triangles.  To determine the intersection point, search through the triangle collection for an intersection with the ray.

(Source: http://www.cs.cornell.edu/courses/cs465/2003fa/homeworks/raytri.pdf)

The reason to use triangles is that the math becomes much simpler (and faster).  In fact, there are no computationally complex (and slow) mathematical operations.  Take a look at the code:

Granted, the math is complex, but the math isn’t computationally complex.  The above method, “flattens” the surface and then determines the intersection of the triangle’s plane with the ray.  The PointInTriangle method then determines if the intersection point is inside the triangle.

The way the PointInTriangle method works is by checking which “side” of each of the triangle’s lines the point is on.  If the point is outside of the triangle, the point will be on the left side of 2 of the lines, and on the right of the other (1: Left, 2: Left, 3: Right).  If the point is inside of the triangle, the point will be on the same side of each line.

So far, my implemented version is 10x slower, but just as accurate as the ArcObjects line of sight operation (the trick is to use the same points to build the triangle collection).  But not to worry, there is plenty of room for improvement.

Currently, the triangles are stored in a 2D array.  A more efficient data structure will greatly improve performance.  Another positive note is that this implementation is thread-safe and doesn’t lock out concurrent processing.

This implementation can be modified for GPU processing.  Using a GPU technique, every triangle can be checked for intersection at the same time.

In conclusion, there is a lot of potential using GPUs to perform line of sight operations.  Transferring the surface’s data to the GPU is a relatively considerable investment (about 2-3 ms).  Luckily, when performing thousands of line of sight operations, the data transfer happens only once at the beginning.  Compared to ArcObjects running on a single thread, we can expect to see at least a 14 times speed up.  This means an algorithm that previously took an hour to run could potentially run in 4 minutes.

It took a bit of research to determine a good method of implementation.  Luckily there were several sources out there to read and use:

http://www.cs.cornell.edu/courses/cs465/2003fa/homeworks/raytri.pdf

http://en.wikipedia.org/wiki/Ray_tracing

http://en.wikipedia.org/wiki/Line-plane_intersection

http://www.codeproject.com/KB/graphics/Simple_Ray_Tracing_in_C_2.aspx

http://t-ray.googlecode.com/svn/trunk/ORayTracer/RayTracer/tTriangle.cs

Cross Browser Mobile Application Solutions

The future is all about mobile. We hear this more and more. Anybody who followed the recent Adobe Max conference in LA will have seen Adobes vision of the future; cloud computing and mobile. But in 2011, the mobile space remains confusing; smartphones, tablets, native apps, hybrid apps, mobile web apps. Where do we begin?

Let’s try to demystify things a little.

Mobile Hardware and Software

There are two basic types of mobile hardware: smartphones and tablets. There are big variations in each category, one of the most critical is size. Size as it relates to portability, and more importantly screen. Smartphone screens vary from 2.1″ to just over 4″; in contrast tablets have a range of 7″ to 10.1″. Why is screen size so critical? Try interacting with an ArcGIS map on a 2.1″ screen!

Users can interact with mobile applications in one of two ways. They can use their Web browser to access a Web site, or they can download and install an application from one of the Mobile App stores. Most Web pages have been built for the PC. This means mouse interaction. Increasingly, Web sites are being optimized for mobile viewing and interaction. This means built for use on smaller screens, and allowing for finger interaction. This usually requires multiple Web sites; one for PC’s and one or more for mobiles. Now suppose your company has taken advantage of the excellent Flex Viewer for ArcGIS to give access to your spatial data. You share with your users the news of a new widget you have added, one perfect for use on mobiles. The deluge of angry iPhone and iPad users, complaining they cannot access the Web site, makes you regret your announcement. It’s an unfortunate reality that both Flash and Silverlight will not run on the browser of any Apple device.

Visiting Apple’s App store or Google’s Android Market, users can access an array of mobile apps. These are installed mobile apps; both free and fee based. A simple search on GIS or maps in an App Store produces a nice list of mobile apps. Let’s imagine you have a unique idea for a mobile application; one you’d like to push into one of these stores. Where do you begin? In the old days, we are talking here about months not years, you would first choose the platform to target for the app. That was usually Apples iOS, for running on the iPad or iPhone. Next hire an Objective C programmer. And build a so-called native mobile application. Once done, launch it into the Apple marketplace and watch it jump off the shelves, figuratively. Android from Google has seen increasing popularity. Suppose you wanted to take your app and push it into the Android market. Stop. Rewind. Hire a Java programmer. Have him rebuild the application. Push it to the Android Market. You get the idea; a single mobile app running on different platforms requires multiple rewrites. When you add in bug fixing and updates, this has the potential to be expensive.

Cross Platform Mobile Apps

In a perfect world we should be able to write an application once and run it across all mobile devices. Whether it be a Web or installed application respectively. Thanks to HTML5 and Adobe Air this is now possible. HTML5 is an augmentation of the current HTML 4.01 specification, which adds new features that designers and developers will be able to use. Important new additions include multimedia elements and dynamic graphics, allowing for improvements in data visualization, image manipulation, and video. In combination with Javascript, HTML5 presents some new and exciting possibilities for Web development. Apple iOS, Android, BlackBerry 6, and HP/Palm WebOS devices all support HTML5 features. HTML5 can be a compelling Web solution for multi-device and multi-screen applications.

Adobe AIR is a sister technology to Adobe Flex. AIR does not run in a browser like Flex; it is used to create an installed application, which has access to more local resources than are available to Flex. Mobile AIR is a new flavor of AIR, designed specifically for mobile devices. Adobe has made some important changes to their existing development tools. Now a project built in mobile Adobe AIR can be cross compiled to run on Apple iOS, Android and Blackberry. That is one code base for multiple platforms!

So now an application built in HTML5 will run on ALL mobile web browsers. An application built in mobile AIR, and distributed in the App stores, can be installed on all major mobile devices. That is huge. One more thing. If you build your HTML5 app for the mobile Web and at some time in the future decide you want this to be an installed app. Step forward PhoneGap; an open source tool to convert an HTML5 app to a cross platform installable app. So there is one code base for both Web and installed apps!

We live in exciting times. It could well be that we are seeing the end of the PC. Applications which are location-focused are a huge part of the new mobile revolution. The cross platform development tools, described in this article, available to designers and developers, will simplify the building of state of the art mobile solutions.

Flex Paths and Button Skins

So you’re building a Flex application. And you want to skin your buttons. But, alas, you’re tired of rectangular buttons. Never fear, Flex paths are here.

First you need to define the coordinates of the vector shape you want to create.

<fx:Declarations>

<fx:String id="arrowPath">

M 0 0

L 20 0

L 100 0

L 120 20

L 100 40

L 0 40

L 20 20

L 0 0

</fx:String>

</fx:Declarations>

Then, you’ll need to create a Graphic object. You can use a scale9Grid to fit the width of the label text without distortion.

<s:Graphic includeIn="up"

height="100%" width="100%"

scaleGridTop="1" scaleGridBottom="39"

scaleGridLeft="20" scaleGridRight="100">

<s:Path data="{arrowPath}">

<s:fill>

<s:LinearGradient id="gradientUp" rotation="90">

<s:GradientEntry color="0x4e77b3"/>

<s:GradientEntry color="0x345c98"/>

</s:LinearGradient>

</s:fill>

<s:stroke>

<s:LinearGradientStroke rotation="90">

<s:GradientEntry color="0x7399CF"/>

<s:GradientEntry color="0xd3d3d3"/>

<s:GradientEntry color="0x23467B"/>

</s:LinearGradientStroke>

</s:stroke>

</s:Path>

</s:Graphic>

You’re on a roll now, so you toss a few buttons into a button bar, back ‘em up so they saddle up next to each other, and you’ve got something like this:

Bada bing.  Bada boom.

Five Reasons You Should Be Excited About the 10.1 ArcGIS Runtime SDK

With ArcGIS version 10.1, Esri will introduce the ArcGIS Runtime and associated SDKs. There’s already a lot of buzz about the Runtime in the developer community, and for good reason. The runtime was been architected from the ground up with an eye on addressing some familiar challenges for GIS developers: exceedingly complex, fine-grained object models; complicated deployment; poor performance; large and memory intensive applications; lack of native 64-bit support … the list goes on. Esri hopes to eliminate many of these “pain points” with the new Runtime SDK, and I think most of us are also ready to let the healing begin. With its powerful yet coarse-grained architecture, it might be tempting to view the new ArcGIS Runtime as MapObjects and ArcGIS Engines love child, but there are (at least) five reasons why it’s more than that.

1)      Simplified deployment

You may have already seen the now almost legendary demo where an Esri dev drags his Runtime project on to a thumb drive, walks it to another machine and fires it up without an install. It never fails to gets gasps from the crowd, and for good reason. How many times have you had to sheepishly tell your client “hmm, it works on my machine”? The ArcGIS Runtime eliminates complicated deployments by packaging everything required by the application into a (relatively) small deployment package. A simple tracking application I built with a pre-beta version of the runtime weighed in at about 175 megs. Since the Runtime is split into several “functionality sets”, you can keep your deployment lean by eliminating functionality your application doesn’t require. As a bonus, a Runtime application doesn’t care about other versions of ArcGIS you might have installed, and will happily run side-by-side with its progenitors.

2)      Performance

The ArcGIS Runtime has been architected to take advantage of available CPU resources, including multiple processors and cores. It provides true multithreading and native 32 and 64-bit support. It also supports an asynchronous programming pattern (made possible by its multithreaded architecture) that contributes to an improved user experience. The ArcGIS Runtime display architecture has also been optimized for speed.

3)     Connected and disconnected modes

Both local and Web data sources for the Runtime use the same programming model, which means it’s easy to build connected and disconnected modes into your application. Since your application’s data source can be based on map (or tile, locator, geoprocessing, etc.) packages, these data can be downloaded from ArcGIS.com on the initial load and then used locally thereafter. A common and effective approach is to use online data for your base map, while deploying a map package with your application to contain operational data. Editing can also be performed in a disconnected manner, using the geodatabase check-in/check-out replication model.

4)     Intuitive object model

While functionally more powerful than MapObjects, the ArcGIS Runtime SDK is considerably more coarse grained than ArcGIS Engine. In other words, it delivers all the functionality that most GIS applications need without an overly complex object model. Under the hood, the Runtime uses REST for communication with both Web and local data sources. Developers who are familiar with any of the ArcGIS Web APIs should find the Runtime API very intuitive to work with right out of the gate.

5)     Multi-platform support

The ArcGIS Runtime supports applications for 32-bit Windows, 64-bit Windows, and for 64-bit Linux. Windows applications can be built using the WPF, Java, or QT SDKs. Linux applications can be built using Java or QT.

While your applications still won’t be writing themselves anytime soon, the ArcGIS Runtime SDK should make GIS development a little more enjoyable. The time you may have had to spend smoothing out your deployment or pouring over a couple dozen object model diagrams can now be spent doing something fun (or at least productive), like fine-tuning your cartography or making tweaks to your application’s UI.

Follow

Get every new post delivered to your Inbox.