Profile | Register | Active Topics | Search | FAQ
You are welcome to post questions, comments and tips in all forums and topics.
And if you think you have relevant information, do not hesitate to make a contribution!

Advanced Search
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Franson GpsTools
 GpsTools
 Guide #1 - How to draw a shapefile with GpsTools
 New Topic  Reply to Topic
 Printer Friendly
Digg this
Author Previous Topic Topic Next Topic  

Jonas
Administrator

Sweden
2349 Posts

Posted - 07/19/2006 :  10:49:13  Show Profile  Reply with Quote
GpsTools 2.30 or later

GpsTools Guide #1 - How to draw a shapefile with GpsTools

1. Introduction

This guide will attempt to show how easy it is to set up an application which displays ESRI shapefiles.

The source code is available at the bottom of this page.

2. Getting started

We will start by familiarizing ourself with the necessary GpsTools objects, required to draw a shapefile.
We will make use of two major parts of the GpsTools SDK; the ShapeFile and the MapShapeFile objects.
The ShapeFile is the object which can parse shapefile data and MapShapeFile is the object which can draw a ShapeFile on the screen.

If you run the sample and try to load a ShapeFile it should almost immediately show your shapefile in the map control.

3. The data in the ShapeFile

The ShapeFile object is very simple to use, we just create it...

GpsShapeNET.ShapeFile sf = new GpsShapeNET.ShapeFile();

...and then we open it:


sf.Open(openFileDialog.FileName, GpsShapeNET.FileMode.FILE_READ);

Now we have a ShapeFile that we can either read shapes from or just pass to the MapShapeFile object.

4. The projection

Before we can draw the ShapeFile we just need one more thing; the DatumGridTemplate of the ShapeFile. This property is required to determine what the coordinates in the shapefile mean in the real world.
If you have a proper shapefile with an accompanying prj-file (which contains the information on how the projection) you are already done. The ShapeFile will have its DatumGridTemplate set.
If the prj-file is missing, or is incomplete (which is sometimes the case) we have to either know the necessary information or we can make a guess. We can guess by examining the coordinates within the shapefile.

Note: Unfortunately we can only guess if the shapefile contains datum or grid coordinates so even though the shapefile will be projected, the coordinates you'll receive isn't necessarily correct.

This is how to guess if the DatumGridTemplate is a grid or a datum:

// if the ShapeFile wasn't able to determine the projection this property will not be set

if (sf.DatumGridTemplate == null)
{
	// Since we need a projection we have to guess what it is...we get the bounding box
double Xmin, Xmax, Ymin, Ymax, Zmin, Zmax, Mmin, Mmax;
sf.GetBoundingBox(out Xmin, out Xmax, out Ymin, out Ymax, out Zmin, out Zmax, out Mmin, out Mmax); string guessStr = ""; // we create a Position object which is used as a DatumGridTemplate GpsToolsNET.Position nodePosition = new GpsToolsNET.Position(); // Take a guess if we should use a datum or grid. // A datum shapefile will have its bounds in degrees... if (Ymax <= 90 && Ymin >= -90 && Xmax <= 180 && Xmin >= -180) { nodePosition.Datum = GpsToolsNET.Datum.WGS_84; guessStr = nodePosition.Datum.ToString(); } else { // ...whereas a grid shapefile will have its bounds in meters.. nodePosition.Grid = GpsToolsNET.Grid.UTM_NORTH; nodePosition.Zone = "15"; guessStr = nodePosition.Grid.ToString(); } // set the guessed DatumGridTemplate.. sf.DatumGridTemplate = nodePosition; // display to user that we guessed! MessageBox.Show("No projection data was found. Guessing the projection of this file as a " + guessStr); }

5. The MapShapeFile and Resolution

Now that we have a ShapeFile capable of giving us shapes to draw we want to draw them. This is very easy if we use the MapShapeFile object.


GpsViewNET.MapShapeFile mf = map1.NewMapShapeFile(sf);
mf.Resolution = 2.0;

The Resolution property conserves memory and generally speed up the drawing performance. The algorithm works by filtering the shapes to draw.

It first looks at the shape's pixel size. If the drawn shape would be smaller than the resolution it will be skipped. The algorithm will then look at individual parts within the shape. If a part would be smaller than the resolution property, the part will be skipped as well. Finally it will look at the distance to the next node in the shape. If the next node to be drawn is closer to the last than the resolution it too will be skipped!

By doing this, there will be less nodes stored in memory in memory and less nodes to be drawn, which will be faster!

If you run this on Pocket PC this is almost a must for all but the very smallest shapefiles since the memory capacity is so limited on that platform.

7. Update the map control

Before we are finished we will look at two things that can affect the drawing of the map control.

7.1 CancelDraw

The CancelDraw methods determines if the current call to Update() will be canceled.


map1.CancelDraw();

7.2 UpdateInterval

The UpdateInterval property is used if you have a large shapefile and you don't want the user to get the impression that your application has hung. By setting this property you'll get periodic updates (for example setting this property to 1000 (milliseconds) you ensure that you'll get an update on the screen each second with as much as it has been able to draw.


map1.UpdateInterval = 1000;

Draw on screen every second, even if we haven't finished the entire update operation.

7.3 Update

The very last thing we do is calling Update():


map1.Update();

This will commit the changes to the map control and draw the shapefile.

8. Resources and source code

1. GpsTools SDK (at least version 2.30)
2. ShapeFileViewer
3. A ESRI compatible shapefile.

zeeshanprojects
Average Member

Pakistan
37 Posts

Posted - 03/30/2008 :  01:40:56  Show Profile  Click to see zeeshanprojects's MSN Messenger address  Send zeeshanprojects a Yahoo! Message  Reply with Quote
hi there!
i use this source code to load shap file. it works successfully but i have a question that i purchase middle east map shape files. so i have 3 folders AREA,POINT,LINES each folder contains several shape files and one apr file. using this source code i have to upload shape file one by one to complete the mape. is there no way to upload all shap file at one time or can u suggest to upload APR file. coz if APR file u open in ARC software it opens all the shape files. i need ur help plz.
Go to Top of Page

Mehend
New Member

United Kingdom
3 Posts

Posted - 07/20/2009 :  05:36:42  Show Profile  Reply with Quote
I would be most grateful if you could update the example code distributed with GPS Tools to include a VB.NetCF version for all of the examples. The "Getting started with VB.NET and GpsTools .NET Compact Framework" lists samples that appear to be missing in the release.
Many thanks
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
GpsGate Forum © 2010 Franson Technology AB Go To Top Of Page
Powered By: Snitz Forums 2000 Version 3.4.04