How to Create a New shape file

Topics: Data Access
Jan 18, 2007 at 3:51 AM
Hi All,
I have just downloaded the latest release of the SharpMap. I'm not able to find some methods in the ShapeFile Class. First One is Save method.

So May be i have downloaded tha wrong release. so please suggest me a proper release or Can anybody tell me how to create a New shape file with only a single point with ShapeMap API.

Thanks
Jan 19, 2007 at 11:24 AM
I tried to write a shape file with a single point. and the code i used is as below.
but its not working. Can anbod Help. Plz tell me how to create a new shape file

FeatureDataTable fdt = new FeatureDataTable();
FeatureDataRow dr;
dr = fdt.NewRow();
SharpMap.Geometries.Point pt = new Point(100.99, 100.99);
dr.Geometry = pt;
fdt.Rows.Add(dr);
ShapeFile shp = ShapeFile.Create("C:\\","Table1",ShapeType.Point,fdt);
shp.Open(true);
shp.Save(fdt);
shp.Close();

Feb 2, 2007 at 11:23 AM
你好!从读取shape文件的方法可以看出,Header部分是有一定的编码方式,与此同时,关于内容的存储同样需要进行相应的格式化。你的这个问题是我的一个反问题,如果有解决办法,我会立刻上传的。
Coordinator
Feb 2, 2007 at 12:09 PM
Hi,

Where did you find a writeable ShapeFile provider?
I can't find it in the latest change set.

regards
Christian
Coordinator
Feb 21, 2007 at 5:25 PM
It's in the Alpha 2.0 code...
Feb 22, 2007 at 6:51 AM
where is the alpha 2.0 code?
Coordinator
Feb 22, 2007 at 3:56 PM
In the planned releases section of the releases tab:

http://www.codeplex.com/SharpMap/Release/ProjectReleases.aspx?ReleaseId=463
Coordinator
Feb 22, 2007 at 4:18 PM
Edited Feb 22, 2007 at 4:19 PM
Oh, note that the DbaseWriter class in that release has a defect in it somewhere which I haven't been able to get around to, but will be doing so shortly here, as I need it for a project.
Apr 11, 2007 at 3:52 PM
I tried to get the dbase write to work,

This seems to not work:

public DbaseField[] Columns
{
get { return _dbaseColumns; }
internal set
{
_dbaseColumns = value;
HeaderLength = (short)((DbaseConstants.ColumnDescriptionLength * _dbaseColumns.Length) + DbaseConstants.ColumnDescriptionOffset + 1 /* For terminator */);
RecordLength = 1; //Deleted flag length
Array.ForEach<DbaseField>(_dbaseColumns, new Action<DbaseField>(delegate(DbaseField field) { RecordLength += (short)field.Length; }));
}
}

in DBaseHeader.cs

In particular:

Array.ForEach<DbaseField>(_dbaseColumns, new Action<DbaseField>(delegate(DbaseField field) { RecordLength += (short)field.Length; }));

doesn't seem to set RecordLength properly. I've never seen this syntax before, so it has me stumped.

Roger
Apr 11, 2007 at 8:10 PM
I delved into it a bit deeper, and the above code is fine. The issue is that I don't understand how to create the FeatureDataTable Model in:

public static ShapeFile Create(DirectoryInfo directory, string layerName, ShapeType type, FeatureDataTable model)

Just using my feature table as it comes from SQL Server doesn't work, as the MaxLength of the field (in case of an Int32) is always -1, which

internal static DataTable DeriveSchemaTable(FeatureDataTable table)

always uses in lieu of any added column.ExtendedProperties. There are no hints in the code on how to set up the model either. I think what is needed is another method that converts column.DataType to dBase field sizes. I suppose I could try assigning the correct values to MaxLength...Well, that didn't work, but using extended properties did:

for (int i = 0; i < Clone.Columns.Count; i++)
{
string ralph = Clone.Columnsi.ColumnName;
if (Type.GetTypeCode(Clone.Columnsi.DataType) == TypeCode.Boolean)
{
Clone.Columnsi.ExtendedProperties.Add("Length", 1);
}
else if (Type.GetTypeCode(Clone.Columnsi.DataType) == TypeCode.Int32)
{
Clone.Columnsi.ExtendedProperties.Add("Length",4);
}

}

Well, in the end, I gave up on the DBF stuff, and just wrote out a SHP:

private void button1_Click(object sender, EventArgs e)
{
//Export an SQL coverage to SHP
//Roger Bedell, roger@sylvanascent.com
//Just simple polygons from a file I have.

//Open the SQL table
string CS = @"Data Source=skinny\SQLEXPRESS;Initial Catalog=USGSMasterNoQuadletts;Integrated Security=True;";
SharpMap.Data.Providers.MsSql MSP = new SharpMap.Data.Providers.MsSql(CS, "PGN10MInStock", "WKB_Geometry", "oid");//.ShapeFile(@"C:\TopoDepotDatabases\conus7\conus7.shp");
MSP.Open();

//Create a new FDS
SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();

//Get the FDS from the database
MSP.ExecuteIntersectionQuery(new SharpMap.Geometries.BoundingBox(-124.875,24.4999980926514,-66.8750076293945,49.4999923706055), ds);

//Get a table that is the same as the SQL table, except for the Geom column(DBF doesn't like that)
SharpMap.Data.FeatureDataTable Clone = ds.Tables0.Clone();

for (int i = 0; i < Clone.Columns.Count; i++)
{
string ralph = Clone.Columnsi.ColumnName;
if (Type.GetTypeCode(Clone.Columnsi.DataType) == TypeCode.Boolean)
{
Clone.Columnsi.ExtendedProperties.Add("Length", 1);
}
else if (Type.GetTypeCode(Clone.Columnsi.DataType) == TypeCode.Int32)
{
Clone.Columnsi.ExtendedProperties.Add("Length",4);
}

}


//Create a shape file, with the correct feature (DBF) columns. Forget the DBF for now...
SharpMap.Data.Providers.ShapeFile.Create(@"C:\topodepotdatabases", "Test", SharpMap.Data.Providers.ShapeType.Polygon);//, Clone);

//Open the SHP
SharpMap.Data.Providers.ShapeFile SHP = new SharpMap.Data.Providers.ShapeFile(@"C:\topodepotdatabases\test.shp");
SHP.Open(true);

//Save the feature table to the SHP
SHP.Save(ds.Tables0);

//Shut down
SHP.Close();
MSP.Close();
}
}



Apr 12, 2007 at 8:35 AM

Can I convert GFX format to Shape format?
Apr 13, 2007 at 12:52 PM

And NMEA Output to a Shape file?
Apr 18, 2007 at 7:07 AM
I am able to create a shape file with only one point in it.

If i open that file using shape viewer it does not show me a point, Why?

my code:

SharpMap.Data.FeatureDataTable dt = new SharpMap.Data.FeatureDataTable();
SharpMap.Data.FeatureDataRow rw = dt.NewRow();
rw.Geometry = new SharpMap.Geometries.Point(100, 100);
dt.AddRow(rw);
SharpMap.Data.Providers.ShapeFile.Create(Server.MapPath(@"~\App_data"), "test", SharpMap.Data.Providers.ShapeType.Polygon, dt);
SharpMap.Data.Providers.ShapeFile shp = new SharpMap.Data.Providers.ShapeFile(Server.MapPath(@"~\App_data\test.shp"));
shp.Open(true);
shp.Save(dt);
shp.Close();

is there there any thing wrong with this ?

if i have shape file, is it possible to edit it.

Thanks & Regards,

maheshkumar
May 9, 2007 at 7:50 AM
Hi,
I am very new to sharpmap.I'm been given to develope an application using sharpmap.Few queries for you to answer
I render "n" number of Map layers to an image button upon checking the ckeckboxlist control.Upon unchecking the ckeckboxlist item,I disable the layer in the img_button.
(1);Say,if I have added 5 layers over the img_button.(ex countries,states,cities,lakes,hospitals layers) and I click on a particular lake.Now I need to get the name of the layer that the user clicked upon.How do I get it?Is it possible?
(2).Secondly,I need to convert the point clicked to world co-ordinates and fetch the row of data corresponding to that lake(on which the user clicked) from the data source of that layer which contains the lakes and display it in a datagrid..

I've used shape files and corresponding Db4 file for each vector layer.
Please kindly help me in this regard as early as possible.




Jun 1, 2007 at 10:01 PM

codekaizen wrote:
Oh, note that the DbaseWriter class in that release has a defect in it somewhere which I haven't been able to get around to, but will be doing so shortly here, as I need it for a project.


I found one problem in SharpMap/Data/Providers/Shapefile/ShapeFile.cs
change the line

                    while (numberAttemptsToOpenDbfForWriting < 3)
to
                    while (_dbaseWriter == null && numberAttemptsToOpenDbfForWriting < 3)

Otherwise it tries to get a DbaseWriter 3 times even if it succeeds on the first try and I think that messes it up. It seemed to help. Still have not successfully written a file but I think it's because I have to define attribute fields.

Brian
Coordinator
Jun 19, 2007 at 10:23 AM
Wow, what an egregious bug. Even worse is the design. I've added the null check and slated a better design. Thanks!