Register  |  Login
ThinkGeo - GPS Tracking and Mapping Solutions  |  Home  |  Cygnus Track  |   Code Community

Discussion Forums

The online community for users of Map Suite GIS components

RSS Feed Available AddThis - Bookmarking and Sharing Button Printer Friendly

PrevPrev NextNext

Using the Spatial Query Capabilities of Map Suite

Posted by ThinkGeo on 11-05-2008 05:16 PM

Tagged As: blog, spatial queries

Map Suite has powerful spatial querying abilities. This allows you to find features of a Layer based on spatial criteria such as containment, intersection etc. This capability is built into the API of Map Suite and is easy even for the non-GIS developer to use. This article will introduce these concepts and show you how they can be accomplished with Map Suite.

Introduction

If you need to build spatial querying capabilities into your maps you can do so quickly and easily using the spatial querying capabilities built into Map Suite. This article will show you the basics of getting started on your way to building your own feature rich spatial applications.

(NOTE: This article references the SpatialQueriesApp that is available in the sample applications with both the trial and full versions of Map Suite. This article also focuses on the Winforms Edition of Map Suite but most of the concepts and all of the example code applies for the Webforms (ASP.NET) Edition as well.)

Register and download the trial version

Opening the Sample Applications in Visual Studio.NET Solution

After you have downloaded and installed the full or trial version of Map Suite, you may want to look through the sample applications and source code that are included with the installation. You can view the samples by going to the START menu...All Programs...ThinkGeo...[YOUR MAP SUITE VERSION] and choose either the VB Sample Apps Source Code or C# Sample Apps Source Code menu item. This will open a solution in Visual Studio.NET that includes all of the Map Suite Sample applications as well as the source code. You may also need to add the Map Suite Map Control to your Toolbox in the Visual Studio.NET IDE. You can do this by using the Add/Remove Items feature and browsing to the winformsedition.DLL file that resides in the installation directory of Map Suite (By default this is C:\Program Files\ThinkGeo\[The Version You Have Installed]\).

Now let's take a look at spatial queries in Map Suite.

(NOTE: This article assumes the basics of loading Shape Files into Map Suite Layers. For a more detailed explanation of how to load Layers and how Map Suite uses shape data and DBF files, see the Map Suite Quick Start User's Guides.)

Why Spatial Queries?

You might be wondering why you would want to use spatial queries. Some good examples of spatial querying might be the need to know the coverage area for a Wi-Fi system or the proximity of possible hazardous materials to schools so that plans can be made accordingly. These are just a couple of examples of how spatial querying can be a powerful tool.

So let's jump right into an example using Map Suite and see how you can get started adding these features to your own applications.

Setting the Map Mode to a Track Shape

The first step in spatial querying is to allow for our user to create a shape that will be used for querying, for example using a rectangle or an ellipse. We can accomplish this by using the Mode property of the Map and one of several mode types from the ModeType Enumeration:

Map1.Mode = ModeType.TrackLine
Map1.Mode = ModeType.TrackPolygon
Map1.Mode = ModeType.TrackRectangle
Map1.Mode = ModeType.TrackCircle
Map1.Mode = ModeType.TrackEllipse

Any one of these values will change the cursor to the mode of selection indicated and allow the user to draw a shape. We now need to be able to create the shape based on what was drawn and we can do this using Map Suite's FinishedTrackShape() Event which takes an argument of type BaseShape. Example code from the SpatialQueriesApp sample application is shown below:

Private Sub Map1_FinishedTrackShape(ByVal Shape As MapSuite.Geometry.BaseShape) Handles Map1.FinishedTrackShape
    If Not Shape Is Nothing Then
        '-- After a shape is drawn, store the shape and re-run the query
        mQueryBaseShape = Shape
        Call SpatialQuery()
    End If
End Sub

The above code captures the BaseShape being passed in and assigns it to the mQueryBaseShape variable which is defined at the Class level as a BaseShape. Immediately after the SpatialQuery() method is called. Let's take a look at that method to get an idea of how Map Suite can now query the data:

Private Sub SpatialQuery()
    Dim RecordNumbers() As Integer
    Dim lyr As Layer
    Dim Index As Integer
    '-- Here we want to deselect everything on each layer
    For Each lyr In Map1.Layers
        lyr.Deselects()
    Next
    '-- Identify which layer with which to work
    If RadioButtonCounties.Checked Then
        Index = 0
    ElseIf RadioButtonRoads.Checked Then
        Index = 1
    ElseIf RadioButtonCities.Checked Then
        Index = 2
    End If
    lyr = Map1.Layers.Item(Index)
    '-- Get the record numbers returned by the query
    RecordNumbers = lyr.SpatialQuery(mQueryBaseShape, CType
    (ComboBoxSpatialRules.SelectedIndex, SpatialQueryContainment))
    '-- Select the returned items on the layer - this will highlight the
    '-- features so the user can see the query results
    lyr.Selects(RecordNumbers)
    '-- Redraw the map
    Map1.Refresh()
End Sub

There are several things occurring in the above code so let's take a look at each step in detail. We first declare an array of type Integer that will hold the record numbers returned from our query. We then declare an object of type Layer that will be used in an iteration to deselect anything that might have been set to selected in a previous query. We then declare an Index of type Integer which will be set when we determine from the user interface which Layer the user wants to query (see user interface of the SpatialQueriesApp in the sample applications).

Now that we have our variables declared we want to go ahead and iterate through all available Layers in the map and deselect anything that might be selected as mentioned above. We then check the user interface RadioButtons to see which one the user has selected. In the case of our SpatialQueriesApp sample application, it is either the Counties, Roads or Cities Layer. After we set the appropriate index, we can now get a reference to that Layer in the Map's Layer collection.

Now comes the magic. With just a simple call to the referenced Layer's SpatialQuery() method, passing in the previously populated BaseShape (mQueryBaseShape) set in the Map's FinishedTrackShape() method and a value from the SpatialQueryContainment Enumeration (NOTE: In the above code, the value is coming from a user interface ComboBox control and then it is cast specifically to type SpatialQueryContainment), an array of record numbers (Integers) is returned. You can then call the Layer's Selects() method and pass in the array. Lastly, we call the Refresh() method on the map. When we do, the appropriate items on the map that resulted from the query will be automatically highlighted, as shown below:

The SpatialQueryContainment Enumeration

Let's take a closer look at the SpatialQueryContainment Enumeration. This provides what type of query operation needs to be performed on the selected Layer features to be queried:

Operation Result
Contained Returns the features that fully contain the querying shape.
Containing Returns the features that the querying shape fully or partially contains.
Fully Containing Returns the features that the querying shape fully contains.
Containing Centroid Returns the features whose centroid is contained by the querying shape.
Fully Outside Returns the features that are fully outside the querying shape.
Outside Returns the features that are fully or partially outside the querying shape.
Intersecting Returns features that are intersected by the querying shape.

An example of each of these query operations is shown below so that you can visually see how each is represented:

(NOTE: The Query Type ComboBox provides reference for which type of query is being shown)

Summary

As you can see, it is very easy to use spatial queries in your Map Suite application. With only a few lines of code, and some representative data, you can have fully functional spatial analysis maps to your users in very little time. Remember to check out the SpatialQueriesApp sample application to see this code and other features such as traversing the map with operations like Zooming, Panning, Track Zoom etc.

Other Resources

There are several other resources at ThinkGeo to help you get started to developing feature rich mapping and spatial applications for Microsoft .NET. Some of those resources are listed below. You can always reach a ThinkGeo support representative by phone toll-free at (866) 847-7510 or via email at support@thinkgeo.com.

Sample Applications

There are 40+ sample applications included with Map Suite when you download the trail version that include the source code to show you how to do various operations using the Map Suite component. It is very beneficial to go through those examples and the code. Both VB.NET and C# are included.

View the Map Suite sample applications online

Discussion Forums

The Map Suite Discussion Forums are a great place to get answers to all of your Map Suite development questions. The ThinkGeo Support Team monitors the forums daily and can help you on your way to being successful with Map Suite.

Visit the Discussion Forums online

4 Comments

SenlinUser is Offline
07-29-2009 04:00 AM
Avatar
Hi,
If I want to find the intersection points of road and circle, what shall I do?
YaleUser is Offline
07-30-2009 01:54 AM
Avatar
Senlin,

Which product are your using? And which version are you using, 3.x or 2.x?

It is better if you can do a post on corresponding forum and made some detailed description on your problem, I am sure you will get feed back soon.

Let me know if any problems.

Thanks.

Yale
SenlinUser is Offline
07-31-2009 02:18 PM
Avatar
Yale,
I have got the answer in the discussion forum.
thank you.

Senlin
RyanUser is Offline
07-31-2009 02:44 PM
Avatar
Glad we could be of assistance.
You are not authorized to post a reply.
Active Forums 4.2