Building a Custom Query

February 17, 2009

Years ago, I used to do a lot of programming with MS Access. Somewhere along the way I saw an interesting code sample of how to write a custom query. I have used the methodology described ever since. There may be better solutions available, and I am open to them, but I have used variations of the following code in all types of web pages and programs.

First Step:

  1. Create Search Form
    parcelsearch1
  2. Code The Search ButtonPrivate Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
    ‘On Error GoTo ActError

    ‘Query which searchs by user input
    Dim strwhere As String

    ‘Parcel Query Defaults
    strOwner = gtxtOwner
    strHouseNum = gtxtHouseNum
    strStreetName = gtxtStreetName
    strMap = gtxtMap
    strGroup = gtxtGroup
    strParcel = gttxtParcel
    strParcelFile = gtxtParcelFile
    strParcelID = gtxtParcelID
    With cmdSearch

    strwhere = “‘”

    If Len(txtOwnerSearch.Text) > 0 Then

    strwhere = ” and ” & strOwner & ” like ‘” & txtOwnerSearch.Text & “%’”
    End If

    If Len(txtCMAPSearch.Text) > 0 Then

    strwhere = strwhere & ” and ” & strMap & ” = ‘” & txtCMAPSearch.Text & “‘”

    End If

    If Len(txtGPSearch.Text) > 0 Then

    strwhere = strwhere & ” and ” & strGroup & ” = ‘” & txtGPSearch.Text & “‘”

    End If

    If Len(txtParcelSearch.Text) > 0 Then

    strwhere = strwhere & ” and ” & strParcel & ” = ” & txtParcelSearch.Text

    End If

    If Len(txtHouseNumSearch.Text) > 0 Then

    strwhere = strwhere & ” and ” & strHouseNum & ” = ” & txtHouseNumSearch.Text

    End If

    If Len(txtStreetNameSearch.Text) > 0 Then

    strwhere = strwhere & ” and ” & strStreetName & ” like ‘” & txtStreetNameSearch.Text & “%’”
    End If

    End With

    strwhere = UCase$(Mid$(strwhere, 6))

    txtSearch.Text = strwhere
    Call sSearch(strwhere)
    Me.ParcelsTableAdapter.Fill(Me.CurvDBDataSet.parcels)
    End Sub

    Explanation:
    I will attempt to explain the code.  It we look at:

    If Len(txtOwnerSearch.Text) > 0 Then
    strwhere = ” and ” & strOwner & ” like ‘” & txtOwnerSearch.Text & “%’”
    End If

    In the If…Then statement we test to see if txtOwnerSearch.text has a value.  I have been told that checking to see if the length of txtOwnerSearch.text is greater than 0 is slightly quicker than checking to see if txtOwnerSearch.text has a value.  If the length of txtOwnerSearch.text is greater than 0 and therefore has a value we begin to build the query statement.

    You will notice that all query statements begin with an AND regardless if it is the first statement.  This is by design and will be handled.

    strwhere = ” and ” & strOwner & ” like ‘” & txtOwnerSearch.Text & “%’”

    strwhere= Combined Query String
    strOwner=Column to be queried
    ” like ‘” & txtOwnerSearch.Text & “%’”=Search operator(Like), value(txtOwnerSearch.Text) and wildcard(%)

    Results:
    “and NAME_1=’Wilson M’”

    Handling the initial AND:

    strwhere = UCase$(Mid$(strwhere, 6))

    UCase$ = Capitalizes the entire string
    Mid$= Cuts off the first 6 characters, ” and “

    Results:
    strWhere=”NAME_1=’Wilson M’”

    Call sSearch(strwhere) – Call the search Subroutine

  3. Building the Search Sub:

    Sub sSearch(ByVal pstrWhere As String)
    ‘Set mouse cursor as this can take some time with large datasets
    On Error GoTo ErrorHandler
    ‘app.MousePointer = vbHourglass
    Dim intCount As Integer
    ‘Get layer to query
    Dim pARMap As ARMap
    pARMap = frmMain.conARMap.ARPageLayout.FocusARMap

    Call sSetIndexes(strParcelFile)
    pARLayer = pARMap.ARLayer(gintGroupIndex).ChildARLayer(gintLayerIndex)  ‘m_LayersIndex(cboLayers.ListIndex)

    ‘Build the ARSearchDef
    Dim pARSearchDef As ArcReaderSearchDef
    pARSearchDef = New ArcReaderSearchDef

    ‘Build WhereClause that meets search criteria
    Dim sWhereClause As String
    ‘Remove quotes from WhereClause if search is numeric
    sWhereClause = pstrWhere
    pARSearchDef.WhereClause = sWhereClause
    m_pFeatureCursor = pARLayer.SearchARFeatures(pARSearchDef)
    intCount = fCreateParcelDS(m_pFeatureCursor)

    If intCount > 0 Then
    lblMeets.Text = “Features MEETING the search criteria: ” & intCount
    Else
    lblMeets.Text = “Features MEETING the search criteria: 0″
    End If
    Exit Sub

    ErrorHandler:
    Debug.Print(Err.Number)
    Select Case Err.Number
    Case -2147219885
    MsgBox(“Check Field Names under Program Options”)
    ‘Me.MousePointer = vbDefault
    Exit Sub
    End Select

    End Sub

    Explanation:
    This subroutine is used to process the query string.  The code here is slanted to work with ArcReader Datasets and calls several custom functions which can be ignored.  The relevant section where the query is applied is:

    Call sSetIndexes(strParcelFile)Subroutine to set variables for search layer indexes
    pARLayer = pARMap.ARLayer(gintGroupIndex).ChildARLayer(gintLayerIndex)  ‘m_LayersIndex(cboLayers.ListIndex) – Set the layer to be searched

    ‘Build the ARSearchDef
    Dim pARSearchDef As ArcReaderSearchDef
    pARSearchDef = New ArcReaderSearchDef

    ‘Build WhereClause that meets search criteria
    Dim sWhereClause As String
    ‘Remove quotes from WhereClause if search is numeric
    sWhereClause = pstrWhere
    pARSearchDef.WhereClause = sWhereClause
    - Apply the search string
    m_pFeatureCursor = pARLayer.SearchARFeatures(pARSearchDef)
    - Build the Querried features
    intCount = fCreateParcelDS(m_pFeatureCursor)
    - Custom function to process the records and provide record count

Summary:

The methodology described above is extremely flexable.  I have used it in VB, VBA, .NET, PHP, and ASP projects.  I hope it helps you.


ArcReaderControl

February 15, 2009

Recently i have begun program a GIS viewer utilizing the arcreadercontrol. i wanted the viewer to have as much flexibility as possible. Some of the feature i am attempting to to include are:

  • Saved program settings
  • Saved map settings
    • zoom level
    • layer status(on/off)
  • Custom searches
  • Geocoding

currently, i have all functionality i desired working with the exception of text mark up. in the following posts i will attempt to detail how each item of functionality was accomplished.

Saved Program Settings

There are several reasons to save program settings.  In the case of the people I work with I find it easier to store the path to GIS data and projects within my programs.  I also, as stated earlier, want to be able to to build some custom searches.  The layer I want to be able to search is a parcel layer.  The parcel data I plan to search is from several different counties.  Unfortunately, due to naming conventions used in each county the field names are not always the same.  Therefore i need to save the field names as settings.

Saved Map Settings

On of the biggest frustrations, I have with ArcReader is the inability to save map settings.  For example, if I am working with a PMF files with many different layers, I would like the ability to save layers visibility based on the last setup I used.  With the standalone ArcReader application this is impossible.  I will work around this limitation in the custom viewer.

Custom Searches

The GIS users I work with tend to heavily utilize the available parcel layers.  To help facilitate their work I plan to create some customized searches to supplement the built in “Find” commands.  Alt hough the “Find” command is suitable for searching a single field, it does not handle searching multiple criteria.

Geocoding

The Geocoding functionailty availbe in ArcReader is limited to the ArcWeb Services Locators.  ArcReader and ArcReadercontrol can not utilize file based address locators.  I will attempt to see if there is a work around to allow file based geocoding.


Creating a Headline section on a Web Page

December 10, 2008

When I design a website, I typically like to add a headlines section to the index page.  I do not always have time to either edit the index or create new stories.  For this reason, I started using a blog with RSS feeds.  I can then use PHP to parse the feed and display the headlines.  To keep things neat I limit the number of headline and once I reach my limit, I put a link to additional posts.

The PHP RSS Parser I use is MagpieRSS, as per the MagpieRSS website:

MagpieRSS: RSS for PHP

MagpieRSS provides an XML-based expat RSS parser in PHP.

MagpieRSS is compatible with RSS 0.9 through RSS 1.0. Also parses RSS 1.0’s modules, RSS 2.0, and Atom.

via Magpie RSS – PHP RSS Parser.

In order to diplay just the headlines, I insert the following PHP block into my index page:

<?
require_once ‘rss_fetch.inc’;

// ADD YOUR FEED LINK
$url = ‘http://www.example.com/articles/Articles.rss’;
$rss = fetch_rss($url);
$counter=1;
$i=1;
foreach ($rss->items as $item ) {
$title = $item['title'];
$url   = $item['link'];
if ($i<9) {

//OPEN HEADLINE IN NEW WINDOW
echo “<p>&bull;&nbsp;<a href=’$url’ target=’_Blank’>$title</a><br /></p>\n”;
}
$i++;
$counter++;
}
if ($i>8) {

//ADD YOUR FEED LOCATION
echo “<a href=’http://www.example.com/Articles’  target=’_Blank’>More…</a><br>\n”;
}
?>


Code Gallery : ArcGIS Server Web ADF for the Microsoft .NET Framework

November 20, 2008

I seem to be having better luck with the manual install.  I can get it running on the SOM with some tweaks to the instructions.  So far I am having no luck with the webserver.  Once I get everything running I will post the edits.

Print Layout Task / Server Object Extension for .NET Web ADF (Windows installer)

This demo adds printing capabilities to the .Net WebADF framework. The custom task allows you to generate high-quality maps from a web browser and export maps to pdf, jpeg, ai, and other formats. Using the task, you can select the configuration of your final map product by selecting from several ArcMap preconfigured layouts; you can also change the content of… Download

Print Layout Task / Server Object Extension for .NET Web ADF (manual install)

This demo adds printing capabilities to the .Net WebADF framework. The custom task allows you to generate high-quality maps from a web browser and export maps to pdf, jpeg, ai, and other formats. Using the task, you can select the configuration of your final map product by selecting from several ArcMap preconfigured layouts; you can also change the content of… Download

via Code Gallery : ArcGIS Server Web ADF for the Microsoft .NET Framework