private void UpdateSpatialFences() { // Get the spatial fence overlay and layer and then clear them mapModel.SpatialFenceLayer.InternalFeatures.Clear(); // Get the spatial fences from the database Collection <Feature> spatialFences; TrackingAccessProvider vehicleProvider = new TrackingAccessProvider(dataRootPath); spatialFences = vehicleProvider.GetSpatialFences(); // Insert fences from database into fence layer if (spatialFences != null) { foreach (Feature spatialFence in spatialFences) { spatialFence.ColumnValues["Restricted"] = "Restricted"; mapModel.SpatialFenceLayer.InternalFeatures.Add(spatialFence.Id, spatialFence); } } }
private void SaveSpatialFences() { TrackingAccessProvider vehicleProvider = null; try { foreach (var item in mapModel.SpatialFenceLayer.FeatureIdsToExclude) { if (mapModel.SpatialFenceLayer.InternalFeatures.Contains(item)) { mapModel.SpatialFenceLayer.InternalFeatures.Remove(item); } } mapModel.SpatialFenceLayer.FeatureIdsToExclude.Clear(); foreach (Feature feature in mapModel.MapControl.TrackOverlay.TrackShapeLayer.InternalFeatures.Where(f => f.Tag != "Measure")) { if (!mapModel.SpatialFenceLayer.InternalFeatures.Contains(feature.Id)) { mapModel.SpatialFenceLayer.InternalFeatures.Add(feature.Id, feature); } } foreach (Feature feature in mapModel.MapControl.EditOverlay.EditShapesLayer.InternalFeatures) { if (!mapModel.SpatialFenceLayer.InternalFeatures.Contains(feature.Id)) { mapModel.SpatialFenceLayer.InternalFeatures.Add(feature.Id, feature); } } mapModel.SpatialFenceOverlay.Refresh(); vehicleProvider = new TrackingAccessProvider(dataRootPath); // Delete Spatial fences which is not in current spatial fence layer vehicleProvider.DeleteSpatialFencesExcluding(mapModel.SpatialFenceLayer.InternalFeatures); // Add or update the spatial fences that already exist foreach (Feature feature in mapModel.SpatialFenceLayer.InternalFeatures) { // Update Spatial fence by feature Id // if the affected data row number is 0, we will add a new row in the database vehicleProvider.UpdateSpatialFenceByFeature(feature); } } catch (Exception e) { MessageBox.Show(e.Message, "Save Spatial Fences"); } finally { mapModel.MapControl.TrackOverlay.TrackMode = TrackMode.None; mapModel.MapControl.TrackOverlay.TrackShapeLayer.InternalFeatures.Clear(); mapModel.MapControl.EditOverlay.EditShapesLayer.InternalFeatures.Clear(); mapModel.PopupOverlay.Popups.Clear(); mapModel.MapControl.Refresh(); if (drawFenceMode == DrawFenceMode.EditFence || drawFenceMode == DrawFenceMode.DrawNewFence) { DrawFenceMode = DrawFenceMode.DrawNewFence; ResetFences(); } } }
// Here we update the vehicles from the database. In this sample we create our data provider each time and // when we are finsihed with it we dispose it. This is very safe however you may get better performance // if you were to cache this. We wanted to sample to be clean and safe and it is up to your expertise to // enhance it further private void UpdateVehicles(LayerOverlay traceOverlay, DateTime currentTime) { Dictionary <int, Vehicle> currentVehicles; TrackingAccessProvider vehicleProvider = new TrackingAccessProvider(dataRootPath); currentVehicles = vehicleProvider.GetCurrentVehicles(currentTime); // Loop through all the vehicle to add the history points if (currentVehicles != null && currentVehicles.All(v => !string.IsNullOrEmpty(v.Value.IconPath))) { InMemoryFeatureLayer vehicleTrailLayer; // Initialize vehicle overlay if it's not initialized if (!traceOverlay.Layers.Contains("VehicleTrail")) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location vehicleTrailLayer = new InMemoryFeatureLayer(); vehicleTrailLayer.Open(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetVehicleTrailStyle(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later vehicleTrailLayer.FeatureSource.Open(); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Speed")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("DateTime")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Longitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Latitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add("VehicleTrail", vehicleTrailLayer); } })); } else { // Find the overlay in the map vehicleTrailLayer = (InMemoryFeatureLayer)traceOverlay.Layers["VehicleTrail"]; } vehicleTrailLayer.InternalFeatures.Clear(); foreach (int vehicleId in currentVehicles.Keys) { Vehicle currentVehicle = currentVehicles[vehicleId]; VehicleViewModel tempVehicleViewModel = Vehicles.FirstOrDefault(v => v.OwnerName == currentVehicle.Name); if (tempVehicleViewModel == null) { tempVehicleViewModel = new VehicleViewModel(currentVehicle, mapModel); Application.Current.Dispatcher.BeginInvoke(new Action(() => Vehicles.Add(tempVehicleViewModel))); } else { tempVehicleViewModel.Vehicle = currentVehicle; tempVehicleViewModel.Load(); } bool isInSpatialFence = IsInSpatialFence(currentVehicle); tempVehicleViewModel.Area = isInSpatialFence ? "In restricted area" : "Out of restricted area"; // Add the vheicle's bread crumbs foreach (Location historyLocation in currentVehicle.HistoryLocations.Take(6)) { Feature breadcrumbFeature = new Feature(historyLocation.GetLocation().GetWellKnownBinary(), currentVehicle.Name + historyLocation.DateTime.ToString(CultureInfo.InvariantCulture)); breadcrumbFeature.ColumnValues["DateTime"] = historyLocation.DateTime.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Speed"] = historyLocation.Speed.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Longitude"] = historyLocation.Longitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Latitude"] = historyLocation.Latitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["VehicleName"] = currentVehicle.Name; breadcrumbFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); vehicleTrailLayer.InternalFeatures.Add(breadcrumbFeature.Id, breadcrumbFeature); } InMemoryFeatureLayer currentVehicleLayer; if (!traceOverlay.Layers.Contains(currentVehicle.Name)) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location currentVehicleLayer = new InMemoryFeatureLayer(); currentVehicleLayer.Open(); currentVehicleLayer.Name = currentVehicle.Id.ToString(CultureInfo.InvariantCulture); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetCurrentVehicleStyle(currentVehicle.IconPath); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later currentVehicleLayer.FeatureSource.Open(); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Speed")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("DateTime")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Longitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Latitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add(currentVehicle.Name, currentVehicleLayer); } })); } else { // Find the overlay in the map currentVehicleLayer = (InMemoryFeatureLayer)traceOverlay.Layers[currentVehicle.Name]; } currentVehicleLayer.InternalFeatures.Clear(); // Add the vehicle's latest position Feature latestPositionFeature = new Feature(currentVehicle.Location.GetLocation().GetWellKnownBinary(), currentVehicle.Name); latestPositionFeature.ColumnValues["DateTime"] = currentVehicle.Location.DateTime.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Speed"] = currentVehicle.Location.Speed.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Longitude"] = currentVehicle.Location.Longitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Latitude"] = currentVehicle.Location.Latitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["VehicleName"] = currentVehicle.Name; latestPositionFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); currentVehicleLayer.InternalFeatures.Add(latestPositionFeature.Id, latestPositionFeature); } } }
// Here we update the vehicles from the database. In this sample we create our data provider each time and // when we are finsihed with it we dispose it. This is very safe however you may get better performance // if you were to cache this. We wanted to sample to be clean and safe and it is up to your expertise to // enhance it further private void UpdateVehicles(LayerOverlay traceOverlay, DateTime currentTime) { Dictionary<int, Vehicle> currentVehicles; using (TrackingAccessProvider vehicleProvider = new TrackingAccessProvider(databasePathFileName)) { currentVehicles = vehicleProvider.GetCurrentVehicles(currentTime); } // Loop through all the vehicle to add the history points if (currentVehicles != null && currentVehicles.All(v => !string.IsNullOrEmpty(v.Value.IconPath))) { InMemoryFeatureLayer vehicleTrailLayer; // Initialize vehicle overlay if it's not initialized if (!traceOverlay.Layers.Contains("VehicleTrail")) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location vehicleTrailLayer = new InMemoryFeatureLayer(); vehicleTrailLayer.Open(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetVehicleTrailStyle(); vehicleTrailLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later vehicleTrailLayer.FeatureSource.Open(); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Speed")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("DateTime")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Longitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Latitude")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); vehicleTrailLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add("VehicleTrail", vehicleTrailLayer); } })); } else { // Find the overlay in the map vehicleTrailLayer = (InMemoryFeatureLayer)traceOverlay.Layers["VehicleTrail"]; } vehicleTrailLayer.InternalFeatures.Clear(); foreach (int vehicleId in currentVehicles.Keys) { Vehicle currentVehicle = currentVehicles[vehicleId]; VehicleViewModel tempVehicleViewModel = Vehicles.FirstOrDefault(v => v.OwnerName == currentVehicle.Name); if (tempVehicleViewModel == null) { tempVehicleViewModel = new VehicleViewModel(currentVehicle, mapModel); Application.Current.Dispatcher.BeginInvoke(new Action(() => Vehicles.Add(tempVehicleViewModel))); } else { tempVehicleViewModel.Vehicle = currentVehicle; tempVehicleViewModel.Load(); } bool isInSpatialFence = IsInSpatialFence(currentVehicle); tempVehicleViewModel.Area = isInSpatialFence ? "In restricted area" : "Out of restricted area"; // Add the vheicle's bread crumbs foreach (Location historyLocation in currentVehicle.HistoryLocations.Take(6)) { Feature breadcrumbFeature = new Feature(historyLocation.GetLocation().GetWellKnownBinary(), currentVehicle.Name + historyLocation.DateTime.ToString(CultureInfo.InvariantCulture)); breadcrumbFeature.ColumnValues["DateTime"] = historyLocation.DateTime.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Speed"] = historyLocation.Speed.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Longitude"] = historyLocation.Longitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["Latitude"] = historyLocation.Latitude.ToString(CultureInfo.InvariantCulture); breadcrumbFeature.ColumnValues["VehicleName"] = currentVehicle.Name; breadcrumbFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); vehicleTrailLayer.InternalFeatures.Add(breadcrumbFeature.Id, breadcrumbFeature); } InMemoryFeatureLayer currentVehicleLayer; if (!traceOverlay.Layers.Contains(currentVehicle.Name)) { // Create an InMemoryMarkerOverlay for the vehicle to hold the points and current location currentVehicleLayer = new InMemoryFeatureLayer(); currentVehicleLayer.Open(); currentVehicleLayer.Name = currentVehicle.Id.ToString(CultureInfo.InvariantCulture); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = GetCurrentVehicleStyle(currentVehicle.IconPath); currentVehicleLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; // Add all the required columns so we can populate later currentVehicleLayer.FeatureSource.Open(); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Speed")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("DateTime")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Longitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Latitude")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("VehicleName")); currentVehicleLayer.Columns.Add(new FeatureSourceColumn("Duration")); Application.Current.Dispatcher.BeginInvoke(new Action(() => { lock (traceOverlay.Layers) { traceOverlay.Layers.Add(currentVehicle.Name, currentVehicleLayer); } })); } else { // Find the overlay in the map currentVehicleLayer = (InMemoryFeatureLayer)traceOverlay.Layers[currentVehicle.Name]; } currentVehicleLayer.InternalFeatures.Clear(); // Add the vehicle's latest position Feature latestPositionFeature = new Feature(currentVehicle.Location.GetLocation().GetWellKnownBinary(), currentVehicle.Name); latestPositionFeature.ColumnValues["DateTime"] = currentVehicle.Location.DateTime.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Speed"] = currentVehicle.Location.Speed.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Longitude"] = currentVehicle.Location.Longitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["Latitude"] = currentVehicle.Location.Latitude.ToString(CultureInfo.InvariantCulture); latestPositionFeature.ColumnValues["VehicleName"] = currentVehicle.Name; latestPositionFeature.ColumnValues["Duration"] = currentVehicle.GetSpeedDuration().ToString(CultureInfo.InvariantCulture); currentVehicleLayer.InternalFeatures.Add(latestPositionFeature.Id, latestPositionFeature); } } }
private void UpdateSpatialFences() { // Get the spatial fence overlay and layer and then clear them mapModel.SpatialFenceLayer.InternalFeatures.Clear(); // Get the spatial fences from the database Collection<Feature> spatialFences; using (TrackingAccessProvider vehicleProvider = new TrackingAccessProvider(databasePathFileName)) { spatialFences = vehicleProvider.GetSpatialFences(); } // Insert fences from database into fence layer if (spatialFences != null) { foreach (Feature spatialFence in spatialFences) { spatialFence.ColumnValues["Restricted"] = "Restricted"; mapModel.SpatialFenceLayer.InternalFeatures.Add(spatialFence.Id, spatialFence); } } }
private void SaveSpatialFences() { TrackingAccessProvider vehicleProvider = null; try { foreach (var item in mapModel.SpatialFenceLayer.FeatureIdsToExclude) { if (mapModel.SpatialFenceLayer.InternalFeatures.Contains(item)) { mapModel.SpatialFenceLayer.InternalFeatures.Remove(item); } } mapModel.SpatialFenceLayer.FeatureIdsToExclude.Clear(); foreach (Feature feature in mapModel.MapControl.TrackOverlay.TrackShapeLayer.InternalFeatures.Where(f => f.Tag != "Measure")) { if (!mapModel.SpatialFenceLayer.InternalFeatures.Contains(feature.Id)) { mapModel.SpatialFenceLayer.InternalFeatures.Add(feature.Id, feature); } } foreach (Feature feature in mapModel.MapControl.EditOverlay.EditShapesLayer.InternalFeatures) { if (!mapModel.SpatialFenceLayer.InternalFeatures.Contains(feature.Id)) { mapModel.SpatialFenceLayer.InternalFeatures.Add(feature.Id, feature); } } mapModel.SpatialFenceOverlay.Refresh(); vehicleProvider = new TrackingAccessProvider(databasePathFileName); // Delete Spatial fences which is not in current spatial fence layer vehicleProvider.DeleteSpatialFencesExcluding(mapModel.SpatialFenceLayer.InternalFeatures); // Add or update the spatial fences that already exist foreach (Feature feature in mapModel.SpatialFenceLayer.InternalFeatures) { // Update Spatial fence by feature Id // if the affected data row number is 0, we will add a new row in the database int updatedCount = vehicleProvider.UpdateSpatialFenceByFeature(feature); if (updatedCount == 0) { vehicleProvider.InsertSpatialFence(feature); } } } catch (OleDbException) { MessageBox.Show(string.Format(CultureInfo.InvariantCulture, "Please make sure {0} is writable", databasePathFileName), "Save Spatial Fences"); } catch (Exception e) { MessageBox.Show(e.Message, "Save Spatial Fences"); } finally { mapModel.MapControl.TrackOverlay.TrackMode = TrackMode.None; mapModel.MapControl.TrackOverlay.TrackShapeLayer.InternalFeatures.Clear(); mapModel.MapControl.EditOverlay.EditShapesLayer.InternalFeatures.Clear(); mapModel.PopupOverlay.Popups.Clear(); mapModel.MapControl.Refresh(); if (vehicleProvider != null) { vehicleProvider.Dispose(); } if (drawFenceMode == DrawFenceMode.EditFence || drawFenceMode == DrawFenceMode.DrawNewFence) { DrawFenceMode = DrawFenceMode.DrawNewFence; ResetFences(); } } }