Telerik blogs

Since the introduction of RadMap many customers have expressed a need to load shapefiles onto it.  We are adding native support for shapefiles in the Q3 Release, along with vastly improving the performance of information, and dynamic layers.  However, the beta is still a few weeks away, and I had a need for loading shapefiles this week, so I put this quick example together.

How to do it

Shapefile parsing in this demo is handled by the reader available in the ESRI Contrib project.  When we make the Q3 release you will be able to switch over to our reader by changing about two lines of code :)

The most basic code required to load a shapefile is shown below:

   1: ShapeFile shapeFileReader = new ShapeFile();
   2: shapeFileReader.Read(shapeFile, dbfFile);
   3: var mapShapes = shapeFileReader.Records.ToMapElements();
   4:  
   5: mapShapes.ToList().ForEach(shape=> this.InformationLayer.Items.Add(shape));

The first thing that needs to be done is parse the shapefile.  After that I call an extension method, which converts the native ShapeFile shapes into MapShapes. The converter also stores the shape record’s attributes in the tag of the new map shape for easy retrieval later. The final step is to add all the shapes to an information layer.  At this point the shapefile has been successfully loaded!  

Unfortunately, nothing will be visible since we have not set any colors for strokes/background.  We actually need to do a little more when adding the elements to the information layer:

   1: mapShapes.ToList().ForEach(shape =>
   2:                            {
   4:                                var mapShape = shape as MapShape;
   5: 
   6: if (mapShape != null)
   7:                                {
   8:                                    mapShape.Fill = new SolidColorBrush(Colors.Yellow);
   9:                                    mapShape.StrokeThickness = 1f;
  10:                                    mapShape.Stroke = new SolidColorBrush(Colors.Black);
  11:                                }
  12: 
  13:                                InformationLayer.Items.Add(shape);
  14:                            });

Now our shapes will be visible, and have a yellow background with a black stroke.  

Demo

I created a simple demo that displays a shapefile, each shape is yellow with a black stroke as discussed above.  When you click a shape it will turn pink. Also, I am logging several mouse events to the event window on the right side.  Along with the events, I output some information about the shape you are interacting with.  For example, in the preloaded shapefile I output the name of the Texas county pulled out of the shapefile.

 

Give it a try:


About the Author

Manol Donev

Technical Lead,
WinCore Team

Comments

Comments are disabled in preview mode.