| 1 | /******************************************************************************
|
|---|
| 2 | * $Id: shapeinfo.cs 7418 2008-02-29 00:02:49Z nsavard $
|
|---|
| 3 | *
|
|---|
| 4 | * Project: MapServer
|
|---|
| 5 | * Purpose: A C# based based mapscript example to dump information from
|
|---|
| 6 | * a shapefile.
|
|---|
| 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 |
|
|---|
| 31 | using System;
|
|---|
| 32 | using System.Collections;
|
|---|
| 33 | using OSGeo.MapServer;
|
|---|
| 34 |
|
|---|
| 35 | /// <summary>
|
|---|
| 36 | /// A MapScript application for creating inline layers with annotations.
|
|---|
| 37 | /// </summary>
|
|---|
| 38 | class Inline {
|
|---|
| 39 |
|
|---|
| 40 | public static void usage()
|
|---|
| 41 | {
|
|---|
| 42 | Console.WriteLine("usage: inline [outformat] [outfile]");
|
|---|
| 43 | System.Environment.Exit(-1);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | public static void Main(string[] args)
|
|---|
| 47 | {
|
|---|
| 48 |
|
|---|
| 49 | if (args.Length < 2) usage();
|
|---|
| 50 |
|
|---|
| 51 | // creating a new map from scratch
|
|---|
| 52 | mapObj map = new mapObj(null);
|
|---|
| 53 | // adding a layer
|
|---|
| 54 | layerObj layer = new layerObj(map);
|
|---|
| 55 | layer.type = MS_LAYER_TYPE.MS_LAYER_POINT;
|
|---|
| 56 | layer.status = mapscript.MS_ON;
|
|---|
| 57 | layer.connectiontype = MS_CONNECTION_TYPE.MS_INLINE;
|
|---|
| 58 | // define the attribute names from the inline layer
|
|---|
| 59 | layer.addProcessing("ITEMS=attribute1,attribute2,attribute3");
|
|---|
| 60 | // define the class
|
|---|
| 61 | classObj classobj = new classObj(layer);
|
|---|
| 62 | classobj.template = "query"; // making the layer queryable
|
|---|
| 63 | // setting up the text based on multiple attributes
|
|---|
| 64 | classobj.setText("('Shape:' + '[attribute1]' + ' Color:' + '[attribute2]' + ' Size:' + '[attribute3]')");
|
|---|
| 65 | // define the label
|
|---|
| 66 | classobj.label.outlinecolor = new colorObj(255, 255, 255, 0);
|
|---|
| 67 | classobj.label.force = mapscript.MS_TRUE;
|
|---|
| 68 | classobj.label.size = (double)MS_BITMAP_FONT_SIZES.MS_MEDIUM;
|
|---|
| 69 | classobj.label.position = (int)MS_POSITIONS_ENUM.MS_LC;
|
|---|
| 70 | classobj.label.wrap = ' ';
|
|---|
| 71 | // set up attribute binding
|
|---|
| 72 | classobj.label.setBinding((int)MS_LABEL_BINDING_ENUM.MS_LABEL_BINDING_COLOR, "attribute2");
|
|---|
| 73 | // define the style
|
|---|
| 74 | styleObj style = new styleObj(classobj);
|
|---|
| 75 | style.color = new colorObj(0, 255, 255, 0);
|
|---|
| 76 | style.setBinding((int)MS_STYLE_BINDING_ENUM.MS_STYLE_BINDING_COLOR, "attribute2");
|
|---|
| 77 | style.setBinding((int)MS_STYLE_BINDING_ENUM.MS_STYLE_BINDING_SIZE, "attribute3");
|
|---|
| 78 |
|
|---|
| 79 | Random rand = new Random((int)DateTime.Now.ToFileTime()); ;
|
|---|
| 80 |
|
|---|
| 81 | // creating the shapes
|
|---|
| 82 | for (int i = 0; i < 10; i++)
|
|---|
| 83 | {
|
|---|
| 84 | shapeObj shape = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_POINT);
|
|---|
| 85 |
|
|---|
| 86 | // setting the shape attributes
|
|---|
| 87 | shape.initValues(4);
|
|---|
| 88 | shape.setValue(0, Convert.ToString(i));
|
|---|
| 89 | shape.setValue(1, new colorObj(rand.Next(255), rand.Next(255), rand.Next(255), 0).toHex());
|
|---|
| 90 | shape.setValue(2, Convert.ToString(rand.Next(25) + 5));
|
|---|
| 91 |
|
|---|
| 92 | lineObj line = new lineObj();
|
|---|
| 93 | line.add(new pointObj(rand.Next(400) + 25, rand.Next(400) + 25, 0, 0));
|
|---|
| 94 | shape.add(line);
|
|---|
| 95 | layer.addFeature(shape);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | map.width = 500;
|
|---|
| 99 | map.height = 500;
|
|---|
| 100 | map.setExtent(0,0,450,450);
|
|---|
| 101 | map.selectOutputFormat(args[0]);
|
|---|
| 102 | imageObj image = map.draw();
|
|---|
| 103 | image.save(args[1], map);
|
|---|
| 104 |
|
|---|
| 105 | //perform a query
|
|---|
| 106 | layer.queryByRect(map, new rectObj(0, 0, 450, 450, 0));
|
|---|
| 107 |
|
|---|
| 108 | resultObj res;
|
|---|
| 109 | shapeObj feature;
|
|---|
| 110 | using (resultCacheObj results = layer.getResults())
|
|---|
| 111 | {
|
|---|
| 112 | if (results != null && results.numresults > 0)
|
|---|
| 113 | {
|
|---|
| 114 | // extracting the features found
|
|---|
| 115 | layer.open();
|
|---|
| 116 | for (int j = 0; j < results.numresults; j++)
|
|---|
| 117 | {
|
|---|
| 118 | res = results.getResult(j);
|
|---|
| 119 | feature = layer.getShape(res);
|
|---|
| 120 | if (feature != null)
|
|---|
| 121 | {
|
|---|
| 122 | Console.WriteLine(" Feature: shapeindex=" + res.shapeindex + " tileindex=" + res.tileindex);
|
|---|
| 123 | for (int k = 0; k < layer.numitems; k++)
|
|---|
| 124 | {
|
|---|
| 125 | Console.Write(" " + layer.getItem(k));
|
|---|
| 126 | Console.Write(" = ");
|
|---|
| 127 | Console.Write(feature.getValue(k));
|
|---|
| 128 | Console.WriteLine();
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | layer.close();
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | } |
|---|