MapWinGIS ActiveX Sample Code : MapWindow Discussion Forum
Dear friends, i put a sample code to export some features of a shapefile into another shapefile with all fields and cell values and load into a Map. The code use generics list collections to load the fields and shapes of the shapefile and to load into the second or exported shapefile
Sample Code to Export Shapes from a Shapefile into a second Shapefile
Posted by:
geofran80 ()
Date: July 31, 2008 02:54AM
Dear friends, i put a sample code to export some features of a shapefile into another shapefile with all fields and cell values and load into a Map.
The code use generics list collections to load the fields and shapes of the shapefile and to load into the second or exported shapefile.
If you want to expand the code is welcome.
Cheers.
Francisco J.
-------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------
Edited 4 time(s). Last edit at 07/31/2008 11:25AM by geofran80.
The code use generics list collections to load the fields and shapes of the shapefile and to load into the second or exported shapefile.
If you want to expand the code is welcome.
Cheers.
Francisco J.
-------------------------------------------------------------------------------------------------------------
Public Class Form1 Dim shp1 As New MapWinGIS.Shapefile 'First Shapefile that contains all Features Dim shp2 As New MapWinGIS.Shapefile 'Second Shapefile the exported with selected Features Dim handle1, handle2 As Integer 'Handles for the two shapefiles Dim mField As MapWinGIS.Field 'Variable for each field Dim mShape As MapWinGIS.Shape 'Variable for each feature or shape Dim FieldList As New List(Of MapWinGIS.Field) 'Generic List of Fields that will contains all fields Dim ShapeList As New List(Of MapWinGIS.Shape) 'Generic List of Shapes that will contains all shapes Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Link the Map and the Legend Legend1.Map = AxMap1.GetOcx 'Open the first Shapefile shp1.Open("c:\ShapefileOrigen.shp") 'Handle the first shapefile handle1 = Legend1.Layers.Add(shp1, True) 'Add the Filename of Shp1 withouth extension to the Legend AxMap1.set_LayerName(handle1, System.IO.Path.GetFileNameWithoutExtension(shp1.Filename)) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Loop all fields and add the properties to the fields For numeroCampos As Integer = 0 To shp1.NumFields - 1 mField = New MapWinGIS.Field 'Create a new field for each field in the first shapefile mField.Name = shp1.Field(numeroCampos).Name 'Save the Name mField.Precision = shp1.Field(numeroCampos).Precision 'Save the Precision mField.Type = shp1.Field(numeroCampos).Type 'Save the Type mField.Width = shp1.Field(numeroCampos).Width 'Save the Width FieldList.Add(mField) 'Add each mfield to the Generic List of Field Next 'Loop all shapes to load into mShape For i As Integer = 0 To shp1.NumShapes - 1 mShape = shp1.Shape(i) ShapeList.Add(mShape) 'Add each mshape to the Generic List of Shapes Next 'Create the Shapefile to save the exported features or shapes shp2.CreateNew("C:\PruebaExportacion4.shp", MapWinGIS.ShpfileType.SHP_POLYGON) 'Begin EditingShapes and Table shp2.StartEditingShapes(True) 'Loop each field into FieldList For Each campo As MapWinGIS.Field In FieldList shp2.EditInsertField(campo, shp2.NumFields) 'Insert all fields into Shp2 Next 'Loop all Shapes that you desire to load into Shp2 from the Shp1 '******************************************************************************************' 'Here you can evaluate an Attribute Selection or another thing to select the shapes to export '******************************************************************************************' For j As Integer = 0 To ShapeList.Count - 3 shp2.EditInsertShape(ShapeList.Item(j), shp2.NumShapes) 'Insert all shapes into Shp2 Next 'Loop all values of cells to load into Shp2 from Shp1 For value As Integer = 0 To shp1.NumShapes - 1 For field As Integer = 0 To shp1.NumFields - 1 shp2.EditCellValue(field, value, shp1.CellValue(field, value)) Next Next 'Stop editing the Shp1 and save the table shp2.StopEditingShapes(True, True) 'Load the Shp2 into the Legend-Map handle2 = Legend1.Layers.Add(shp2, True) 'Add the Filename of Shp2 withouth extension to the Legend AxMap1.set_LayerName(handle2, System.IO.Path.GetFileNameWithoutExtension(shp2.Filename)) End Sub End Class
-------------------------------------------------------------------------------------------------------------
Edited 4 time(s). Last edit at 07/31/2008 11:25AM by geofran80.
Re: Sample Code to Export Shapes from a Shapefile into a second Shapefile
Posted by:
geofran80 ()
Date: July 31, 2008 03:03AM
I forgot to save the shapefile,
If you need to save the shapefile, only add to the finish of the button click event the following code:
shp2.save("Filename")
Although the stopeditingshapes save changes.
Cheers.
Francisco J.
If you need to save the shapefile, only add to the finish of the button click event the following code:
shp2.save("Filename")
Although the stopeditingshapes save changes.
Cheers.
Francisco J.
Re: Sample Code to Export Shapes from a Shapefile into a second Shapefile
Posted by:
geofran80 ()
Date: July 31, 2008 10:57AM
Sorry I put the code in this forum, if the administrator think that it must be in the Mapwindow Sample Code, change it without problem.
Thanks.
Cheers.
Francisco J.
Thanks.
Cheers.
Francisco J.
Re: Sample Code to Export Shapes from a Shapefile into a second Shapefile
Posted by:
danames ()
Date: July 31, 2008 04:30PM
Francisco - thanks for pointing out that this needed to be moved. I was happy to do it. By the way, thank you to you and others who post code and answer people's questions on this forum. I think we've got one of the more helpful active forums of any open source GIS project! - Dan
Re: Sample Code to Export Shapes from a Shapefile into a second Shapefile
Posted by:
geofran80 ()
Date: July 31, 2008 06:09PM
Yes Dan, don't worry, i'm very pleasure to help and post in this phorum.
Every day I enjoy more using mapwingis and .NET combined.
Thanks a lot of to you and another administrator, users, and so.
Cheers.
Best regards.
Francisco J.
Every day I enjoy more using mapwingis and .NET combined.
Thanks a lot of to you and another administrator, users, and so.
Cheers.
Best regards.
Francisco J.
Re: Sample Code to Export Shapes from a Shapefile into a second Shapefile
Posted by:
spider ()
Date: April 22, 2009 11:15AM
So Cool!
Thanks.
Thanks.
Sorry, only registered users may post in this forum.


