Register  |  Login
ThinkGeo - GPS Tracking and Mapping Solutions  |  Home  |  Cygnus Track  |   Code Community

Discussion Forums

The online community for users of Map Suite GIS components

RSS Feed Available AddThis - Bookmarking and Sharing Button Printer Friendly

PrevPrev NextNext

Create ShapeFiles From Scratch in Map Suite 3.x

Posted by ThinkGeo on 03-05-2009 11:29 PM

Map Suite 3.0 has the ability to edit existing ShapeFiles, but what if you want to create your own? The code below should get you started, but if you want to explore more ShapeFile editing, check out our EditShapeFile sample application included with the evaluation download. Additionally, if you're using Map Suite Web Edition, you can download the attached sample application at the end of this article.

Here is a very simple example of creating a point-based ShapeFile from scratch. It adds three records and fills the value for the field "Name".

C#

    string shapeFilePath = "c:\\cities.shp";
    
    DbfColumn NameColumn = new DbfColumn("Name", DbfColumnType.String, 30, 0);
    DbfColumn PopulationColumn = new DbfColumn("Population", DbfColumnType.Integer, 10, 0);
    
    ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Point, shapeFilePath, new DbfColumn[] { NameColumn, PopulationColumn });
    
    ShapeFileFeatureSource citiesShapeFile = new ShapeFileFeatureSource(shapeFilePath, ShapeFileReadWriteMode.ReadWrite);
    Dictionary<string, string> LawrenceColumns = new Dictionary<string, string>();
    LawrenceColumns.Add("Name", "Lawrence");
    LawrenceColumns.Add("Population", "80098");
    Feature lawrence = new Feature(new PointShape(-95.253199, 38.959902), LawrenceColumns);
    
    Dictionary<string, string> NaplesColumns = new Dictionary<string, string>();
    NaplesColumns.Add("Name", "Naples");
    NaplesColumns.Add("Population", "21804");
    Feature naples = new Feature(new PointShape(-81.798510, 26.152941), NaplesColumns);
    
    citiesShapeFile.Open();
    citiesShapeFile.BeginTransaction();
    citiesShapeFile.AddFeature(lawrence);
    citiesShapeFile.AddFeature(naples);
    citiesShapeFile.CommitTransaction();
    citiesShapeFile.Close();

VB.NET

    Dim shapeFilePath As String = "c:\cities.shp"
    
    Dim NameColumn As New DbfColumn("Name", DbfColumnType.[String], 30, 0)
    Dim PopulationColumn As New DbfColumn("Population", DbfColumnType.[Integer], 10, 0)
    
    ShapeFileFeatureSource.CreateShapeFile(ShapeFileType.Point, shapeFilePath, New DbfColumn() {NameColumn, PopulationColumn})
    
    Dim citiesShapeFile As New ShapeFileFeatureSource(shapeFilePath, ShapeFileReadWriteMode.ReadWrite)
    Dim LawrenceColumns As New Dictionary(Of String, String)()
    LawrenceColumns.Add("Name", "Lawrence")
    LawrenceColumns.Add("Population", "80098")
    Dim lawrence As New Feature(New PointShape(-95.253199, 38.959902), LawrenceColumns)
    
    Dim NaplesColumns As New Dictionary(Of String, String)()
    NaplesColumns.Add("Name", "Naples")
    NaplesColumns.Add("Population", "21804")
    Dim naples As New Feature(New PointShape(-81.79851R, 26.152941), NaplesColumns)
    
    citiesShapeFile.Open()
    citiesShapeFile.BeginTransaction()
    citiesShapeFile.AddFeature(lawrence)
    citiesShapeFile.AddFeature(naples)
    citiesShapeFile.CommitTransaction()      
    citiesShapeFile.Close()

8 Comments

RoseUser is Offline
03-20-2009 01:33 PM
Avatar
That is great!

But the ShapeFile created does not have the .prj file, how to make sure the .prj file is created at the same time?

Thanks a lot!

Rose


BenUser is Offline
03-22-2009 11:31 PM
Avatar
Rose,

The .prj file is not supported in the current version. In other word, we cannot generate any .prj file or read any info from it. It is in the plan though so I think in the future release, we can support it. Sorry for the inconvenience now.

Thanks,

Ben


ClintUser is Offline
04-07-2009 05:51 PM
Avatar
Thanks Ben, keep the blog posts coming!


BenUser is Offline
04-08-2009 09:21 PM
Avatar
Sure, Clint, I will :)


ChrisUser is Offline
09-10-2009 09:39 AM
Avatar
Ben,

Has the capability to create the .prj file been implemenbed yet?

Thanks,
Chris


DavidUser is Offline
09-11-2009 04:29 AM
Avatar
Chris,

This functionality is still in the queue but at a low priority. We are just finishing up this release and they have not yet defined the items for the next one yet. How would you plan on using it? Just curious?

David


ChrisUser is Offline
09-11-2009 08:35 AM
Avatar
David,

Thanks for your quick response.

We are generating a set of shapefiles based on census data and public domain data for our customers. Curretntly we are using MapInfo to do that(which is a real pain and multi phased process). Having generated shapefiles using ThinkGeo, the process is much cleaner, however, the one downside is the generation of the .prj file. I still have to use yet another tool to do that. It wold be nice if one could do it in one go.

Looking forward to the new release!

Chris


DavidUser is Offline
09-13-2009 08:40 PM
Avatar
Chris,

Just a thought but if it is the Tiger Data then most of what will be in the Prj file will be exactly the same. Maybe you could process two files in your old system and see what the difference is in the prj file. Maybe, just maybe the differences will be really small and predictable and you could build it yourself. I am thinking this as all of the tiger data is in the same projection and not much else should be in the Prj file.

David


You are not authorized to post a reply.
Active Forums 4.2