As a universal development platform, both Silverlight and JavaScript APIs can render map data of other vendors. This article uses rendering esri shp format map data as an example, describes in detail how to render Esri SHP Map data on Bing Map Silverlight Control. Because BMSC does not have the corresponding data reading API, it needs to use third-party open-source components to read SHP, and the data is provided to the client by publishing the WCF Service.
I. Open-source components
1. NetTopologySuite -- Address: http://code.google.com/p/nettopologysuite/ 2. GeoAPI -- GeoAPI is part of the open source project SharpMap.
Ii. esri shp Data Reading and publishing the WCF ServiceUse the open-source component API above to read esri shp map data and extract all the space data in it into the WKT format. Then, publish the data to the client for calling through the service. 1 /// <summary>
2 // read the SHP file in the specified path and return the space object set
3 /// </summary>
4 /// <param name = "shapepath"> SHP file path </param>
5 /// <returns> set of spatial objects </returns>
6 private IGeometryCollection ReadShape (string shapepath)
7 {
8 // determine whether a file exists
9 if (! File. Exists (shapepath ))
10 throw new ArgumentException ("file" + shapepath + "not found! ");
11
12 // read the space data of the SHP file and return the space object set
13. ShapefileReader reader = new ShapefileReader (shapepath );
14 IGeometryCollection geometries = reader. ReadAll ();
15 return geometries;
16} The Silverlight client can conveniently call the interface to obtain data by using the external service publishing interface of WCF.
1 /// <summary>
2 // read SHP map data and publish the WCF Service
3 /// </summary>
4 /// <returns> return the WKT of the spatial object </returns>
5 public List <string> GetShapeDate ()
6 {
7 string shpPath = "D: \ BingMapDemo. DataService \ App_Data \ region boundary _ polyline. shp ";
8 IGeometryCollection collection = ReadShape (shpPath );
9
10 List <string> result = new List <string> ();
11 foreach (IGeometry item in collection. Geometries)
12 {
13 // store space data in WKT format
14 result. Add (item. AsText ());
15}
16 return result;
17}
Iii. Coordinate TransformationBecause the space data format stored by the GeoAPI space object (IGeometry) cannot be completely compatible with the space coordinate object in Bing Map Silverlight Control, after the client obtains the data, it must convert the Coordinate object of GeoAPI to the Location of Bing Map Silverlight Control to be compatible with the use of Coordinate data.
1 public static class CoordinateConvertor
2 {
3 public static ICoordinate Convert (Location location)
4 {
5 return new Coordinate {Y = location. Latitude, X = location. longdistance };
6}
7
8 public static Location ConvertBack (ICoordinate location)
9 {
10 return new Location {Latitude = location. Y, longpolling = location. X };
11}
12
13 public static LocationCollection CoordinatesToLocationCollection (ICoordinate [] coordinates)
14 {
15 var locations = new LocationCollection ();
16 foreach (var coordinate in coordinates)
17 {
18 locations. Add (ConvertBack (coordinate ));
19}
20 return locations;
21}
22
23 public static ICoordinate [] LocationCollectionToCoordinates (LocationCollection locations)
24 {
25 var coordinates = new Coordinate [locations. Count];
26 for (var x = 0; x <locations. Count; x ++)
27 {
28 coordinates [x] = (Coordinate) Convert (locations [x]);
29}
30 return (ICoordinate []) coordinates;
31}
32
33 public static LocationCollection LocationRectToLocationCollection (LocationRect locationRect)
34 {
35 var locations = new LocationCollection
36 {
37 locationRect. Northwest,
38 locationRect. Southwest,
39 locationRect. Southeast,
40 locationRect. Northeast,
41 locationRect. Northwest
42 };
43 return locations;
44}
45}
4. Rendering SHP space dataThe space data in the SHP file is published to the client in WK format through WCF, and the client reads the WKT as the space object of GeoAPI, then, coordinates are converted to the coordinate data format compatible with Bing Map Silverlight Control by means of coordinate transformation, and the data is rendered in the form of Polygon on the client side. 1 private void ShpMapReader ()
2 {
3 GeometryServiceClient service = new GeometryServiceClient ();
4 service. GetShapeDateCompleted + = service_GetShapeDateCompleted;
5 service. GetShapeDateAsync ();
6}
7
8 private void service_GetShapeDateCompleted (object sender, GetShapeDateCompletedEventArgs e)
9 {
10 if (e. Error = null)
11 {
12 ObservableCollection <string> result = e. Result;
13 WKTReader reader = null;
14 foreach (var item in result)
15 {
16 reader = new WKTReader ();
17 // read WKT to a space object
18 IGeometry geometry = reader. Read (item );
19 if (geometry = null) continue;
20 // Boundary
21 MapPolygon line = new MapPolygon ();
22 // convert coordinates
23 line. Locations = CoordinateConvertor. CoordinatesToLocationCollection (geometry. Coordinates );
24 line. Fill = new SolidColorBrush (Colors. Gray );
25 line. BorderBrush = new SolidColorBrush (Colors. Green );
26 line. BorderThickness = new Thickness (2 );
27 line. MouseEnter + = new MouseEventHandler (line_MouseEnter );
28 line. MouseLeave + = new MouseEventHandler (line_MouseLeave );
29 this. mlayer. Children. Add (line );
30}
31}
32} The implementation of rendering esri shp space data in Bing Maps is very simple. If you have mastered the application open-source components, you can read the data. Below are the operations.
5. Related Resources:
[1], SharpMap: http://sharpmap.codeplex.com
[2], NetTopologySuite: http://code.google.com/p/nettopologysuite
[3], WKT: http://www.opengis.org/techno/specs.htm
Description
This document is a learning note and is intended to be shared with people with lofty ideals. You are welcome to repost this article. To save the author's Creative Enthusiasm, mark the original article connection in a prominent position.
Author: Beniao
Article Source: http://beniao.cnblogs.com/or http://www.cnblogs.com/