Adding shapes to a shapefile

Oct 25, 2010 at 2:30 PM

Hi all, 

This is really urgent, so any help is very much appreciated! 

I am trying to label my shapes with names that I want to give them.

So I created a shapefile and now I am reading it in my application. 

Can someone help me to create a Shape, add attributes to it and then add it to the file? I want to do this so I can use the label layer to label my shapes with using the attributes that I have added for them. 

If anybody knows how to do that or knows a better way of labelling shapes with your own text, please help. 

Thanks, 
Daniel R.

Developer
Oct 25, 2010 at 3:32 PM
Try using the FeatureSet class instead. The problem with a Shapefile class is that it is an abstract class not intended for public use. I may go about making the entire set of shapefile classes internal so that users can only see the FeatureSet class. If you know you are working with polygons, for instance, you should be working with the PolygonShapefile, and not the Shapefile base class, so the FeatureSet exists as a wrapper that lets you work with the features even if you don't know where they are coming from or what type of feature they will be.

using DotSpatial.Data;
using DotSpatial.Topology;

FeatureSet fs = new FeatureSet();
fs.Open("filename.shp");
List<Coordinate> coords = new List<Coordinate>();
int numPoints = 100;
for(int i = 0; i < numPoints; i++)
{
double x = 1; // some x calculation
double y = 1; // some y calculation
coords.Add(new Coordinate(x, y));
}
Polygon p = new Polygon(coords);
fs.Features.Add(p);
fs.Features[fs.Features.Count - 1].DataRow["FieldName"] = "AttributeValue";
// or working directly with the data table instead:
fs.DataTable.Rows[fs.Features.Count - 1]["FieldName"] = "AttributeValue";


Ted



On Mon, Oct 25, 2010 at 7:30 AM, daniel_r <notifications@codeplex.com> wrote:

From: daniel_r

Hi all,

This is really urgent, so any help is very much appreciated!

I am trying to label my shapes with names that I want to give them.

So I created a shapefile and now I am reading it in my application.

Can someone help me to create a Shape, add attributes to it and then add it to the file? I want to do this so I can use the label layer to label my shapes with using the attributes that I have added for them.

If anybody knows how to do that or knows a better way of labelling shapes with your own text, please help.

Thanks,
Daniel R.

Read the full discussion online.

To add a post to this discussion, reply to this email (DotSpatial@discussions.codeplex.com)

To start a new discussion for this project, email DotSpatial@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Developer
Oct 25, 2010 at 3:34 PM
Woops, you probably need to add fs.SaveAs("filename", true) in there somewhere.

Also, in addition to Polygon, you have LineString, Point, MultiPoint, MultiPolygon, and MultiLineString which are all ogc classes from the Topology suite.

Ted


On Mon, Oct 25, 2010 at 8:32 AM, Harold Dunsford <hadunsford@gmail.com> wrote:
Try using the FeatureSet class instead. The problem with a Shapefile class is that it is an abstract class not intended for public use. I may go about making the entire set of shapefile classes internal so that users can only see the FeatureSet class. If you know you are working with polygons, for instance, you should be working with the PolygonShapefile, and not the Shapefile base class, so the FeatureSet exists as a wrapper that lets you work with the features even if you don't know where they are coming from or what type of feature they will be.

using DotSpatial.Data;
using DotSpatial.Topology;

FeatureSet fs = new FeatureSet();
fs.Open("filename.shp");
List<Coordinate> coords = new List<Coordinate>();
int numPoints = 100;
for(int i = 0; i < numPoints; i++)
{
double x = 1; // some x calculation
double y = 1; // some y calculation
coords.Add(new Coordinate(x, y));
}
Polygon p = new Polygon(coords);
fs.Features.Add(p);
fs.Features[fs.Features.Count - 1].DataRow["FieldName"] = "AttributeValue";
// or working directly with the data table instead:
fs.DataTable.Rows[fs.Features.Count - 1]["FieldName"] = "AttributeValue";


Ted




On Mon, Oct 25, 2010 at 7:30 AM, daniel_r <notifications@codeplex.com> wrote:

From: daniel_r

Hi all,

This is really urgent, so any help is very much appreciated!

I am trying to label my shapes with names that I want to give them.

So I created a shapefile and now I am reading it in my application.

Can someone help me to create a Shape, add attributes to it and then add it to the file? I want to do this so I can use the label layer to label my shapes with using the attributes that I have added for them.

If anybody knows how to do that or knows a better way of labelling shapes with your own text, please help.

Thanks,
Daniel R.

Read the full discussion online.

To add a post to this discussion, reply to this email (DotSpatial@discussions.codeplex.com)

To start a new discussion for this project, email DotSpatial@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com



Oct 25, 2010 at 4:58 PM

Thanks Ted.

I will try it straight away! 

Thank you very much for the rapid reply!

Regards,
Daniel R. 

Oct 25, 2010 at 5:23 PM

Thank you very much Ted. The method that you pointed out works very well. 

Now I am able to save my points to the shapefile and show them on the map afterwards. 

Though I am having a different problem now.

I use the following code (taken from the developer corner doc.) to label my points:

IMapLabelLayer labelLayer = new MapLabelLayer();          
ILabelCategory category = labelLayer.Symbology.Categories[0];            
category.Expression = "[FieldName]";            
category.Symbolizer.Orientation = ContentAlignment.MiddleCenter;            
myLayer.ShowLabels = true;            
myLayer.LabelLayer = labelLayer;

When executing the last line of that code it throws a NullReferenceException from the FeatureSet.cs more precisely from the SelectIndices() method.

on the line: --> if (_shapeIndices[shp].Extent.Intersects(region)) result.Add(shp); 

I think I saw that the _shapeIndices was null.

Do you have any idea what it might be?

Thanks,
Daniel R. 

Developer
Oct 25, 2010 at 5:35 PM

In short it looks like a bug.  I will add this to the issue tracker.  In the mean time, you can probably solve your problem by calling:

 

myFeatureSet.InitializeVertices();

after you have made the changes to the vector content, but before adding the label.  That should set the ShapeIndices correctly.

 

Ted

 

 

 

Oct 25, 2010 at 5:53 PM

Hey Ted, 

I am using the MapWindow.dll then MapWindow 6. It is not the new DotSpatial. It might gotten fixed. Don't know. 

I will try your proposed fix and will let you know how i go.

Again thanks heaps.
Daniel R. 

Oct 25, 2010 at 6:04 PM

No It didn't work Ted. 

But it might not be a bug. It might be my crappy shapefile. 

I created the file using MapWindow 4.xx version. 

And I just went to open it with MapWindow 6 app. and it didn't want to show the layer. The error:

************** Exception Text **************
System.OverflowException: Value was either too large or too small for an Int32.  
at System.Convert.ToInt32(Double value)  
at MapWindow.Map.MapPointLayer.DrawFeatures(MapArgs e, IEnumerable`1 indices)  
at MapWindow.Map.MapPointLayer.DrawFeatures(MapArgs args, List`1 indices, List`1 clipRectangles, Boolean useChunks)  
at MapWindow.Map.MapPointLayer.DrawRegions(MapArgs args, List`1 regions)  
at MapWindow.Map.MapFrame.Initialize(List`1 regions)  
at MapWindow.Map.MapFrame.Initialize()
...

So, summarized I can open the file and display the layer in MapWindow 4 and also in my program in which I am using MapWindow 6. 

But when I try to add the label layer it gave me the error about the shapeIndices and also it doesn't want to display the layer in Map Window 6 app.

If my shapefile is stuffed, is there a way that i can create it programatically or using MapWindow 6?

Thanks,
Daniel R. 

 

 

Developer
Oct 25, 2010 at 6:15 PM

Interesting... I wonder.  I think there have been reports of a problem using DotSpatial when all the points are exactly in the same location.  We added a buffer check if there was only one point, but we didn't check in the case of multiple points but where all the points were the same.  If your points are in different locations, then this won't explain your bug though.  If you could attach a zip file with your shapefile to an "Issue" in the issue tracker, then I can probably hunt down the exception and fix it tonight.

Ted

 

Oct 25, 2010 at 6:33 PM

Will do Ted.

Thank you very much for your help. Looking forward to the outcome.

Daniel R. 

Oct 25, 2010 at 6:42 PM

The Issue has been created.

Id #123  Title: ShapeIndices in the FeatureSet not initialized.

Thanks.
Daniel R. 

Oct 26, 2010 at 1:49 AM

Yes Ted. I've done a bit of testing and all of that. And it seems like a bug where the _shapeIndices variable is probably forgotten to be set to a the indices of the shapes in the shapefile. 

It shows the shapes in my app and also in Map Window apps v6 and v4 the point show up and also I can see that the data was entered correctly by looking at the attributes.

But setting the label layer still doesn't work. I've added my zip with the shapefile on the issues. (sorry didn't see that you already created one so i created one as well)

If you find a fix to it or you find that is my mistake in some step please let me know as soon as possible since the app is due soon. :(

Thank you for your effort. Hope I can be of help in future.
Daniel R. 

Oct 26, 2010 at 5:59 PM

Hi Ted,

I have been working on my application all this time. I noticed when I use: map.AddFeatureLayer() and choose the file myself the labels work, but when i try to read in the file programaticaly:

featureSet.Open(name); -> layer = map.Layers.Add(name); - it fails very badly. And it is the same problem as previously.

Do you have any idea what it can be?

Thanks,
Daniel R.

Developer
Oct 26, 2010 at 6:54 PM
This may be the same bug resurfacing in a different way? Are you using the source code I posted last night? The DotSpatial version choked on it so I built in a check that should deal with the extent issue more gracefully. It should work if you call map.Layers.Add(stuff) regardless of whether it is a string or not. The AddFeatureLayer code is different and may have it's own set of bugs. I suspect though, that it handled the degenerate extents problem differently. After the changes I made last night, the shapefile loads no problem now in the TestViewer application. However, I removed the release binaries because I discovered that our newly implemented automatically incrementing version number for our dll libraries is incompatible with our serialization code. Hence if you save a file, update to the next stable release a few hours later, and try to open the file again, it will break. I am working on fixing that, but I didn't get it finished last night.

If you are working with my updated code, then you may still be evading my work around based on the way you are adding layer content. But as long as the MapFrame is listening to the "LayerAdded" event correctly, it shouldn't matter how you add the file. Let me know if you are using the updated code and still getting the bug. If so, I will re-open the issue.

Ted


On Tue, Oct 26, 2010 at 10:59 AM, daniel_r <notifications@codeplex.com> wrote:

From: daniel_r

Hi Ted,

I have been working on my application all this time. I noticed when I use: map.AddFeatureLayer() and choose the file myself the labels work, but when i try to read in the file programaticaly:

featureSet.Open(name); -> layer = map.Layers.Add(name); - it fails very badly. And it is the same problem as previously.

Do you have any idea what it can be?

Thanks,
Daniel R.

Read the full discussion online.

To add a post to this discussion, reply to this email (DotSpatial@discussions.codeplex.com)

To start a new discussion for this project, email DotSpatial@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com