source: trunk/mapserver/mapscript/csharp/examples/drawquery.cs

Last change on this file was 7418, checked in by nsavard, 16 years ago

Standardized header.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/******************************************************************************
2 * $Id: drawquery.cs 7418 2008-02-29 00:02:49Z nsavard $
3 *
4 * Project: MapServer
5 * Purpose: A C# based mapscript example to use the atribute query and
6 * highlight the results.
7 * Author: Tamas Szekeres, szekerest@gmail.com
8 *
9 ******************************************************************************
10 * Copyright (c) 1996-2008 Regents of the University of Minnesota.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies of this Software or works derived from this Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 *****************************************************************************/
30
31using System;
32using OSGeo.MapServer;
33
34/// <summary>
35/// A C# based mapscript example to use the arribute query an highlight the results.
36/// </summary>
37class DrawQuery
38{
39 public static void usage()
40 {
41 Console.WriteLine("usage: QueryMap {mapfile} {query string} {outfile} {-zoom}");
42 System.Environment.Exit(-1);
43 }
44
45 public static void Main(string[] args)
46 {
47 Console.WriteLine("");
48 if (args.Length < 3 || args.Length > 4) usage();
49
50 bool ZoomToResults = (args.Length == 4 && args[3] == "-zoom");
51
52 mapObj map = new mapObj(args[0]);
53 Console.WriteLine ("# Map layers " + map.numlayers + "; Map name = " + map.name);
54
55 QueryByAttribute(args[1], map, ZoomToResults);
56
57 map.querymap.status = mapscript.MS_ON;
58 map.querymap.color.setRGB(0,0,255);
59 map.querymap.style = (int)MS_QUERYMAP_STYLES.MS_HILITE;
60
61 try
62 {
63 imageObj image = map.drawQuery();
64 image.save(args[2],map);
65 }
66 catch (Exception ex)
67 {
68 Console.WriteLine( "QueryMap: ", ex.Message );
69 }
70 }
71
72 private static bool IsLayerQueryable(layerObj layer)
73 {
74 if ( layer.type == MS_LAYER_TYPE.MS_LAYER_TILEINDEX )
75 return false;
76
77 if(layer.template != null && layer.template.Length > 0) return true;
78
79 for(int i=0; i<layer.numclasses; i++)
80 {
81 if(layer.getClass(i).template != null && layer.getClass(i).template.Length > 0)
82 return true;
83 }
84 return false;
85 }
86
87 public static void QueryByAttribute(string qstring, mapObj map, bool zoomToResults)
88 {
89 Console.WriteLine("\nPerforming QueryByAttribute:");
90 try
91 {
92 layerObj layer;
93 rectObj query_bounds = null;
94 for (int i = 0; i < map.numlayers; i++)
95 {
96 layer = map.getLayer(i);
97 if (layer.connection != null && IsLayerQueryable(layer))
98 {
99 Console.WriteLine("Layer [" + i + "] name: " + layer.name);
100 BuildQuery(layer, qstring);
101 // zoom to the query results
102 using (resultCacheObj results = layer.getResults())
103 {
104 if (results != null && results.numresults > 0)
105 {
106 // calculating the extent of the results
107 if (query_bounds == null)
108 query_bounds = new rectObj(results.bounds.minx, results.bounds.miny,
109 results.bounds.maxx, results.bounds.maxy,0);
110 else
111 {
112 if (results.bounds.minx < query_bounds.minx) query_bounds.minx = results.bounds.minx;
113 if (results.bounds.miny < query_bounds.miny) query_bounds.miny = results.bounds.miny;
114 if (results.bounds.maxx > query_bounds.maxx) query_bounds.maxx = results.bounds.maxx;
115 if (results.bounds.maxy > query_bounds.maxy) query_bounds.maxy = results.bounds.maxy;
116 }
117 }
118 }
119 }
120 }
121 // setting the map extent to the result bounds
122 if (query_bounds != null)
123 {
124 if (zoomToResults)
125 {
126 map.setExtent(query_bounds.minx, query_bounds.miny, query_bounds.maxx, query_bounds.maxy);
127 map.scaleExtent(1.2, 0, 0); // increasing the visible area
128 Console.WriteLine("Current map scale: 1:" + (int)map.scaledenom);
129 }
130 }
131 else
132 Console.WriteLine("The query returned 0 results ...");
133 }
134 catch (Exception e)
135 {
136 Console.WriteLine("QueryByAttribute: " + e.Message);
137 }
138 }
139
140 private static void BuildQuery(layerObj layer, string qstring)
141 {
142 if (layer != null && layer.map != null)
143 {
144 /*layer.open();
145 string qs = "";
146 string att = "";
147 for (int i=0; i < layer.numitems; i++)
148 {
149 if (qs == "")
150 {
151 qs = "(";
152 att = layer.getItem(i);
153 }
154 else
155 {
156 qs += " OR ";
157 }
158 qs += "'[" + layer.getItem(i) + "]'='" + qstring + "'";
159 }
160 qs += ")";
161 layer.close();*/
162 string qs = qstring;
163 string att = null;
164
165 Console.WriteLine("Query string: " + qs);
166
167 try
168 {
169 layer.queryByAttributes(layer.map, att, qs, 1);
170 }
171 catch (Exception e)
172 {
173 Console.WriteLine("BuildQuery: " + e.Message);
174 }
175 }
176 }
177}
178
Note: See TracBrowser for help on using the repository browser.