//When our maps are created, all map features, etc. are built based on data in SQL Tables. //The system runs "many" queries to generate the //data required and a DataSet is created with 3 Tables (see below). //There are checkboxes in the UI that determine if Hospitals, Physicians or Zip Coded Polygons display... public void RebuildMap(bool ifResetExtent) { //What tables do we have available? int HOSPITAL_TABLE = 0; int DETAIL_TABLE = 1; int DOCTOR_TABLE = 2; //First Remove Previous Layers ClearMap(); MapControl.ThreadingMode = MapThreadingMode.Default; bool ifMarkerLayerAdded = false; if (mapFilters == null) { mapFilters = new MapFilters(oApp, this); //Map Filters run may queries to produce the data. } if (mapFilters.ReturnDataSet != null && chkDisplayHospitals.Checked && mapFilters.ReturnDataSet.Tables.Count > 0) { //Add Hospital Layer try { HospitalTable = mapFilters.ReturnDataSet.Tables[HOSPITAL_TABLE]; MarkerLayer.SetupMarkerLayer(HospitalTable, mwMarkerLayer.MarkerLayerType.hospital, MapControl); ifMarkerLayerAdded = true; } catch { HospitalTable = null; } } if (mapFilters.ReturnDataSet != null && chkDisplayPhysicians.Checked && mapFilters.ReturnDataSet.Tables.Count > 2) { //Add Doctor Layer try { PhysicianTable = mapFilters.ReturnDataSet.Tables[DOCTOR_TABLE]; if (PhysicianTable.Rows.Count > 1000) { if (bc.YesNoMessage("The number of physicians exceeds 1000, Continue?") == System.Windows.Forms.DialogResult.No) return; } if (PhysicianTable.Rows.Count > 0) { MarkerLayer.SetupMarkerLayer(PhysicianTable, mwMarkerLayer.MarkerLayerType.doctor, MapControl); SetupCheckedList(); ifMarkerLayerAdded = true; } } catch { PhysicianTable = null; } } if (ifMarkerLayerAdded) { MapControl.Overlays.Add("MarkerOverlay", MarkerLayer); MapControl.Refresh(); } //Build Detail layer (Polygons) if (!optNone.Checked) { try { DetailTable = mapFilters.ReturnDataSet.Tables[DETAIL_TABLE]; if (DetailTable.Rows.Count > 0) { // First Build Legend Layer string LegendTitle = "Physician Counts"; if (optInternalDecile.Checked) LegendTitle = "Internal Decile"; if (optClaimsDecile.Checked) LegendTitle = "Claims Decile"; if (optInternalCases.Checked) LegendTitle = "Internal Cases"; LegendLayer = new mwLegend(); LegendLayer.BuildLegend(DetailTable, MapControl, LegendTitle); BuildDetailLayer(DetailTable, LegendLayer,ifResetExtent); } else { DetailTable = null; } } catch { } //Ignore } //Now Refresh the Entire Map and button status MapControl.ThreadingMode = MapThreadingMode.Multithreaded; MapControl.Refresh(); } private void BuildDetailLayer(DataTable table, mwLegend legend,bool ifResetExtent) { //Setup Detail Layer (Zip Code Polygons) DetailLayer = new mwMapLayer(table, "name", "location", "", mwMapLayer.FeatureType.polygons); if (DetailLayer.ifFeaturesInitialized) { DetailLayer.Open(); DetailLayer.Columns.Add(new FeatureSourceColumn("TotalSelected")); DetailLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; ClassBreakStyle Breaks = new ClassBreakStyle("TotalSelected"); for (int j = 0; j < 5; j++) { AreaStyle aStyle = new AreaStyle(new GeoPen(GeoColor.StandardColors.Black), new GeoSolidBrush(GeoColor.FromArgb(100, legend.ScaleColors[j]))); Breaks.ClassBreaks.Add(new ClassBreak(legend.nScaleValue[j], aStyle)); } DetailLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(Breaks); DetailLayer.Close(); LayerOverlay Layer = new LayerOverlay(); Layer.Layers.Add("DetailLayer1", DetailLayer); MapControl.Overlays.Add("DetailLayer", Layer); if (ifResetExtent) { try { DetailLayer.FeatureSource.Open(); RectangleShape Box = DetailLayer.FeatureSource.GetBoundingBox(); SetCurrentExtent(Box); DetailLayer.FeatureSource.Close(); } catch (SystemException ex) { bc.OKMessage(ex.Message); } } } } //Here are the two classes used by our map to create the Markers and Polygon Shapes public class mwMarkerLayer : SimpleMarkerOverlay { public enum MarkerLayerType { doctor, hospital, visit }; private System.Windows.Forms.Form MapForm; public InMemoryFeatureLayer MarkerBoundingLayer = new InMemoryFeatureLayer(); public List MarkerPoints = new List(); public List MarkerList = new List(); public mwMarkerLayer(System.Windows.Forms.Form mapform) { MapForm = mapform; } public bool SetupMarkerLayer(DataTable markerTable, MarkerLayerType type, WinformsMap mapcontrol) { int nPreviousListCount = MarkerList.Count; int nNewListCount = markerTable.Rows.Count; FillMarkerList(markerTable, type); Image MarkerImage = null; switch (type) { case MarkerLayerType.doctor: MarkerImage = global::Marketware.Resource1.Doctor24BB; break; case MarkerLayerType.hospital: MarkerImage = global::Marketware.Resource1.Hospital24; break; case MarkerLayerType.visit: MarkerImage = global::Marketware.Resource1.visit24; break; } try { if (MapControl == null) MapControl = mapcontrol; for (int j = nPreviousListCount; j < nPreviousListCount + nNewListCount; j++) { MapAddress Ma = MarkerList[j]; if (Ma.Longitude == 0 || Ma.Latitude == 0) continue; PointShape point = new PointShape(Ma.Longitude, Ma.Latitude); ShapeValidationResult result = point.Validate(ShapeValidationMode.Simple); if (result.IsValid) { Marker marker = new Marker(point); marker.Image = MarkerImage; marker.Width = 24; marker.Height = 24; marker.YOffset = -6; marker.Click += new EventHandler(Marker_Click); marker.ToolTipText = Ma.Name; marker.Tag = Ma.Record_id + type.ToString(); marker.Visible = true; Markers.Add(marker); MarkerPoints.Add(point); } } } catch (SystemException ex) { bc.OKMessage(ex.Message); return false; } return true; } private void FillMarkerList(DataTable listTable, MarkerLayerType MarkerType) { foreach (DataRow Row in listTable.Rows) { MapAddress ma = new MapAddress(Row,0); ma.Parent_id = Row["parent_id"].ToString(); ma.Phone = Row["phone_number_1"].ToString(); try { ma.LastActionDate = Row["last_action_date"].ToString(); } catch { } if (MarkerType == MarkerLayerType.doctor) ma.Category = Row["Specialty"].ToString(); else if (MarkerType == MarkerLayerType.hospital) ma.Category = Row["Category"].ToString(); else if (MarkerType == MarkerLayerType.visit) ma.Category = Row["Objective"].ToString(); ma.Name = (MarkerType != MarkerLayerType.hospital) ? Row["Name"].ToString() : Row["Company_Name"].ToString(); ma.Type = MarkerType.ToString(); MarkerList.Add(ma); } } public class mwMapLayer : InMemoryFeatureLayer { public enum FeatureType { polygons, points, lines }; private FeatureType featureType = FeatureType.polygons; private string IdColumnName; private string Longitude; private string Latitude; private string WellKnownText; public DataTable MapTable; public bool ifFeaturesInitialized = true; public mwMapLayer(DataTable maptable, string idcolumnname, string longitude, string latitude, FeatureType featuretype) { IdColumnName = idcolumnname; Longitude = longitude; Latitude = latitude; featureType = featuretype; if (featureType != FeatureType.points) WellKnownText = longitude; MapTable = maptable; if (!SetupFeatures()) ifFeaturesInitialized = false; } public bool SetupFeatures() { try { this.FeatureSource.Open(); foreach (DataColumn Col in MapTable.Columns) { FeatureSourceColumn col = new FeatureSourceColumn(Col.ColumnName); this.Columns.Add(col); } this.FeatureSource.Close(); Feature feature; foreach (DataRow row in MapTable.Rows) { string wkt = ""; if (featureType == FeatureType.polygons) { wkt = row[WellKnownText].ToString().Trim(); if (bc.Left(wkt, 5).Trim() == "MULTI") { try { MultipolygonShape multi = new MultipolygonShape(wkt); multi.Id = bc.NameProper(row[IdColumnName].ToString(), false); feature = multi.GetFeature(); } catch { continue; } } else { try { PolygonShape polyShape = new PolygonShape(wkt); ShapeValidationResult result = polyShape.Validate(ShapeValidationMode.Simple); if (!result.IsValid) continue; polyShape.Id = bc.NameProper(row[IdColumnName].ToString(), false) + bc.Getkey(); feature = polyShape.GetFeature(); } catch { continue; } } } else if (featureType == FeatureType.lines) { wkt = row[WellKnownText].ToString().Trim(); try { LineShape lineShape = new LineShape(wkt); ShapeValidationResult result = lineShape.Validate(ShapeValidationMode.Simple); if (!result.IsValid) continue; lineShape.Id = bc.NameProper(row[IdColumnName].ToString(), false); feature = lineShape.GetFeature(); } catch { continue; } } else { try { PointShape pointShape = new PointShape(Convert.ToDouble(row[Longitude]), Convert.ToDouble(row[Latitude])); pointShape.Id = bc.NameProper(row[IdColumnName].ToString(), false); feature = pointShape.GetFeature(); } catch { continue; } } foreach (DataColumn Col in MapTable.Columns) { string Value = row[Col.ColumnName].ToString(); feature.ColumnValues.Add(Col.ColumnName, Value); } InternalFeatures.Add(feature); } } catch (SystemException ex) { bc.OKMessage(ex.Message); return false; } return true; } public void ClearMap() { try { if (MarkerLayer.Markers.Count > 0) { foreach (var item in MarkerLayer.Markers) { item.Visible = false; MapControl.Controls.Remove(item); item.Dispose(); } } MapControl.Overlays.Remove("MarkerOverlay"); MarkerLayer = new mwMarkerLayer(this); MapControl.Refresh(); } catch { }; try { MapControl.Overlays.Remove("DetailLayer"); MapControl.AdornmentOverlay.Layers.Remove("LegendLayer"); MapControl.Refresh(); } catch { }; }