Table of Contents

Source Code WpfDesktopEditionSample SQLInMemoryLayer CS 140609.zip

TestWindow.xaml

 <Window x:Class="CustomRotationProjection.TestWindow"  
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
           xmlns:my="clr-namespace:ThinkGeo.MapSuite.WpfDesktopEdition;assembly=WpfDesktopEdition"  
     Title="Displaying and Editing SQL Data on the map with an InMemoryFeatureLayer" Height="640" Width="800"  
         Loaded="Window_Loaded" >  
     <Grid Height="Auto">  
         <Grid.ColumnDefinitions>  
             <ColumnDefinition Width="778*" />  
         </Grid.ColumnDefinitions>  
         <my:WpfMap Name="wpfMap1" />  
         <StackPanel VerticalAlignment="Top" HorizontalAlignment="Right" Background="White" Width="181">  
             <Label Content="Load Data from Database Using" HorizontalAlignment="Left"  VerticalAlignment="Top"/>  
             <RadioButton GroupName="ColumnType" Name="WKT" Content="WKT Column" Checked="LoadData_Checked" IsChecked="True" />  
             <RadioButton GroupName="ColumnType" Name="XandY" Content="X and Y Columns (Points Only)"  Checked="LoadData_Checked" />  
             <RadioButton GroupName="ColumnType" Name="Geometry" Content="Geometry Column" Checked="LoadData_Checked" />  
             <Label x:Name="lblEditFeature" Content="Edit Feature" Width="157" />  
             <ComboBox x:Name="cbo_Features" SelectionChanged="cbo_Features_SelectionChanged" Width="153"/>  
             <Label></Label>  
             <Button x:Name="btn_SaveFeature" Content="Save Feature" Click="btn_SaveFeatures_Click" IsEnabled="False" Width="144"/>  
             <Label></Label>  
         </StackPanel>  
     </Grid>  
 </Window>  
 

TestWindow.xaml.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.IO;  
 using System.Windows;  
 using System.Windows.Controls;  
 using System.Windows.Data;  
 using System.Windows.Documents;  
 using System.Windows.Input;  
 using System.Windows.Media;  
 using System.Windows.Media.Imaging;  
 using System.Windows.Shapes;  
 using System.Collections.ObjectModel;  
 using ThinkGeo.MapSuite.Core;  
 using ThinkGeo.MapSuite.WpfDesktopEdition;  
 using System.Data.SqlClient;  
 
 namespace CustomRotationProjection  
 {  
     /// <summary>  
     /// Interaction logic for TestWindow.xaml  
     /// </summary>  
     public partial class TestWindow : Window  
     {  
 
         public TestWindow()  
         {  
             InitializeComponent();  
         }  
 
         private void Window_Loaded(object sender, RoutedEventArgs e)  
         {  
 
             //Add A Basemap of the World Map Kit  
             wpfMap1.MapUnit = GeographyUnit.DecimalDegree;  
             WorldMapKitWmsWpfOverlay worldOverlay = new WorldMapKitWmsWpfOverlay();  
             wpfMap1.Overlays.Add("WMK", worldOverlay);  
 
             //Setup an InMemoryLayer to hold the data we query from our data source.  
             InMemoryFeatureLayer myData = loadSQLDataIIntoInMemoryLayer();  
 
             //Create a layer overlay to hold our data and so we can display it on top of the base map.  
             LayerOverlay layerOverlay = new LayerOverlay();  
             //Set the layer overlay IsBase to false so it's not considered a base map and so our data will be displayed on top.  
             layerOverlay.IsBase = false;  
             layerOverlay.Layers.Add("myData", myData);  
 
             //Add the overlay to the map.  
             wpfMap1.Overlays.Add("layerOverlay", layerOverlay);  
 
             //Zoom into our data held in a SQL Database  
             myData.Open();  
             wpfMap1.CurrentExtent = myData.GetBoundingBox();  
             wpfMap1.Refresh();  
         }  
 
         private InMemoryFeatureLayer loadSQLDataIIntoInMemoryLayer()  
         {  
             cbo_Features.Items.Clear();  
             cbo_Features.Items.Add("");  
             //Setup an InMemoery Layer with our Test Point and Test Line  
             InMemoryFeatureLayer myData = new InMemoryFeatureLayer();  
             myData.Open();  
 
             //Create the colummns that will hold the attribute data about the shape that we care about coming back from the data source.  
             myData.Columns.Add(new FeatureSourceColumn("FeatureID"));  
             myData.Columns.Add(new FeatureSourceColumn("Name"));  
 
             //Create connection to your datasource  
             SqlConnection dbcon = new SqlConnection("Server=(Local);Database=TestSQLSpatial;Trusted_Connection=True;");  
             dbcon.Open();  
             SqlCommand command = dbcon.CreateCommand();  
             command.CommandText = "Select FeatureID, Name, XPoint, YPoint, ShapeWKT, ShapeGeometry.STAsBinary() as 'ShapeGeometry' From TestData";  
             SqlDataReader datareader = command.ExecuteReader();  
 
             //Begin Adding the Test Features to the Layer  
             myData.EditTools.BeginTransaction();  
             while (datareader.Read())  
             {  
                 Feature myFeature = new Feature();  
 
                 if (this.XandY.IsChecked == true)  
                 {  
                     //Check to make sure the X & Y Columns aren't Null if they are continue to next record  
                     if (datareader["XPoint"] != DBNull.Value && datareader["YPoint"] != DBNull.Value)  
                     {  
                         //Create a Feature based on an X & Y columns in our DB  
                         myFeature = new Feature(Convert.ToDouble(datareader["XPoint"]), Convert.ToDouble(datareader["YPoint"]));  
                     }  
                     else {continue;}  
                 }  
                 if (this.WKT.IsChecked == true)  
                 {  
                     //Create a Feature based on the WKT column in our DB  
                     myFeature = new Feature(datareader["ShapeWKT"].ToString());  
                 }  
                 if (this.Geometry.IsChecked == true)  
                 {  
                     //Create a Feature based on the Geometry column in our DB  
                     myFeature = new Feature((byte[])datareader["ShapeGeometry"]);  
                 }  
 
                 //Set the name of the Test Feature to Test Point  
                 myFeature.ColumnValues["FeatureID"] = datareader["FeatureID"].ToString();  
                 myFeature.ColumnValues["Name"] = datareader["Name"].ToString();  
                 //Add the Feature to the InMemoryLayer  
                 myData.EditTools.Add(myFeature);  
                 cbo_Features.Items.Add(datareader["Name"].ToString());  
             }  
 
             dbcon.Close();  
             //Commit the Changes to the InMemoryLayer  
             myData.EditTools.CommitTransaction();  
 
             //Define how we want the different shapes to be rendered on the map.  
             //Make all Points Red Stars  
             myData.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleStarStyle(GeoColor.StandardColors.Red, 16);  
             //Make All Lines Dark Green  
             myData.ZoomLevelSet.ZoomLevel01.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.SimpleColors.Green, 6, false);  
             //Make All Polygons Red with a black outline.  
             myData.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Red, GeoColor.StandardColors.Black);  
             //Add a Text Style to Label the shapes on the map.  
             myData.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateSimpleTextStyle("Name", "Arial", 10, DrawingFontStyles.Bold, GeoColor.StandardColors.Black);  
             myData.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.HaloPen = new GeoPen(GeoColor.StandardColors.White);  
             //Apply these styles to all zoom levels  
             myData.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
 
             return myData;  
         }  
 
         private void LoadData_Checked(object sender, RoutedEventArgs e)  
         {  
             if (wpfMap1.Overlays.Contains("layerOverlay"))  
             {  
                 //Get a reference to our InMemoryLayer that we created when the program started.  
                ((LayerOverlay)wpfMap1.Overlays["layerOverlay"]).Layers["myData"] = loadSQLDataIIntoInMemoryLayer();  
                 wpfMap1.Refresh();  
             }  
         }  
 
         private void cbo_Features_SelectionChanged(object sender, SelectionChangedEventArgs e)  
         {  
 
             //Clear out any features that may still be in the EditShapesLayer  
             wpfMap1.EditOverlay.EditShapesLayer.InternalFeatures.Clear();  
 
             if (cbo_Features.Items.Count > 0 && cbo_Features.SelectedValue.ToString() != "")  
             {  
                 btn_SaveFeature.IsEnabled = true;  
                 //Get a Reference to InMemoryFeatureLayer  
                 InMemoryFeatureLayer myData = ((InMemoryFeatureLayer)((LayerOverlay)wpfMap1.Overlays["layerOverlay"]).Layers["myData"]);  
 
                 wpfMap1.EditOverlay.EditShapesLayer.Open();  
                 //Check to see if this the first time we have ever edited the features if so add a columns to the EditShapesLayer so are attributes on the feature is stored correctly  
                 if (wpfMap1.EditOverlay.EditShapesLayer.Columns.Count == 0)  
                 {  
                     wpfMap1.EditOverlay.EditShapesLayer.Columns.Add(new FeatureSourceColumn("Name"));  
                     wpfMap1.EditOverlay.EditShapesLayer.Columns.Add(new FeatureSourceColumn("FeatureID"));  
                 }  
                 //Find the feature selected in the Combo Box in our InMemoryFeatureLayer  
                 Feature selectedFeature = myData.QueryTools.GetFeaturesByColumnValue("Name", cbo_Features.SelectedValue.ToString())[0];  
                 //Add the Selected Feature to the EditShapesLayer  
                 wpfMap1.EditOverlay.EditShapesLayer.InternalFeatures.Add(selectedFeature);  
                 //Calculate the Control Points for the feature so you can edit it interactively  
                 wpfMap1.EditOverlay.CalculateAllControlPoints();  
 
 
             }  
             else  
             {  
                 btn_SaveFeature.IsEnabled = false;  
             }  
             //Refresh the map  
             wpfMap1.Refresh();  
         }  
 
         private void btn_SaveFeatures_Click(object sender, RoutedEventArgs e)  
         {  
 
             //Check to Make Sure that we hae some features in the edit overlay  
             if (wpfMap1.EditOverlay.EditShapesLayer.InternalFeatures.Count != 0)  
             {  
                 // Build up the update SQL statement  
                 string updateSQL = "Update TestData Set ";  
                 Feature updatedFeature = wpfMap1.EditOverlay.EditShapesLayer.InternalFeatures[0];  
                 if (updatedFeature.GetWellKnownType().ToString() == "Point")  
                 {  
                     PointShape updatedPointShape = new PointShape(updatedFeature.GetWellKnownText());  
                     updateSQL += "XPoint =  " + updatedPointShape.X.ToString() + ", ";  
                     updateSQL += "YPoint =  " + updatedPointShape.Y.ToString() + ", ";  
                 }  
                 else  
                 {  
                     updateSQL += "XPoint = Null, ";  
                     updateSQL += "YPoint = Null, ";  
                 }  
                 updateSQL += "ShapeWKT = '" + updatedFeature.GetWellKnownText() + "', ";  
                 updateSQL += "ShapeGeometry = " + "geometry::STGeomFromText('" + updatedFeature.GetWellKnownText() + "', 0) ";  
                 updateSQL += "Where Name = '" + updatedFeature.ColumnValues["Name"].ToString() + "'";  
 
                 //Create connection to your datasource  
                 //You will need to point this to your SQL Server and run the TestDataSQLScript.txt in this solution to create test table and test data  
                 SqlConnection dbcon = new SqlConnection("Server=(Local);Database=TestSQLSpatial;Trusted_Connection=True;");  
                 dbcon.Open();  
                 SqlCommand command = dbcon.CreateCommand();  
                 command.CommandText = updateSQL;  
                 command.ExecuteNonQuery();  
                 dbcon.Close();  
                 //Remove the Old Ldayer InMemory Layer.  
                 ((LayerOverlay)wpfMap1.Overlays["layerOverlay"]).Layers.Remove("myData");  
                 //Reload the Data that we just updated from the database so we can update the map.  
                 ((LayerOverlay)wpfMap1.Overlays["layerOverlay"]).Layers.Add("myData", loadSQLDataIIntoInMemoryLayer());  
             }  
             //Clear out any features in the EditShapes Layer and Refresh the Map.  
             wpfMap1.EditOverlay.EditShapesLayer.InternalFeatures.Clear();  
             wpfMap1.Refresh();  
         }  
     }  
 }