private void tsmiEditRegionAddRoadsAndPOI_Click(object sender, EventArgs e) { ShapeFileERMTType shapeFileERMTType = ((ToolStripMenuItem)sender).Name == "tsmiEditRegionAddRoads" ? ShapeFileERMTType.Path : ShapeFileERMTType.POI; OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Shape Files (*.shp)|*.shp" }; DialogResult result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { FileInfo shapeFileInfo = new FileInfo(openFileDialog.FileName); try { if (!File.Exists(shapeFileInfo.ToString().Replace(".shp", ".dbf")) || !File.Exists(shapeFileInfo.ToString().Replace(".shp", ".shx"))) { //if there's no DBF / SHX file, can't use this .shp. //http://thinkgeo.com/forums/MapSuite/tabid/143/aft/2947/Default.aspx //1) In real life, how those files (.shp .shx .dbf) are generated ? //1. These files are automatically created upon creation of a shapefile, using the CreateShapeFile method of Map Suite or another shapefile creation application. These three files are required for a shapefile to function as dictated by ESRI shapefile standard. CustomMessageBox.ShowMessage(ResourceHelper.GetResourceText("EditRegionShapeFileMissingFiles")); return; } else { Region selectedRegion = (Region)_selectedNode.Tag; if (selectedRegion != null) { String newRegionFileName = SaveShapefiles(shapeFileInfo, selectedRegion, shapeFileERMTType); switch (shapeFileERMTType) { case ShapeFileERMTType.Path: { selectedRegion.PathFileName = newRegionFileName; break; } case ShapeFileERMTType.POI: { selectedRegion.POIFileName = newRegionFileName; break; } } RegionHelper.Save(selectedRegion); CustomMessageBox.ShowMessage(shapeFileERMTType == ShapeFileERMTType.Path ? ResourceHelper.GetResourceText("PathSuccessfullySaved") : ResourceHelper.GetResourceText("POISuccessfullySaved")); } } } catch (Exception ex) { //Shape was not valid } finally { } } }
private void tsmiEditRegionAddChildRegion_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Shape Files (*.shp)|*.shp" }; DialogResult result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { FileInfo shapeFileInfo = new FileInfo(openFileDialog.FileName); try { int count = (LayerOverlay)winformsMap1.Overlays[0] != null ? ((LayerOverlay)winformsMap1.Overlays[0]).Layers.Count : 0; if (!File.Exists(shapeFileInfo.ToString().Replace(".shp", ".dbf")) || !File.Exists(shapeFileInfo.ToString().Replace(".shp", ".shx"))) { //if there's no DBF / SHX file, can't use this .shp. //http://thinkgeo.com/forums/MapSuite/tabid/143/aft/2947/Default.aspx //1) In real life, how those files (.shp .shx .dbf) are generated ? //1. These files are automatically created upon creation of a shapefile, using the CreateShapeFile method of Map Suite or another shapefile creation application. These three files are required for a shapefile to function as dictated by ESRI shapefile standard. CustomMessageBox.ShowMessage(ResourceHelper.GetResourceText("EditRegionShapeFileMissingFiles")); } else { ShapeFileFeatureLayer newRegion = MapHelper.GetRegionFeatureLayer(shapeFileInfo); DataTable table = GetDataTable(newRegion); PickColumnForm pck = new PickColumnForm(table); if (pck.ShowDialog() == DialogResult.OK) { LoadingForm.ShowLoading(); _columnName = pck.SelectedColumn; string parentColumnName = pck.SelectedParentColumn; try { int regionCount = 0; List <Region> siblingRegions = null; String newRegionFileName = string.Empty; Region parent = null; foreach (DataRow row in table.Rows) { Boolean saveRegion = false; Region region = NewRegionData; region.RegionName = row[_columnName].ToString(); if (parentColumnName != "No parent column") //If la segunda columna esta seleccionada parentNameColumnIndex!=-1 { //Quien es el pais? int idCountry = GetCountry(RegionHelper.Get(region.IDParent.Value)); if (region.IDParent != null) { Region parentRegion = RegionHelper.Get(region.IDParent.Value); if (parentRegion.IDParent != null) { if (idCountry == _regionData.IDRegion) { region.IDParent = _regionData.IDRegion; saveRegion = true; } else { if (siblingRegions == null) { siblingRegions = RegionHelper.GetChildsAtLevel(idCountry, _regionData.RegionLevel); } foreach (Region r in siblingRegions) //Hermanos { if (r.RegionName != (string)row[parentColumnName]) { continue; } region.IDParent = r.IDRegion; saveRegion = true; break; } } } else { region.IDParent = parentRegion.IDRegion; } } } else { saveRegion = true; } parent = RegionHelper.Get(region.IDParent.Value); if (saveRegion) { region.RegionLevel = parent.RegionLevel + 1; if (newRegionFileName == string.Empty) { newRegionFileName = SaveShapefiles(shapeFileInfo, region); } region.ShapeFileName = newRegionFileName; if (table.Rows.Count == 1) { //it's the only region in the shapefile. So, no need to specify an index region.ShapeFileIndex = null; } else { //it's a shapefile that contains more than 1 regions. Index is needed. region.ShapeFileIndex = regionCount; } RegionHelper.Save(region); regionCount++; } } //Reload map and tree LoadRegions(); if (parent != null) { ShowShapeChilds(parent); } LoadingForm.Fadeout(); CustomMessageBox.ShowMessage(regionCount + " " + ResourceHelper.GetResourceText("NewRegionsHaveBeenImported")); } catch (Exception ex) { LogHelper.LogError(ex); throw; } finally { LoadingForm.Fadeout(); } } } } catch (Exception ex) { //Shape was not valid } finally { } } }