gsuite wpf edition quick start guide - mapgenix gsuite · gsuite wpf edition quick start guide ......

25
GSuite WPF Edition Quick Start Guide Welcome to GSuite WPF Edition from Mapgenix, a powerful .Net control for custom development of WPF (Windows Presentation Foundation) Desktop applications. The aim of this guide is to help the developer get started with building a simple WPF mapping app. It comes with code in C#. Along the way, many concepts of GIS and mapping are explained with links to specific guides to learn more. This guide is divided into two main parts: I) Installing trial version of GSuite WPF edition It shows how to download the sample apps and install the Mapgenix dlls from NuGet. There is also a video for it Installing trial version of GSuite WPF Edition. II) My first app using GSuite WPF edition It shows how to create your first WPF app from scratch using GSuite WPF edition. There is also a video for it My First App using GSuite WPF edition. To get the most of this guide, you will find three separate sections with different colors to distinguish from the main text.

Upload: dohanh

Post on 27-Apr-2018

240 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

GSuite WPF Edition Quick Start Guide

Welcome to GSuite WPF Edition from Mapgenix, a powerful .Net control for custom

development of WPF (Windows Presentation Foundation) Desktop applications. The aim of this

guide is to help the developer get started with building a simple WPF mapping app. It comes

with code in C#. Along the way, many concepts of GIS and mapping are explained with links to

specific guides to learn more.

This guide is divided into two main parts:

I) Installing trial version of GSuite WPF edition

It shows how to download the sample apps and install the Mapgenix dlls from NuGet.

There is also a video for it Installing trial version of GSuite WPF Edition.

II) My first app using GSuite WPF edition

It shows how to create your first WPF app from scratch using GSuite WPF edition.

There is also a video for it My First App using GSuite WPF edition.

To get the most of this guide, you will find three separate sections with different colors to

distinguish from the main text.

Code

In a blue frame is code to follow to complete the exercises.

Page 2: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Information

In this section we extend a little bit on a subject and to point to more detailed information.

Warning

You need to pay attention because of a pitfall or there might be something that could

cause headaches.

I) Installing trial version of GSuite WPF edition

First, of course, you need to register and be logged in to Mapgenix website.

https://www.mapgenix.com/en/login/

1) Download trial version https://www.mapgenix.com/en/products/wpf-edition/download-

trial/

What the downloaded zip file containsThe Zip file downloaded is actually a

WPF project containing sample code and data. GSuite Wpf itself is installed thru NuGet

package manager in Visual Studio.

It also contains the license info ‘MapgenixKey’ in app.config in appsettings

The data is under the directory ‘SampleData’

The Zip file is about 36.7 MB

Page 3: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

2) Extract ZIP file GSuiteWPF2.0.zip to your development folder

Naming the development folder

The folder name must not contain special characters such as . # $ & * ? !

3) Open the solution GSuiteWPF2.0.sln in Visual Studio 12 Upgrade 4 or higher.

4) Add Mapgenix NuGet packages references

- Go to ‘Manage NuGet Packages’ under References in Solution Explorer

- Create New Package Source:

Name: Mapgenix

Source: https://www.myget.org/F/mapgenix-nuget/api/v2

- Install the package Mapgenix.GSuite.Wpf

Installation of the NuGet packages

You need to manually install only the package Mapgenix.GSuite.Wpf that

contains the Wpf map control. By installing this package, all the relevant

dependent packages will be automatically installed and added as references to

the solution.

Additionally you need to agree with the license terms of the libraries

Microsoft.AspNet.WebApi.Client, NetTopologySuite and Trove.FLEE pacakges

and click ‘I accept’.

It will add the following dlls as reference to your project:

Page 4: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

How the dlls are named

The dlls starting with Mapgenix are specific to Mapgenix. The convention for the

naming the front ends (editions) dlls is Mapgenix followed by GSuite and ending

with the edition name.

The convention for the naming of the back ends dlls is Mapgenix and ending with

the back end name. The back end dlls are common to all the front end editions.

-Mapgenix.GSuite.Wpf

-Mapgenix.Canvas

-Mapgenix.FeatureSource

-Mapgenix.Gdal.Data

-Mapgenix.Index

-Mapgenix.Layers

-Mapgenix.Projection

-Mapgenix.RasterSource

-Mapgenix.Shapes

-Mapgenix.Styles

-Mapgenix.Symbology

-Mapgenix.Utils

GSuite also uses third party libraries for specific functionalities such as format

support, projections, geometric functions, charts, etc.

-DotSpatial.Data

-DotSpatial.Mono

-DotSpatial.Projections

-DotSpatial.Serialization

-DotSpatial.Topology

-gdal_csharp

-gdalconst_csharp

-ogr_csharp

-osr_csharp

-GeoAPI

-NetTopologySuite

-NetTopologySuite.IO.PostGis

-Ciloci.Flee

-LumenWorks.Framework.IO

-PdfSharp

-PdfSharp.Charting

-Mono.Security

-Npgsql

Page 5: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

-Npgsql.EntityFramework

-Portable.Licensing

-PowerCollections

-Xml.Schema.Linq

-Zedgraph

It will also add dlls from .Net Base Class Library if they are not referenced yet to

your project.

-System

-System.Data.SQLite

-System.Data.SQLite.EF6

-System.SQLite.Linq

-System.Drawing

-System.Net.Http

-System.Net.Http.Formatting

The NuGet packages

The installation will put all the Nuget Packages in the packages folder at the level

of GSuiteWPF2.0.sln solution. It has a size of 227 MB.

5) Run the sample app

About the data used in the sample app

- The sample app uses data in various formats. The data is in the folder

‘yourfolder\SampleData\’. It has a size of 84.1 MB

- For the samples ‘Sql Server Spatial’, ’Post GIS’, ‘SpatiaLite’ under the

category ‘Databases’, you need to follow the instructions in Spatial Database

guide to set the databases.

The sample app has many examples of mapping and GIS functionalities organized

by category. Most examples use shapefiles as sample data. Each example shows a

specific task with the code behind. You can take advantages of those examples by

copy pasting the code and adapting it in your production code.

Page 6: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

The 16 main categories are:

-Getting Started: How to accomplish the most common functionalities in mapping

and GIS such as loading layers, highlighting features etc.

-Navigation: How to navigate the map by panning, zooming in, zooming out, rotating

the map, showing graticule, etc

-Zoom Levels and Scales: How to display layers by zoom levels, zoom to a feature,

get the scale of the map, etc.

-Vector formats: How to load vector based layers such as TAB files, CAD, GPX etc.

-Databases: How to load spatial databases such as SpatiaLite, PostGIS, SQL

Server Spatial, etc.

-Protocols: How to load layers based on protocols such as KML, GeoJSON, etc.

-Raster formats: How to load raster based layers such as ASCII Grid, Arc Grid,

DEM, Geo TIFF, BIL, Erdas Imagine, etc.

-Web Mapping Services: How to consume tiled web maps as background map such

as OpenStreetMap, Google Map, HERE Map, Bing Map, etc.

-OGC Web Services: How to consume OGC Web Services such as WMS, WFS,

etc.

-Styles on Vector: How to display vector layers based on value, class, charts,

regular expression etc.

-Styles on Raster: How to display raster layers based on cell value.

-Markers: How to display and manipulate different types of markers.

-Labeling: How to label features of vector based layers

-Querying and Analysis: How to perform geometric operations and spatial analysis

such as spatial queries, buffer, etc

-Map Projections and Coordinates: How to apply projection conversions and deal

with world coordinates.

-Edit: How to edit shapes and apply topological rules such as snapping.

The sample app has a search tool to find the specific functionality you are looking for:

Page 7: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Consistent functionalities and API across GSuite editions

GSuite offers the same functionalities across the different editions. What you can do in

one desktop edition, you can also do it a web edition.

Also, the API is almost entirely consistent across the different editions of GSuite. This

transversability of functionalities and API means that a developer familiar in one edition

is going to need minimum effort to learn another edition. This also means that porting

an application from desktop to web or vice-versa is a seamless process

II) My First App using GSuite WPF edition

1) Create a new desktop WPF Application project using Visual Studio 12 Upgrade 4 or

higher.

2) Add GSuite references to your project

- Go to ‘Manage NuGet Packages’ under References in Solution Explorer

- Go to the Mapgenix Package Source that you added previously.

Name: Mapgenix

Source: https://www.myget.org/F/mapgenix-nuget/api/v2

- Install the package Mapgenix.GSuite.WPF

Page 8: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Installation of the NuGet packages

You need to click ‘Install’ only the package Mapgenix.GSuite.WPF that contains

the Windows map control. By installing this package, all the relevant dependent

packages will be automatically installed and added as references to the solution.

Additionally, you need to agree with the license terms of the libraries

Microsoft.AspNet.WebApi.Client, NetTopologySuite and Trove.FLEE pacakges

and click ‘I accept’.

Tips on the NuGet packages

If you have the NuGet packages already installed, you can reference the dlls from

the original ‘packages’ folder or you can move the dlls to a central location. That way

you can use that central location for references for all your GSuite projects and not

have to download the Mapgenix NuGet packages each time.

3) Add the license information to App.config

The license info is found in the App.config file of the downloaded trial version in

the solution GSuiteWPF2.0 (See Installing Trial Version of GSuite WPF Edition

above). Look for ‘MapgenixKey’ in app.config in appsettings

<configuration>

<appSettings>

<add key="MapgenixKey" value="YourValue" /> </appSettings> </configuration>

Page 9: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Permanent key

After purchasing the full version of GSuite, you will receive a new key to place in that same App.config file. This will allow GSuite to run permanently and without water marks on the map.

4) Build Solution (F6)

5) Add the Map Control to the ToolBox

(You need to be in Designer View)

- Choose Item…

- Browse…

Browse to the bin directory of your application and select

Mapgenix.GSuite.Wpf.dll

Reminder

Do not forget to build first, otherwise you will not find the dll.

Page 10: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

The map control should appear in the Toolbox:

6) Add Map control to form

Now you are ready to write your first WPF app using GSuite and have a simple

functional map with a shapefile layer on top of OpenStreetMap. In addition, you will do

some simple spatial analysis on the shapefile.

So, let’s get started!

My first GSuite WPF Sample app

First, let’s look at some important concepts of mapping and GIS and how they relate to GSuite.

Important concepts

Page 11: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Data

In GIS, layers are the mechanism used to display geographic datasets in the map. Each

layer references a dataset and styles that determines how the data is going to be displayed and

labeled.

There are two major types of data in GIS, vector and raster.

Vector data type is a coordinate-based data model that represents geographic features

as point, lines and polygons.

-Point features are represented as a single coordinate pair.

-Line features are represented as ordered lists of vertices.

-Polygon features are represented as closed ordered lists of vertices.

Very often, attributes are associated with each vector feature.

Many formats exist for storing data in vector. The most popular are shapefiles, Tab files,

GeoJSON, KML. In this exercise, we will use a shapefile.

In GSuite, the most important class related to vector data type is BaseFeatureSource, the

abstract class from which all vector layers are based on for the data access and source. Also

important is BaseFeatureLayer, the abstract class from which all vector based layers are based

on for the styling and drawing logic. For example, ShapeFileFeatureLayer uses

ShapeFileFeatureSource for the access logic of the actual shapefile files (.shp, .shx, .dbf).

Raster data type is a spatial data model that defines space as an array of equally sized cells

arranged in rows and columns. Each cell contains an attribute value. Unlike the vector type

which stores coordinates explicitly, raster coordinates are contained in the ordering of the

row/column matrix. You can think of raster as image with a geographic dimension.

Page 12: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Many formats exist for storing data in raster. The most popular are GeoTIFF, JPEG2000,

GRID, ECW. Keep in mind that web map services such as Google Maps, Bing Maps, Here

Maps and OpenStreetMap are based on raster since they returns images. In this exercise,

we will use OpenStreetMap.

As for Vector, GSuite uses the classes BaseRasterSource and BaseRasterLayer in the

same logic.

So, you can see that there are two different types of layers. Vector layers such as shapefiles

that are based on the vector model and raster layers that are based on the raster model.

Note that many organizations use a relational database to store its spatial data. They are

called spatial databases and are used to store vast amount of data and to work in a multi-user

environment. Popular spatial databases are Microsoft SQL server spatial, PostGIS and

SpatiaLite.

The geospatial data formats

You can find more detailed info on vector, raster, web map services and spatial

databases in the guides:

- Data guide

- Web Mapping Services guide

- Spatial Databases guide

In those guides, you will find valuable information on those different formats and

especially on how to extent BaseFeatureSource with your own format if you need to.

Page 13: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

It is also recommended to look at the complete internal documention on the core dlls. It

contains all the common classes used by all GSuite editions.

- Core API internal documentation

Map

The Map is the highest level object. You can think of the Map as the container and visualizer

of the layers you want to display. The Map allows the user to do navigation interactions such as

panning, zooming in, zooming out etc.

This map contains the web map service OpenStreetMap (raster layer) and a line based shapefile (vector layer)

representing rails roads of Brazil.

The Map control

The map is the visualizer and container of layers. The user can navigate the map by

mouse interaction.

The map is the front end object by excellence. It is in Mapgenix.GSuite.Wpf.dll. In that

dll, you will also found many classes specific to the WPF front end such as EditOverlays,

MapTool etc.

Look at the WPF edition API internal documentation for more details.

Page 14: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Now we are ready to code!

The namespaces

The first step is to set the namespaces for GSuite. GSuite Wpf uses a total of 11

namespaces.

One namespace for the front end WPF Map control: Mapgenix.GSuite.Wpf

Ten namespaces for the core, back end: Mapgenix.Canvas

Mapgenix.FeatureSource

Mapgenix.Gdal.Data

Mapgenix.Index

Mapgenix.Layers

Mapgenix.RasterSource

Mapgenix.Shapes

Mapgenix.Styles

Mapgenix.Symbology

Mapgenix.Utils

Mapgenix back ends namespaces

Each back end namespace correspond to a back end dll reference of the same name.

Brief description:

-Mapgenix.Canvas: contains classes related to map drawing, canvas, tiling, colors,

pen, brushes etc. It also contains the projection class.

-Mapgenix.FeatureSource: contains classes related to features, vector based data,

spatial databases, sql etc.

-Mapgenix.Gdal.Data: a branching of DotSpatial. It contains classes related to Gdal

and Ogr for supporting raster and vector data formats.

-Mapgenix.Index: contains classes related to the RTree spatial index.

-Mapgenix.Layers: contains classes related to layers such as vector, raster layers,

adornments etc.

-Mapgenix.RasterSource: contains classes related to raster data.

-Mapgenix.Shapes: contains classes related to shapes and geometry.

-Mapgenix.Styles: contains classes related to styling such as categorized, graduated

styles etc.

-Mapgenix.Symbology: is a branching of DotSpatial. It contains classes related to

styling Gdal and Ogr based layers.

Page 15: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

-Mapgenix.Utils: contains classes for performing utility routine functions in other

modules.

You can get more info in the the internal API documentation.

Note, that you don’t necessarily need all the namespaces depending on what you need

to do. For example, if you are not doing anything related to raster you don’t need the

namespace Mapgenix.RasterSource.

For this exercise, we need to include only 5 namespaces.

using Mapgenix.GSuite.Wpf; using Mapgenix.Canvas; using Mapgenix.Layers; using Mapgenix.Shapes; using Mapgenix.Styles;

Adding the layers to the map

Now we are ready to write the code to add the layers to the map in the Loaded event of the

form.

In this exercise, we are going to add OpenStreetMap (raster) as a background map and a

shapefile layer (vector) representing the countries of the world

(Countries_Spherical_Mercator.shp)

ESRI shapefile vector data format

Shapefile is one of the most popular vector data storage format for describing features:

points, lines and polygons. Each feature has attributes. All the features in one shapefile

are of the same type (point, line or polygon).

About spatial index: It is highly recommended to build a spatial index to your shapefile.

A spatial index improves substantially the drawing and spatial consulting speed. GSuite

uses R-Tree index and it has the extension .idx.

Page 16: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

To build the spatial index, use the code:

ShapeFileFeatureLayer.BuildIndexFile(“..//myshapefile.shp”,

BuildIndexMode.DoNotRebuild);

For Countries_Spherical_Mercator shapefile, the spatial index already exists so you

don’t have to call that code.

Setting the MapUnit of the Map

It is extremely important to set correctly the MapUnit property of the Map. If it is not set

correctly, zoom levels, spatial calculation, map navigation etc will not work correctly.

Fo that, you need to know if your data is unprojected or projected. If your data is unprojected,

set the MapUnit to DecimalDegrees (Longitude/Latitude). If it is projected set it to Meters. In

some cases for the USA, the MapUnit can be in feet.

In our case, we set MapUnit to Meters because all our data is projected (more specifically

Spherical Mercator) for both OpenStreetMap and the shapefile.

In the Loaded event of the form:

WPFMap.MapUnit = GeographyUnit.Meter;

Coordinate systems and map projections

In this exercise, the geographic data (Open Street Map and the shapefile layer) are in

the same world coordinate system (also commonly called projection). All the data is in

Spherical Mercator. You need to be aware that in many cases, not all the data will be in

the same coordinate, as it will be further on. This means that it is necessary to deal with

more advanced practices such as map projection conversions and datum

transformation.

Please read the blog post Demystifying map projections to better understand what you

are dealing with.

Adding OpenStreetMap as a background map

Page 17: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

OpenStreetMap is a tiled web map that can be used in GSuite as background map. It is

important to understand that they are basically image tiles to give context to the map but no

spatial analysis or editing can be done on it. Here we are using OpenStreetMap. We could just

as easily use other web map services such as Google Maps, HERE Maps or Bing Maps. Note

that we are also setting the cache folder to have more responsive drawing. Then we add the

OpenStreetMap layer to the layer collection of Overlays.

In the Loaded event of the form:

string cachePath = "C:\\Temp\\"; OpenStreetMapOverlay osmOverlay = new OpenStreetMapOverlay(); osmOverlay.CacheDirectory = cachePath; WPFMap.Overlays.Add("OSM", osmOverlay);

Tiled web map vs OGC WMS

Tiled web maps are a standard for displaying background world maps from street level

to world level. They all use the Spherical Mercator projection meaning that you can

switch from one to another without worrying about projection issues. The most popular

are:

-OpenStreetMaps

-GoogleMaps

-HereMaps

-BingMaps

Web Map Service (WMS) by Open Geospatial Consortium (OGC) is another standard

for serving maps. Thousands of organizations publish WMS maps and they can be in

any projection and about any themes.

Learn more in the Web Mapping Services guide.

Adding country shapefile to map

We add the shapefile Countries_Spherical_Mercator.shp to the map. It is going to show on

top of OpenStreetMap. Countries_Spherical_Mercator.shp can be found in the directory

SampleData where was installed the solution for the trial version of GSuite WPF. (See Installing

Trial Version of WPF Edition).

Page 18: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

The layer is of vector type so we need to specify the style to display it. Since, it is a

shapefile with geometry of type polygon, we need to use an AreaStyle. Keep in mind the

following rules for displaying vector layers such as shapefiles:

Layer with point geometry PointStyle

Layer with line geometry LineStyle

Layer with polygon geometry AreaStyle

The same style is applied from zoom level 1 (world level) to zoom level 20 (street level). Notice

that here some transparency is used for the inner color of the AreaStyle.

We also display the label for each country. We use the column ‘CNTRY_NAME’ of the dbf file of

the shapefile Countries_Spherical_Mercator. The overlapping rule is set to non overlapping so

that the labels are not displayed on top of each other.

As mentioned earlier, each shapefile has a dbf with attribute value. Here we use the column

‘CNTRY_NAME’, we could have use another column such as ‘CURR_TYPE’.

The visualization of spatial data

In this exercise, all the features are displayed and label in a uniform way. For more

advanced styling and labeling, please check the guides. In those guides, zoom levels

are also discussed.

- Style guide

- Label guide

Page 19: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Those guides address common issues of visualizations in mapping as explained in those

blog posts

- How to overcome the challenges of geospatial data

- Coloring in classification

Then the shapefile layer is added to the layer collection of a LayerOverlay. If more shapefile

layers are needed, they will be added to that same collection.

The LayerOverlay is added to the Overlays collection of the map.

In the Loaded event of the form:

ShapeFileFeatureLayer countryLayer = FeatureLayerFactory.CreateShapeFileFeatureLayer("C:\\MyShapefiles\\Countries_Spherical_Mercator.shp"); countryLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(80, GeoColor.StandardColors.LightGoldenrodYellow), GeoColor.StandardColors.LightGray); countryLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("CNTRY_NAME", "Arial", 12, DrawingFontStyles.Bold, GeoColor.StandardColors.Black); countryLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.OverlappingRule = LabelOverlappingRule.NoOverlapping;

countryLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

LayerOverlay staticOverlay = new LayerOverlay(); staticOverlay.Layers.Add("Countries", countryLayer); WPFMap.Overlays.Add(staticOverlay);

It is good custom to use a scaleline to show the graphic scale changing as the user zooms in

and out. It is also recommended to have the x and y in world coordinate showing at mouse

move over the map. It gives more context to your map.

In the Loaded event of the form:

WPFMap.MapTools.ScaleLine.IsEnabled = true;

Page 20: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

You can use a label below the map to show the x any coordinates of the maps.

In the MouseMove event of the Map:

private void WPFMap_MouseMove(object sender, MouseEventArgs e) { Point point = e.MouseDevice.GetPosition(null); PointShape pointShape = ExtentHelper.ToWorldCoordinate(WPFMap.CurrentExtent,

Convert.ToSingle(point.X), Convert.ToSingle(point.Y), Convert.ToSingle(WPFMap.Width), Convert.ToSingle(WPFMap.Height));

Label1.Content = (Math.Round(pointShape.X, 2).ToString() + “ “ + Math.Round(pointShape.Y, 2).ToString());

}

Also, it is necessary to set the geographic extent of the map. In other words, set the map where

it is going to be zoomed in. In this exercise, the extent of the map is based on the full extent of

the shapefile layer.

In the Loaded event of the form:

countryLayer.Open(); WPFMap.CurrentExtent = countryLayer.GetBoundingBox(); countryLayer.Close();

Doing some spatial analysis

Now we are going to scratch the surface of spatial analysis. Here we are going to do a

distance query on the countries layer to determine the countries within a certain distance of a

point.

We are going to need to add an InMemoryFeatureLayer to hold the point we are doing the

spatial query with and to hold the features from countries layer resulting from the distance

query. An InMemoryFeatureLayer is typically used for temporary features or features that

dynamically changes such as moving features.

First, we create the InMemoryFeature and set its styles. The style for point based features is a

red dot. The Style for area based features is an area style with yellow inner color and red outline

so that its shows highlighted. Of course, we could use any symbol we want and make visible

Page 21: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

features in that InMemoryFeatureLayer at different zoom levels. Then we create a

LayerOverlay for the InMemoryFeatureLayers.

In the Loaded event of the form:

InMemoryFeatureLayer inMemoryLayer = FeatureLayerFactory.CreateInMemoryFeatureLayer(); inMemoryLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Yellow, GeoColor.StandardColors.OrangeRed, 3); inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.StandardColors.Red, 10, GeoColor.StandardColors.Black, 1);

LayerOverlay highlightOverlay = new LayerOverlay(); highlightOverlay.Layers.Add("Highlight", inMemoryLayer); WPFMap.Overlays.Add("HighlightOverlay", highlightOverlay);

Layers, Overlays and the Map

You can think of a LayerOverlay as a collection of layers. You add the LayerOverlays

to the Overlay collection of the map. It is recommended to create a LayerOverlay to

manage similar layers. For example, put all the static layers such as shapefiles in one

LayerOverlay and all the dynamic layers such as InMemoryFeatureLayers in another.

That way you can refresh one LayerOverlay without having to refresh the entire map.

There are also TrackInteractiveOverlay and EditInteractiveOverlay for doing shape

editing.

Next, we add the point with coordinates -25 of longitude and 2 of latitude (in the middle of the Atlantic) to the InMemoryFeatureLayer. That point on the map is used as the reference geometry to perform the distance query. Before doing the distance query, we have to do a projection conversion of the point to Spherical Mercator.

Page 22: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Always be aware of the map projection!

We have the coordinates of the point in longitude and latitude (WGS84) but the map is in

a different coordinate system (Spherical Mercator). So, it is necessary to perform a

projection conversion to that point so that it aligns well with the rest of the data.

Always be aware of that your data might not be in the same coordinate system!

Proj4Projection proj4Projection = new Proj4Projection();

//Geographic Coordinate System (WGS84) http://spatialreference.org/ref/epsg/4326/ proj4Projection.InternalProjectionParametersString = Proj4Projection.GetWgs84ParametersString();

//Spherical Mercator http://spatialreference.org/ref/sr-org/7483/ proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetSphericalMercatorParametersString();

proj4Projection.Open();

PointShape queryPointShape =(PointShape)proj4Projection.ConvertToExternalProjection(new PointShape(-25,2)); //25 degree west of longitude and 2 degree north of latitude

proj4Projection.Close();

The projection library

GSuite uses the library Proj4 for map projections and coordinates conversion. Virtually

all the projections in the world are supported from WGS84, Spherical Mercator, UTM,

Stateplane to the most obscure national projections.

You can find all the projections in spatialreference.org.

The geometric shapes Notice that a PointShape with X and Y coordinates was created. GSuite has geometry classes such as PointShape, LineShape, PolygonShape, RectangleShape etc to construct shapes. In addition, you can do various geometric operations and geometric operations on them. You can find more information in the Spatial Analysis Guide.

Page 23: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

With the point now reprojected to match the base map (OpenStreetMap), we create a feature with that point. That feature is added to the InMemoryFeatureLayer.

inMemoryLayer.Open(); inMemoryLayer.Add(new Feature(queryPointShape)); inMemoryLayer.Close();

Feature: the basic unit of a vector layer

Feature is the base unit for all vector based layers or FeatureLayers. You can think of a

Feature as a single geographic entity with attributes. Regardless of the source, it can be

shapefile, tab file, spatial database, every single entity is a Feature. See the Data Guide

for more info on that.

We are ready to do the proximity analysis. We are going to do a distance query to find

all the countries with a distance of 1,750 kilometers from the point.

The result of the query is a collection of Features that we add to the

InMemoryFeatureLayer so that those features display highlighted on the map. Finally,

don’t forget to refresh the map so that all the layers show up.

countryLayer.Open();

Collection<Feature> queryResults = countryLayer.QueryTools.GetFeaturesWithinDistanceOf

(queryPointShape, WPFMap.MapUnit, DistanceUnit.Kilometer, 1750, ReturningColumnsType.NoColumns);

countryLayer.Close(); inMemoryLayer.Open(); foreach (Feature feature in queryResults) { inMemoryLayer.Add(feature.Id, feature); }

inMemoryLayer.Close();

WPFMap.Refresh();

Page 24: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Doing spatial analysis

QueryTools is a shortcut from a vector layer (inheriting from FeatureLayer) to access a

series of spatial and distance queries for doing spatial analysis such as:

-GetFeaturesContaining

-GetFeaturesCrossing

-GetFeaturesNearestTo

-GetFeaturesWithin

-GetFeaturesOverlapping

-And many more.

To get more information on that, please refer to the Spatial Analysis guide.

Now run the app and you should see the map as below.

The result of the distance query shows that from the point in the middle of the Atlantic there are eight countries within

1,750 kilometers. Those countries are Brazil as well

as seven African countries, Cape Verde, Senegal, Guinea-Bissau, Guinea, Sierra Leone, Liberia and The Gambia.

Page 25: GSuite WPF Edition Quick Start Guide - Mapgenix GSuite · GSuite WPF Edition Quick Start Guide ... -DotSpatial.Projections -DotSpatial.Serialization ... get the scale of the map,

Summary

Now that you know how to use GSuite WPF Edition and build a very simple map, you can add more

complex functionalities. Let's review some core concepts and the main object relationships of

GSuite:

1. You always need to set the unit of measurement properly, the MapUnit (decimal degrees or

meters, more rarely feet), based on your data.

2. Shapefiles (and other data sources like PostGIS, SpatiaLite etc) are the data to be used to

render maps in GSuite.

3. InMemoryFeatureLayer is used to store and display dynamic data

4. A Map is the basic class that contains all of the other objects that are used to define how the

map will be rendered.

5. A Map has one-to-many Overlays. An Overlay as a collection of layers. A Layer contains the

data (from shapefile or other data sources) for drawing.

6. A layer can have one-to-many ZoomLevels. ZoomLevels help to define ranges of at what scale

layers should be displayed or not.

7. ZoomLevel as Styles for the symbology and the labeling

8. And always be mindful of the map projection!