MapWindow 4 - ActiveX Control Programming : MapWindow Discussion Forum
This code woulb be added to the CreateShapefile method and the result would be this:
Hope it serves you.
Francisco J.
Edited 1 time(s). Last edit at 08/23/2009 07:26AM by geofran80.
Attachments: error1.gif (78 KB)
I have a table like this: pointName lat lon type A N280234 E1132345 triangle B N234512 E1122532 circle C N254321 E1262134 pentagon ..... I want make a shapefile from the table and draw a map ,that's draw
how to make shapefile from a table?
Posted by:
zsanhong ()
Date: August 21, 2009 04:26AM
I have a table like this:
pointName lat lon type
A N280234 E1132345 triangle
B N234512 E1122532 circle
C N254321 E1262134 pentagon
.....
I want make a shapefile from the table and draw a map ,that's
draw a "triange",label "A" in postion(N280234 E1132345)
draw a "circle",lable "B" in postion(N234512 E1122532)
draw a "pentagon",lable "C" in postion(N254321 E1262134)
how can I do this? thank you very much.
(sorry,my poor English.)
pointName lat lon type
A N280234 E1132345 triangle
B N234512 E1122532 circle
C N254321 E1262134 pentagon
.....
I want make a shapefile from the table and draw a map ,that's
draw a "triange",label "A" in postion(N280234 E1132345)
draw a "circle",lable "B" in postion(N234512 E1122532)
draw a "pentagon",lable "C" in postion(N254321 E1262134)
how can I do this? thank you very much.
(sorry,my poor English.)
Re: how to make shapefile from a table?
Posted by:
zsanhong ()
Date: August 21, 2009 04:26AM
anyone help me?
Re: how to make shapefile from a table?
Posted by:
sindizzy ()
Date: August 22, 2009 08:47AM
yes here what you should do
create a shapefile
iterate through your list
create shapes
insert shapes into shapefile
close shapefile
add shapefile to map. display shapes depending on attributes.
you must get familiar with the framework before you do anything. look at the documentation wiki, review the examples on the website, then post the code that you draft.
AGP
create a shapefile
iterate through your list
create shapes
insert shapes into shapefile
close shapefile
add shapefile to map. display shapes depending on attributes.
you must get familiar with the framework before you do anything. look at the documentation wiki, review the examples on the website, then post the code that you draft.
AGP
Re: how to make shapefile from a table?
Posted by:
zsanhong ()
Date: August 23, 2009 05:39AM
thanks!my code is like this:
Edited 4 time(s). Last edit at 08/23/2009 05:59AM by zsanhong.
int Layer = 0;
MapWinGIS.Shapefile sf=new MapWinGIS.Shapefile ();
if (Layer > -1)
{
axMap1.RemoveLayer(Layer);
Layer = -1;
sf.Close();
}
else
{
sf = new MapWinGIS.Shapefile();
}
string shpfileName = Application.StartupPath + @"\myPoint.shp";
string shxfileName = Application.StartupPath + @"\myPoint.shx";
string dbffileName = Application.StartupPath + @"\myPoint.dbf";
if (File.Exists(dbffileName))
{
File.Delete(shpfileName);
File.Delete(shxfileName);
File.Delete(dbffileName);
}
// string fileName=Application.StartupPath+@"\myPoint.shp";
if (!(Result = sf.CreateNew(shpfileName, MapWinGIS.ShpfileType.SHP_POINT)))
{
ShowErrowMsg (sf);
}
int fldIndex = 0;
MapWinGIS.Field f = new MapWinGIS.Field();
f.Name = "PointName";
f.Type = MapWinGIS.FieldType.STRING_FIELD;
f.Width = 10;
if (!(Result = sf.EditInsertField(f, ref fldIndex, null)))
{
ShowErrowMsg(sf);
}
fldIndex += 1;
f = new MapWinGIS.Field();
f.Name = "PointType";
f.Type = MapWinGIS.FieldType.STRING_FIELD;
f.Width = 10;
if (!(Result = sf.EditInsertField(f, ref fldIndex, null)))
{
ShowErrowMsg(sf);
}
fldIndex += 1;
f = new MapWinGIS.Field();
f.Name = "longitude";
f.Type = MapWinGIS.FieldType.DOUBLE_FIELD ;
f.Precision = 14;
if (!(Result = sf.EditInsertField(f, ref fldIndex, null)))
{
ShowErrowMsg(sf);
}
fldIndex += 1;
f = new MapWinGIS.Field();
f.Name = "latitude";
f.Type = MapWinGIS.FieldType.DOUBLE_FIELD;
f.Precision = 14;
if (!(Result = sf.EditInsertField(f, ref fldIndex, null)))
{
ShowErrowMsg(sf);
}
fldIndex += 1;
MapWinGIS.Shape shape1 = new MapWinGIS.Shape();
shape1.ShapeType = MapWinGIS.ShpfileType.SHP_POINT;
MapWinGIS.Point pt = new MapWinGIS.Point();
pt.x = 75.0;
pt.y = 23.0;
shape1.InsertPoint(pt, ref pointIndex);
sf.EditInsertShape(shape1, ref shapeIndex);
sf.EditCellValue(1, shapeIndex, "PointA");
sf.EditCellValue(2, shapeIndex, "Diamond");
shape1 = new MapWinGIS.Shape();
shape1.ShapeType = MapWinGIS.ShpfileType.SHP_POINT;
pt = new MapWinGIS.Point();
pt.x = 22.0;
pt.y = 25.0;
pointIndex += 1;
shape1.InsertPoint(pt, ref pointIndex);
sf.EditInsertShape(shape1, ref shapeIndex);
sf.EditCellValue(1, shapeIndex, "PointB");
sf.EditCellValue(2, shapeIndex, "Triangle");
shape1 = new MapWinGIS.Shape();
shape1.ShapeType = MapWinGIS.ShpfileType.SHP_POINT;
pt = new MapWinGIS.Point();
pt.x = 32.0;
pt.y = 35.0;
pointIndex += 1;
shape1.InsertPoint(pt, ref pointIndex);
sf.EditInsertShape(shape1, ref shapeIndex);
sf.EditCellValue(1, shapeIndex, "PointC");
sf.EditCellValue(2, shapeIndex, "pentagon");
Result = sf.StopEditingShapes(true, true, null);
Layer =axMap1.AddLayer (sf,true);
for (int j = 0; j < sf.NumShapes; j++)
{
string PointType = System.Convert.ToString(sf.get_CellValue(2, j));
if (PointType == "Diamond")
{
axMap1.set_ShapePointType(Layer, shapeIndex, MapWinGIS.tkPointType.ptDiamond);
}
if (PointType == "Triangle")
{
axMap1.set_ShapePointType(Layer, shapeIndex, MapWinGIS.tkPointType.ptTriangleUp);
}
if (PointType == "pentagon")
{
axMap1.set_ShapePointType(Layer, shapeIndex, MapWinGIS.tkPointType.ptUserDefined);
}
}
I don't know how to draw a "ptUserDefined",like pentagon.Edited 4 time(s). Last edit at 08/23/2009 05:59AM by zsanhong.
Re: how to make shapefile from a table?
Posted by:
geofran80 ()
Date: August 23, 2009 05:59AM
Friend using filestreams and streamreaders you can follow all lines of your file and convert then into one datatable net class.
Later you can add every line as a shape into your shapefile as you comment up.
I use the following code for convert one txt delimited file by ";" format as follow into one DataTable, later you can convert into shapefile.
Format File Sample:
-------------------
FieldID;FieldDesc;XCoord;YCoord
1;Item1;336944,98;4074692,712
2;Item2;378559,11;4105652,017
3;Item3;327295,32;4101832,363
4;Item4;373332,21;4064037,887
Sample Code TxtDelimited2DataTable:
----------------------------------
Main form code
________________________________________________________________________________
DlgTXT2Table:
-------------
Hope it serves you.
Francisco J.
Later you can add every line as a shape into your shapefile as you comment up.
I use the following code for convert one txt delimited file by ";" format as follow into one DataTable, later you can convert into shapefile.
Format File Sample:
-------------------
FieldID;FieldDesc;XCoord;YCoord
1;Item1;336944,98;4074692,712
2;Item2;378559,11;4105652,017
3;Item3;327295,32;4101832,363
4;Item4;373332,21;4064037,887
Sample Code TxtDelimited2DataTable:
----------------------------------
Main form code
Private fileToConvert As String Public Property File2Convert() As String Get Return fileToConvert End Get Set(ByVal value As String) fileToConvert = value End Set End Property Private _delimiter As String Public Property Delimiter() As String Get Return _delimiter End Get Set(ByVal value As String) _delimiter = value End Set End Property Private myshp As String Public Property MyShapefile() As String Get Return myshp End Get Set(ByVal value As String) myshp = value End Set End Property Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Legend1.Map = AxMap1.GetOcx End Sub 'Function to recreate the Table of Attributes of the File Public Function DataToTable(ByVal fileName As String, ByVal delimiter As String) As DataTable Dim dt As New DataTable("Data") Dim headerData As String Dim data As String() Dim fieldsInfo() As String data = File.ReadAllLines(fileName) headerData = data(0) fieldsInfo = headerData.Split(delimiter) For Each item In fieldsInfo dt.Columns.Add(New DataColumn(item)) Next For i As Integer = 1 To data.Count - 1 Dim row As DataRow = dt.NewRow row.ItemArray = data(i).Split(delimiter) dt.Rows.Add(row) Next Return dt End Function Private Sub OpenTableToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenTableToolStripMenuItem.Click Dim dlg As New dlgTxt2Table If dlg.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub Dim dt As DataTable = DataToTable(File2Convert, Delimiter) DataGridView1.DataSource = dt End Sub
________________________________________________________________________________
DlgTXT2Table:
-------------
Imports System.Windows.Forms Public Class dlgTxt2Table Private ofd As OpenFileDialog Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click Me.DialogResult = System.Windows.Forms.DialogResult.OK Form1.File2Convert = ofd.FileName Form1.Delimiter = CStr(ComboBox1.SelectedItem) Me.Close() End Sub Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click Me.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.Close() End Sub Private Sub btnSelectfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectfile.Click ofd = New OpenFileDialog If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub TextBox1.Text = ofd.FileName End Sub Private Sub dlgTxt2Table_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class
Hope it serves you.
Francisco J.
Re: how to make shapefile from a table?
Posted by:
geofran80 ()
Date: August 23, 2009 07:03AM
Friend, here you have one sample code and solution:
The step for use the sample application is:
Step1. You have this environment.
Step2. Select the txt or csv file from your directory using the File > Open Table menu item.
Step3 - Step4. Select the file and the separator (in my case i use ";" separator) you can improve the sample with custom separators having account the coordinate decimal separator.
Step5. You can see into your DataGridView added to the sample app the features or coordinate items.
Step6. Open the Conversion menu and navigate to Table To Shapefile menu item.
Step7. Here you need to select the columns (by default using strings.contains method I detected the X and Y coordinates) but if you use another xy coordinate field's name you can custom it. Later you select the shapefile name that you need to save it.
Step8. You can see the result in your AxMap object with custom size and colors of your points or features.
This is the sample app code:
[www.megaupload.com]
Hope it serves you.
Francisco J.
The step for use the sample application is:
Step1. You have this environment.
Step2. Select the txt or csv file from your directory using the File > Open Table menu item.
Step3 - Step4. Select the file and the separator (in my case i use ";" separator) you can improve the sample with custom separators having account the coordinate decimal separator.
Step5. You can see into your DataGridView added to the sample app the features or coordinate items.
Step6. Open the Conversion menu and navigate to Table To Shapefile menu item.
Step7. Here you need to select the columns (by default using strings.contains method I detected the X and Y coordinates) but if you use another xy coordinate field's name you can custom it. Later you select the shapefile name that you need to save it.
Step8. You can see the result in your AxMap object with custom size and colors of your points or features.
This is the sample app code:
[www.megaupload.com]
Hope it serves you.
Francisco J.
Re: how to make shapefile from a table?
Posted by:
geofran80 ()
Date: August 23, 2009 07:21AM
'If you want to try if your coordinates is accord with your table you can label it Form1.AxMap1.MultilineLabels = True For shp As Integer = 0 To myShp.NumShapes - 1 Form1.AxMap1.AddLabelEx(hndshp, "Item " & shp + 1 & vbCrLf & myShp.Shape(shp).Point(0).x & "-" & myShp.Shape(shp).Point(0).y, Convert.ToUInt32(RGB(0, 0, 0)), _ myShp.Shape(shp).Point(0).x, myShp.Shape(shp).Point(0).y, MapWinGIS.tkHJustification.hjCenter, 0.0) Next Form1.AxMap1.set_LayerLabelsShadow(hndshp, True) Form1.AxMap1.set_LayerLabelsShadowColor(hndshp, Convert.ToUInt32(RGB(255, 255, 0)))
This code woulb be added to the CreateShapefile method and the result would be this:
Hope it serves you.
Francisco J.
Edited 1 time(s). Last edit at 08/23/2009 07:26AM by geofran80.
Re: how to make shapefile from a table?
Posted by:
SandraMF ()
Date: November 14, 2009 01:48PM
Hi Francisco,
I am new to new to programming (using Visual Basic Express 2008) and only last week encountered MapWindow, so please bare with me if my questions are a bit "daft" :) Firstly, thank you for a few useful piece of code (http://www.megaupload.com/?d=JA8QR7FV) it has already taught me a lot. My question is, how do you add the text file contents (from the datagridview) to the .dbf table of the shapefile? I managed to add the column headings using the following code:
But I can't seem to get the rest of the table across :( I would appreciate any help you could give me.
Thank you in advance
Sandra
I am new to new to programming (using Visual Basic Express 2008) and only last week encountered MapWindow, so please bare with me if my questions are a bit "daft" :) Firstly, thank you for a few useful piece of code (http://www.megaupload.com/?d=JA8QR7FV) it has already taught me a lot. My question is, how do you add the text file contents (from the datagridview) to the .dbf table of the shapefile? I managed to add the column headings using the following code:
For j As Integer = 0 To Form1.DataGridView1.ColumnCount - 1
'Add the fields
Dim newFld As New MapWinGIS.Field
newFld.Key = Form1.DataGridView1.Columns(j).HeaderText
newFld.Name = Form1.DataGridView1.Columns(j).HeaderText
newFld.Width = 50
newFld.Type = MapWinGIS.FieldType.STRING_FIELD
fldIdx = Form1.DataGridView1.ColumnCount
myShapefile.EditInsertField(newFld, fldIdx)
But I can't seem to get the rest of the table across :( I would appreciate any help you could give me.
Thank you in advance
Sandra
Re: how to make shapefile from a table?
Posted by:
pmeems ()
Date: November 16, 2009 02:30AM
Sandra,
After you've setup your shapefile you add the shapes (points in this example), when you've added the shape using
Shape.Create
Shape.InsertPoint
Shapefile.EditInsertShape
You can add the attribute values using
Shapefile.EditCellValue
Hope it helps.
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
After you've setup your shapefile you add the shapes (points in this example), when you've added the shape using
Shape.Create
Shape.InsertPoint
Shapefile.EditInsertShape
You can add the attribute values using
Shapefile.EditCellValue
Hope it helps.
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
Re: how to make shapefile from a table?
Posted by:
SandraMF ()
Date: November 16, 2009 03:10AM
Hi Paul,
Thank you for your response. I am able to create the points and shapes successfully from opendialog .txt files (see example below) using code (see below) I adapted from the posts above but am unable to get the other .txt records to attach to the shapefiles'.dbf at the same time. I tried using EditCellValue() but couldn't figure out how to iterate through the text file and assign all rows as shapefile cells values without knowing the structure and contents of the .txt first. I may well be missing something obvious but I would appreciate your advice.
Text File example (however, structure will be dynamic i.e. opendialog):
Name,XLong,YLat
Berg-en-Dal,31.45122,-25.42001
Crocodile Bridge,31.89236,-25.35843
Letaba,31.57673,-23.85102
Lower Sabie,31.91727,-25.12106
Malelane,31.50890,-25.47235
Thank you
Sandra
Thank you for your response. I am able to create the points and shapes successfully from opendialog .txt files (see example below) using code (see below) I adapted from the posts above but am unable to get the other .txt records to attach to the shapefiles'.dbf at the same time. I tried using EditCellValue() but couldn't figure out how to iterate through the text file and assign all rows as shapefile cells values without knowing the structure and contents of the .txt first. I may well be missing something obvious but I would appreciate your advice.
Text File example (however, structure will be dynamic i.e. opendialog):
Name,XLong,YLat
Berg-en-Dal,31.45122,-25.42001
Crocodile Bridge,31.89236,-25.35843
Letaba,31.57673,-23.85102
Lower Sabie,31.91727,-25.12106
Malelane,31.50890,-25.47235
Private Sub CreateShapefile(ByVal fieldXCoord As Integer, ByVal fieldYCoord As Integer)
Dim myShapefile As New MapWinGIS.Shapefile
myShapefile.CreateNew(TextBox1.Text, MapWinGIS.ShpfileType.SHP_POINT)
myShapefile.StartEditingShapes(True)
Dim shapeindex As Long = 0
Dim pointindex As Long = 0
Dim success As Boolean
Try
For Each r As DataGridViewRow In frmDataGrid.DataGridView.Rows
Dim point As New MapWinGIS.Point
Dim shape As New MapWinGIS.Shape
success = shape.Create(MapWinGIS.ShpfileType.SHP_POINT)
point.x = CDbl(r.Cells(fieldXCoord).Value)
point.y = CDbl(r.Cells(fieldYCoord).Value)
shape.InsertPoint(point, pointindex)
myShapefile.EditInsertShape(shape, shapeindex)
shapeindex += 1
Next
success = myShapefile.StopEditingShapes(True)
myShapefile.SaveAs(myShapefile.Filename)
myShapefile.Open(myShapefile.Filename)
Dim handle As Integer = frmMain.mwLegend.Layers.Add(myShapefile, True)
frmMain.mwLegend.Map.LayerName(handle) = System.IO.Path.GetFileNameWithoutExtension(myShapefile.Filename)
frmMain.mwAxMap.ZoomToLayer(handle)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Thank you
Sandra
Re: how to make shapefile from a table?
Posted by:
pmeems ()
Date: November 17, 2009 12:11AM
Sandra,
In your frmDataGrid.DataGridView.Rows loop you should add a frmDataGrid.DataGridView.Columns loop and for each columns you add a value.
But you first need to set-up your shapefile to hold those columns.
So after myShapefile.CreateNew you should also loop through your columns and add every column as a field to the shapefile using:
MapWinGIS:Field
Shapefile_EditInsertField
Hope it helps.
If I got some time I'll create a test script for you.
--
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
In your frmDataGrid.DataGridView.Rows loop you should add a frmDataGrid.DataGridView.Columns loop and for each columns you add a value.
But you first need to set-up your shapefile to hold those columns.
So after myShapefile.CreateNew you should also loop through your columns and add every column as a field to the shapefile using:
MapWinGIS:Field
Shapefile_EditInsertField
Hope it helps.
If I got some time I'll create a test script for you.
--
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
Re: how to make shapefile from a table?
Posted by:
SandraMF ()
Date: November 17, 2009 09:19AM
Hi Paul,
Thank you once again for your direction, I really appreciate it.
I have managed to get the headers imported but still no records :( Everything else is working fine (open text file, read into datagridview, save as shapefile, display in map and legend.......) but each time I think "this" version of my code is going to work, I open the .dbf and ...... only find headers. What am I doing wrong? I'm sure it must be something stupid but I've tried so many different versions of myShapefile.EditCellValue() I'm seeing cross-eyed. I'm guessing it's the value part of EditCellValue(column, row, value) that I'm getting wrong (see red text in my code below.
Edited 1 time(s). Last edit at 11/17/2009 09:21AM by SandraMF.
Thank you once again for your direction, I really appreciate it.
I have managed to get the headers imported but still no records :( Everything else is working fine (open text file, read into datagridview, save as shapefile, display in map and legend.......) but each time I think "this" version of my code is going to work, I open the .dbf and ...... only find headers. What am I doing wrong? I'm sure it must be something stupid but I've tried so many different versions of myShapefile.EditCellValue() I'm seeing cross-eyed. I'm guessing it's the value part of EditCellValue(column, row, value) that I'm getting wrong (see red text in my code below.
'Create shapefile from datagridview table Dim myShapefile As New MapWinGIS.Shapefile Dim success As Boolean myShapefile.CreateNew(sfd.FileName, MapWinGIS.ShpfileType.SHP_POINT) myShapefile.StartEditingShapes(False) 'Add the fields to .dbf file For j As Integer = 0 To frmDataGrid.DataGridView.ColumnCount - 1 Dim newFld As New MapWinGIS.Field Dim fldIdx As Integer newFld.Key = frmDataGrid.DataGridView.Columns(j).HeaderText newFld.Name = frmDataGrid.DataGridView.Columns(j).HeaderText newFld.Width = 25 newFld.Type = MapWinGIS.FieldType.STRING_FIELD fldIdx = frmDataGrid.DataGridView.ColumnCount myShapefile.EditInsertField(newFld, fldIdx) Next 'Add points as x, y shapes to shapefile For Each r As DataGridViewRow In frmDataGrid.DataGridView.Rows Dim shapeindex As Long = 0 Dim pointindex As Long = 0 Dim point As New MapWinGIS.Point Dim shape As New MapWinGIS.Shape success = shape.Create(MapWinGIS.ShpfileType.SHP_POINT) point.x = CDbl(r.Cells(cboXCoord.SelectedItem).Value) point.y = CDbl(r.Cells(cboYCoord.SelectedItem).Value) shape.InsertPoint(point, pointindex) myShapefile.EditInsertShape(shape, shapeindex) shapeindex += 1 'Add all attributes to .dbf file Dim intcount As Integer = 0 For Each Row As DataGridViewRow In frmDataGrid.DataGridView.Rows If frmDataGrid.DataGridView.Rows(intcount).Cells(0).Value = "" Then myShapefile.EditCellValue(intcount, -1, "") intcount += 1 End If Next Row Next success = myShapefile.StopEditingShapes(True) Try 'Save edits myShapefile.SaveAs(myShapefile.Filename) Dim myShp As New MapWinGIS.Shapefile 'Add shapefile to map and legend myShp.Open(myShapefile.Filename) Dim hndshp As Integer = frmMain.mwLegend.Layers.Add(myShp, True) frmMain.mwAxMap.set_LayerName(hndshp, System.IO.Path.GetFileNameWithoutExtension(myShp.Filename)) frmMain.mwAxMap.set_ShapeLayerPointSize(hndshp, 3) frmMain.mwAxMap.set_ShapeLayerPointColor(hndshp, Convert.ToInt32(RGB(255, 0, 0))) frmMain.mwAxMap.ZoomToLayer(hndshp) 'Error reporting Catch ex As Exception MsgBox(ex.Message) End Try 'Finish Me.Close() frmDataGrid.Close() End Sub
Edited 1 time(s). Last edit at 11/17/2009 09:21AM by SandraMF.
Re: how to make shapefile from a table?
Posted by:
pmeems ()
Date: November 17, 2009 12:54PM
Sandra,
You now do a double loop of the rows.
Change your red code to
You should also use myShapefile.StopEditingShapes(True, True) to save the table as well.
No need to call myShapefile.SaveAs(), just call myShapefile.Close(), re-open and add it to the layer.
You could also just call SaveAs() and add it directly to the layer.
Hope it helps.
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
You now do a double loop of the rows.
Change your red code to
For k As Integer = 0 To frmDataGrid.DataGridView.ColumnCount - 1 Dim cellValueOfDataGrid as string = frmDataGrid.DataGridView(k, r).Value myShapefile.EditCellValue(k, k, cellValueOfDataGrid) Next k
You should also use myShapefile.StopEditingShapes(True, True) to save the table as well.
No need to call myShapefile.SaveAs(), just call myShapefile.Close(), re-open and add it to the layer.
You could also just call SaveAs() and add it directly to the layer.
Hope it helps.
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
Re: how to make shapefile from a table?
Posted by:
SandraMF ()
Date: November 17, 2009 02:44PM
Hi again Paul,
Thank you for getting back to me so quickly. I have input the code as you suggested but have encountered the following errors:
1) I first got an "Overload resolution" error but then I changed frmDataGrid.DataGridView(k, r).Value to frmDataGrid.DataGridView.Datasource (k, r).Value which seemed to solve the problem until I debugged,
2) Then I got a "System Missing Member Exception" because {"No default member found for type 'DataTable'."}. I did some Googling and some sites suggest swapping the row and column around, which I tired but got the same error.
3) Another site suggested a conversion, so i changed the code as follows:
From this I got an Invalid Cast Exception"because {Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataView'}.
I made sure that I had the Imports for the System.Windows.Forms.DataGridViewRow just incase but still no go.
The [www.vb-helper.com] website suggests building a bound dataset version of the datagrid, which is what I'm going to try out now unless you think I'm barking up the wrong tree?
Thanks again
Cheers
Thank you for getting back to me so quickly. I have input the code as you suggested but have encountered the following errors:
1) I first got an "Overload resolution" error but then I changed frmDataGrid.DataGridView(k, r).Value to frmDataGrid.DataGridView.Datasource (k, r).Value which seemed to solve the problem until I debugged,
2) Then I got a "System Missing Member Exception" because {"No default member found for type 'DataTable'."}. I did some Googling and some sites suggest swapping the row and column around, which I tired but got the same error.
3) Another site suggested a conversion, so i changed the code as follows:
'Add all attributes to .dbf file For k As Integer = 0 To frmDataGrid.DataGridView.RowCount - 1 Dim dv As DataView = CType(frmDataGrid.DataGridView.DataSource, DataView) Dim ThisFieldID As String = CType(dv(k).Item("id"), String) myShapefile.EditCellValue(k, k, ThisFieldID) Next k
From this I got an Invalid Cast Exception"because {Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataView'}.
I made sure that I had the Imports for the System.Windows.Forms.DataGridViewRow just incase but still no go.
The [www.vb-helper.com] website suggests building a bound dataset version of the datagrid, which is what I'm going to try out now unless you think I'm barking up the wrong tree?
Thanks again
Cheers
Attachments: error1.gif (78 KB)
Re: how to make shapefile from a table?
Posted by:
pmeems ()
Date: November 18, 2009 05:02AM
Sandra,
First of all you're looping the rows again.
You need to do this (pseudo-code):
--
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
First of all you're looping the rows again.
You need to do this (pseudo-code):
// Create shapefile.
// Setup fields.
// loop through all rows:
Foreach row in DataGridView.Rows
// Create and add shape based on coordinates from DataGridView.
// Loop all columns and add them as attributes to shape:
For k As Integer = 0 To frmDataGrid.DataGridView.ColumnCount - 1
Dim cellValueOfDataGrid as string = row.Cells(k).Value
myShapefile.EditCellValue(k, shapeID, cellValueOfDataGrid)
Next k
Next row
--
Paul
--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group
Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.
---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS
Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl
*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn
Re: how to make shapefile from a table?
Posted by:
SandraMF ()
Date: November 18, 2009 08:40AM
EXCELLENT!
Thanks so much for your help and patience Paul, it's working :) :)
I added in a - 1 for the row index to compensate for the headers but what a beautiful site to see all those attributes in the .dbf :)
If it will be useful, I'll add all my code to the the Sample code library once I'm done for future beginners.
Thanks again and enjoy your day.
Cheers
Sandra
Thanks so much for your help and patience Paul, it's working :) :)
I added in a - 1 for the row index to compensate for the headers but what a beautiful site to see all those attributes in the .dbf :)
For k As Integer = 0 To frmDataGrid.DataGridView.ColumnCount - 1 Dim cellValueOfDataGrid As String = r.Cells(k).Value myShapefile.EditCellValue(k, shapeindex - 1, cellValueOfDataGrid) Next k
If it will be useful, I'll add all my code to the the Sample code library once I'm done for future beginners.
Thanks again and enjoy your day.
Cheers
Sandra
Re: how to make shapefile from a table?
Posted by:
dbyond ()
Date: December 06, 2011 06:07AM
Did Sandra's code for this ever get posted somewhere?
Thanks
Thanks
Re: how to make shapefile from a table?
Posted by:
dbyond ()
Date: December 06, 2011 06:09AM
Did Sandra's code ever get posted anywhere.
Thanks
Thanks
Sorry, only registered users may post in this forum.


