PointIcons.cs

This example demonstrates how to generate unique value classification for point shapefile and to assign an icon for each category. The icons will be chosen from the folder on disk automatically provided that the name of the icon matches the value of the chosen field. Here is a screenshot with the results of the code execution.

using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Assigns markers for points
// </summary>
public void PointIcons(AxMap axMap1, string dataPath, string iconPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
if (!Directory.Exists(iconPath))
{
MessageBox.Show("Icons folder wasn't found: " + iconPath);
return;
}
string roads = dataPath + "roads.shp";
string buildings = dataPath + "buildings.shp";
if (File.Exists(roads))
{
Shapefile sfRoads = new Shapefile();
sfRoads.Open(roads, null);
//axMap1.AddLayer(sfRoads, true);
Shapefile sfBuildings = new Shapefile();
sfBuildings.Open(buildings, null);
axMap1.AddLayer(sfBuildings, true);
}
Shapefile sf = new Shapefile();
string filename = dataPath + "points.shp";
if (!sf.Open(filename, null))
{
MessageBox.Show("Failed to open shapefile: " + sf.ErrorMsg[sf.LastErrorCode]);
}
else
{
int layerHandle = axMap1.AddLayer(sf, true);
sf = axMap1.get_Shapefile(layerHandle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding
int index = sf.Table.FieldIndexByName["Type"];
var names = new HashSet<string>();
for (int i = 0; i < sf.Table.NumRows; i++)
{
names.Add((string)sf.Table.CellValue[index, i]);
}
string[] files = Directory.GetFiles(iconPath);
foreach (string file in files)
{
string name = Path.GetFileNameWithoutExtension(file);
if (Path.GetExtension(file).ToLower() == ".png" && names.Contains(name))
{
Image img = new Image();
if (img.Open(file, ImageType.USE_FILE_EXTENSION, true, null))
{
ct.Expression = "[Type] = \"" + name + "\"";
}
}
}
sf.DefaultDrawingOptions.Visible = false; // hide all the unclassified points
axMap1.Redraw();
}
}
}
}
ImageType
The type of images supported by MapWinGIS.
Definition: Enumerations.cs:92
tkPointSymbolType
The available types of point symbols.
Definition: Enumerations.cs:1143
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
Map component for visualization of vector, raster or grid data.
Definition: AxMap.cs:56
void Redraw()
Redraws all layers in the map if the map is not locked.
Definition: AxMap.cs:183
Represents an raster image of particular format which may be added to the map.
Definition: Image.cs:66
float LineWidth
Gets or sets the width of the lines to draw shapes.
Definition: ShapeDrawingOptions.cs:624
bool Visible
Gets or sets the values which indicates whether shapes will be visible.
Definition: ShapeDrawingOptions.cs:847
Image Picture
Gets or sets the picture which will be used as texture brush (ShapeDrawingOptions....
Definition: ShapeDrawingOptions.cs:635
tkPointSymbolType PointType
Gets or sets the type of the point symbols.
Definition: ShapeDrawingOptions.cs:741
ShapefileCategory Add(string Name)
Creates a new visualization category, adds it to the list and returns its reference to the caller.
Definition: ShapefileCategories.cs:66
void ApplyExpressions()
Maps shapes to the visualization categories based in ShapefileCategory.Expression.
Definition: ShapefileCategories.cs:143
Represents a set of visualization options for shapefile layer.
Definition: ShapefileCategory.cs:47
string Expression
Gets or sets expression which defines shapes which belong to the category.
Definition: ShapefileCategory.cs:61
ShapeDrawingOptions DrawingOptions
Gets or sets visualization options associated with the category.
Definition: ShapefileCategory.cs:52
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
ShapeDrawingOptions DefaultDrawingOptions
Gets or sets an instance of ShapeDrawingOptions class which holds default drawing options.
Definition: Shapefile.cs:111
int LastErrorCode
Gets the code of last error which took place inside this object.
Definition: Shapefile.cs:249
ShapefileCategories Categories
Gets or sets an instance of ShapefileCategories class associated with the shapefile.
Definition: Shapefile.cs:81
int NumRows
Gets the number of rows in the table.
Definition: TableClass.cs:312
bool Open(string ImageFileName, ImageType fileType, bool InRam, ICallback cBack)
Opens an image from file.
Definition: Image.cs:357
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
int AddLayer(object Object, bool visible)
Adds a layer to the map.
Definition: AxMap.cs:1342
Shapefile get_Shapefile(int layerHandle)
Gets shapefile object associated with the layer.
Definition: AxMap.cs:1546
bool Open(string shapefileName, ICallback cBack)
Opens shapefile from the disk.
Definition: Shapefile.cs:1430
Table Table
Gets the reference to the attribute table associated with the shapefile.
Definition: Shapefile.cs:677