User Tools

Site Tools


source_code_serviceseditionsample_splitpolygonsbasedongrid_cs_120321.zip

Source Code ServicesEditionSample SplitPolygonsBasedOnGrid CS 120321.zip

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace SplitPolygonsBasedOnGrid  
 {  
     static class Program  
     {  
         /// <summary>  
         /// The main entry point for the application.  
         /// </summary>  
         [STAThread]  
         static void Main()  
         {  
             Application.EnableVisualStyles();  
             Application.SetCompatibleTextRenderingDefault(false);  
             Application.Run(new frmSplitPolygons());  
         }  
     }  
 }  
 

SplitPolygons.cs

 using System;  
 using System.Collections.ObjectModel;  
 using System.IO;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace SplitPolygonsBasedOnGrid  
 {  
     public partial class frmSplitPolygons : Form  
     {  
         public frmSplitPolygons()  
         {  
             InitializeComponent();  
         }  
 
         private void btnProcess_Click(object sender, EventArgs e)  
         {  
             MapSuiteTileMatrix matrix = new MapSuiteTileMatrix(Int32.Parse(txtTargetScale.Text), 256, 256, GeographyUnit.DecimalDegree);  
 
             string[] filesToDelete = Directory.GetFiles(txtSourceDirectory.Text, "*SplitTemp.*");  
 
             foreach (string file in filesToDelete)  
             {  
                 File.Delete(file);  
             }  
 
             string[] files = Directory.GetFiles(txtSourceDirectory.Text, "*.shp");  
 
             foreach (string file in files)  
             {  
                 if (!file.Contains("_split.shp"))  
                 {  
                     ShapeFileFeatureSource.BuildIndexFile(file, BuildIndexMode.DoNotRebuild);  
                     ShapeFileFeatureSource shapeFile = new ShapeFileFeatureSource(file, ShapeFileReadWriteMode.ReadOnly);  
                     shapeFile.Open();  
                     if (shapeFile.GetShapeFileType() == ShapeFileType.Polygon)  
                     {  
 
                         ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Polygon, file.Replace(".shp", "_AreaSplitTemp.shp"), new Collection<DbfColumn>() { new DbfColumn("RECID", DbfColumnType.Integer, 10, 0) });  
 
 
 
                         ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Polyline, file.Replace(".shp", "_LineSplitTemp.shp"), new Collection<DbfColumn>() { new DbfColumn("RECID", DbfColumnType.Integer, 10, 0) });  
 
 
                         ShapeFileFeatureSource targetAreaShapeFile = new ShapeFileFeatureSource(file.Replace(".shp", "_AreaSplitTemp.shp"), ShapeFileReadWriteMode.ReadWrite);  
                         targetAreaShapeFile.Open();  
                         targetAreaShapeFile.BeginTransaction();  
 
                         ShapeFileFeatureSource targetLineShapeFile = new ShapeFileFeatureSource(file.Replace(".shp", "_LineSplitTemp.shp"), ShapeFileReadWriteMode.ReadWrite);  
                         targetLineShapeFile.Open();  
                         targetLineShapeFile.BeginTransaction();  
 
 
                         RowColumnRange rowColumnRange = matrix.GetIntersectingRowColumnRange(shapeFile.GetBoundingBox());  
 
                         for (int x = 0; x < rowColumnRange.MaxRowIndex - rowColumnRange.MinRowIndex + 1; x++)  
                         {  
                             for (int y = 0; y < rowColumnRange.MaxColumnIndex - rowColumnRange.MinColumnIndex + 1; y++)  
                             {  
                                 TileMatrixCell cell = matrix.GetCell(x + rowColumnRange.MinRowIndex, y + rowColumnRange.MinColumnIndex);  
 
                                 Collection<Feature> fearures = shapeFile.GetFeaturesInsideBoundingBox(cell.BoundingBox, new Collection<string>() { "RECID" });  
 
                                 foreach (Feature feature in fearures)  
                                 {  
                                     BaseShape areaIntersection = null;  
                                     BaseShape lineIntersection = null;  
                                     MultipolygonShape polygon = (MultipolygonShape)feature.GetShape();  
 
                                     areaIntersection = polygon.GetIntersection(cell.BoundingBox);  
                                     MultilineShape multiLine = new MultilineShape();  
 
                                     foreach (PolygonShape singlePolygon in polygon.Polygons)  
                                     {  
                                         multiLine.Lines.Add(new LineShape(singlePolygon.OuterRing.Vertices));  
 
                                         foreach (RingShape ring in singlePolygon.InnerRings)  
                                         {  
                                             multiLine.Lines.Add(new LineShape(ring.Vertices));  
                                         }  
                                     }  
 
                                     lineIntersection = multiLine.GetIntersection(cell.BoundingBox);  
 
                                     if (lineIntersection is MultilineShape)  
                                     {  
                                         if (((MultilineShape)lineIntersection).Lines.Count == 0)  
                                         {  
                                             lineIntersection = null;  
                                         }  
                                     }  
 
                                     if (areaIntersection != null)  
                                     {  
                                         Feature newFeature = new Feature(areaIntersection);  
                                         newFeature.ColumnValues.Add("RECID", feature.ColumnValues["RECID"].ToString());  
                                         targetAreaShapeFile.AddFeature(newFeature);  
                                     }  
                                     if (lineIntersection != null)  
                                     {  
                                         Feature newFeature = new Feature(lineIntersection);  
                                         newFeature.ColumnValues.Add("RECID", feature.ColumnValues["RECID"].ToString());  
                                         targetLineShapeFile.AddFeature(newFeature);  
                                     }  
 
                                     txtStatus.Text = string.Format("Row {0} of {1}, Column {2} of {3} in {4}", x, rowColumnRange.MaxRowIndex - rowColumnRange.MinRowIndex + 1, y, rowColumnRange.MaxColumnIndex - rowColumnRange.MinColumnIndex + 1, Path.GetFileName(shapeFile.ShapePathFileName));  
                                     Application.DoEvents();  
                                 }  
                             }  
                         }  
                         targetLineShapeFile.CommitTransaction();  
                         targetAreaShapeFile.CommitTransaction();  
 
                         targetAreaShapeFile.Close();  
                         targetLineShapeFile.Close();  
 
                         RenameTempFiles(file.Replace(".shp", "_AreaSplitTemp.shp"));  
                         RenameTempFiles(file.Replace(".shp", "_LineSplitTemp.shp"));  
 
                     }  
                     shapeFile.Close();  
                 }  
 
             }  
             MessageBox.Show("Finished!");  
             txtStatus.Text = "Finished!";  
         }  
 
         private void RenameTempFiles(string shapePathFileName )  
         {  
             File.Move(shapePathFileName, shapePathFileName.Replace("SplitTemp", "Split"));  
             File.Move(shapePathFileName.Replace(".shp", ".dbf"), shapePathFileName.Replace(".shp", ".dbf").Replace("SplitTemp", "Split"));  
             File.Move(shapePathFileName.Replace(".shp", ".shx"), shapePathFileName.Replace(".shp", ".shx").Replace("SplitTemp", "Split"));  
             File.Move(shapePathFileName.Replace(".shp", ".idx"), shapePathFileName.Replace(".shp", ".idx").Replace("SplitTemp", "Split"));  
             File.Move(shapePathFileName.Replace(".shp", ".ids"), shapePathFileName.Replace(".shp", ".ids").Replace("SplitTemp", "Split"));  
         }  
 
         private void btnCancel_Click(object sender, EventArgs e)  
         {  
             this.Close();  
         }  
 
         private void btnLookupSource_Click(object sender, EventArgs e)  
         {  
             FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();  
             folderBrowserDialog.ShowNewFolderButton = false;  
             folderBrowserDialog.ShowDialog();  
             txtSourceDirectory.Text = folderBrowserDialog.SelectedPath;  
         }  
     }  
 }  
 
source_code_serviceseditionsample_splitpolygonsbasedongrid_cs_120321.zip.txt · Last modified: 2015/09/08 05:37 by admin