ShapefileToDrawingLayer.cs

This example demonstrates how to create a drawing layer from the specified shapefile. Here is a screenshot with the results of the code execution.

using AxMapWinGIS;
using MapWinGIS;
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Examples
{
public partial class MapExamples
{
// <summary>
// Creates a drawing layer from the specified shapefile.
// </summary>
public void ShapefileToDrawingLayer(AxMap axMap1, string dataPath)
{
axMap1.Projection = tkMapProjection.PROJECTION_NONE;
axMap1.GrabProjectionFromData = true;
string[] filenames = new []{"buildings.shp", "roads.shp", "points.shp"};
for (int i = 0; i < filenames.Length; i++)
{
filenames[i] = dataPath + filenames[i];
}
Extents extents = null;
var center = (axMap1.Extents as Extents).Center;
try
{
for (int n = 0; n < filenames.Length; n++)
{
Shapefile sf = new Shapefile();
if (sf.Open(filenames[n], null))
{
if (axMap1.Projection == tkMapProjection.PROJECTION_NONE)
if (extents == null)
extents = sf.Extents; // the extents of the fist shapefile will be used to setup display
int drawHandle = axMap1.NewDrawing(tkDrawReferenceList.dlSpatiallyReferencedList);
for (int i = 0; i < sf.NumShapes; i++)
{
Shape shp = sf.Shape[i];
if (shp.ShapeType == ShpfileType.SHP_POINT)
{
double x = 0.0;
double y = 0.0;
shp.get_XY(0, ref x, ref y);
axMap1.DrawPointEx(drawHandle, x, y, 5, 0);
}
else
{
for (int p = 0; p < shp.NumParts; p++)
{
int initIndex = shp.Part[p];
int numPoints = shp.EndOfPart[p] - shp.Part[p] + 1;
if (numPoints > 0)
{
double[] x = new double[numPoints];
double[] y = new double[numPoints];
for (int j = 0; j < numPoints; j++)
{
shp.get_XY(j + initIndex, ref x[j], ref y[j]);
}
object xObj = x;
object yObj = y;
bool drawFill = shp.ShapeType == ShpfileType.SHP_POLYGON;
uint color = sf.ShapefileType == ShpfileType.SHP_POLYGON ? sf.DefaultDrawingOptions.FillColor :
axMap1.DrawPolygonEx(drawHandle, ref xObj, ref yObj, numPoints, color, drawFill);
}
}
}
}
}
}
}
finally
{
if (extents != null)
axMap1.Extents = extents;
axMap1.LockWindow(tkLockMode.lmUnlock);
axMap1.Redraw();
}
}
}
}
tkLockMode
The lock mode of the map, either locked or not.
Definition: Enumerations.cs:940
ShpfileType
The type of the shapefile.
Definition: Enumerations.cs:169
tkMapProjection
Commonly used map projections to be set in Form Designer (see AxMap.Projection property).
Definition: Enumerations.cs:1741
tkDrawReferenceList
The type of spatial reference for the drawing layer.
Definition: Enumerations.cs:484
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 a rectangle on the map.
Definition: Extents.cs:49
GeoProjection Clone()
Creates a copy GeoProjection object
Definition: GeoProjection.cs:498
uint LineColor
Gets or sets the line color of the shapes.
Definition: ShapeDrawingOptions.cs:577
uint FillColor
Gets or sets the fill color of the shape.
Definition: ShapeDrawingOptions.cs:471
A shape object represents a geometric shape which can be added to a shapefile which is displayed in t...
Definition: Shape.cs:41
ShpfileType ShapeType
Gets or sets the type of the shape.
Definition: Shape.cs:538
bool get_XY(int pointIndex, ref double x, ref double y)
Gets the coordinates of the specified point.
Definition: Shape.cs:639
int NumParts
Gets the number of parts contained in the shape.
Definition: Shape.cs:459
Provides a functionality for accessing and editing ESRI shapefiles.
Definition: Shapefile.cs:72
Extents Extents
Gets bounding box which encompass all the shapes in the shapefile.
Definition: Shapefile.cs:146
GeoProjection GeoProjection
Get or sets an instance of GeoProjection class associated with the shapefile.
Definition: Shapefile.cs:206
int NumShapes
Gets the number of shapes in the shapefile.
Definition: Shapefile.cs:254
ShapeDrawingOptions DefaultDrawingOptions
Gets or sets an instance of ShapeDrawingOptions class which holds default drawing options.
Definition: Shapefile.cs:111
ShpfileType ShapefileType
Gets the type of the shapefile.
Definition: Shapefile.cs:270
tkMapProjection Projection
Sets projection of the map. It providers 2 most commonly used coordinate system/projections to be eas...
Definition: AxMap.cs:2709
GeoProjection GeoProjection
Gets or sets projection for map.
Definition: AxMap.cs:2719
bool GrabProjectionFromData
Gets or sets a value indicating whether projection for will be taken from the first datasource added ...
Definition: AxMap.cs:2701
void DrawPointEx(int layerHandle, double x, double y, int pixelSize, uint color, byte alpha=255)
Draws a point on the specified drawing layer.
Definition: AxMap.cs:1766
int NewDrawing(tkDrawReferenceList projection)
Creates a new drawing layer on the map returning its handle.
Definition: AxMap.cs:1868
void DrawPolygonEx(int layerHandle, ref object xPoints, ref object yPoints, int numPoints, uint color, bool fill, byte alpha=255)
Draws a polygon on the specified drawing layer.
Definition: AxMap.cs:1795
Extents Extents
Gets or sets the extents of the map using an Extents object.
Definition: AxMap.cs:2426
void LockWindow(tkLockMode lockMode)
Locks the window so that any changes will not be displayed until it is unlocked.
Definition: AxMap.cs:524
bool Open(string shapefileName, ICallback cBack)
Opens shapefile from the disk.
Definition: Shapefile.cs:1430