Copy all geometry and features from one shapefile to another

Topics: SharpMap Project, SharpMap v2.0, WinForms Controls
Aug 12, 2009 at 8:44 AM

Hello guys!

 

I would like to ask you for a litte bit of help.

Here's what I need :

I have some shapefiles and I want to make some new ones. Using C# and SharpMap v2.0, I need to copy from example1.shp to myshape.shp the whole content of example1.shp. System.IO.File.Copy(@"C:\example1", @"C:\destination") is certainly is not an option.

 

I need to open example1, parse it, and copy each feature and geometry in the newly created myshape.shp (at the time, I have only the header.) Some code snippet would be higly appreciated.

 

You me be asking why do I need this?
Well, the answer is quite simple : I want to add color info (and some style info maybe) into the layer plus some other things I don't want to mention yet, until it's done and working smooth. (if you want, I could share my work afterwards)

 

Well, I must mention that I am a beginner with SharpMap. I would also like to apologise for my "not-so-good" english.

 

I thank you all in advance.

Coordinator
Aug 12, 2009 at 11:13 AM

Hi georgeblaga,

there is a FormatConverter Project in the demo section of the sharpmap solution that might get you started. Just pretend you want to convert from shapefile to shapefile.

Hth

FObermaier

Coordinator
Aug 12, 2009 at 11:33 AM
Edited Aug 12, 2009 at 11:34 AM

Perhaps the ShapeExporter demo would be an easier starting point - that demo takes a shapefile of n records and produces n shapefiles of 1 record - there is less *magic* to get in the way ;) .

hth jd

Aug 12, 2009 at 12:29 PM
Edited Aug 12, 2009 at 12:58 PM

Thanks fobermaier and johndiss for your replies. I really appreciate!

 

But the thing is that I don't want to use converters. I create a new, blank shapefile and then I start adding features from an existing, "genuine" shapefile.Actually, is not really blank, because the create method from the shapefile class writes the file header.

So, if I didn't make myself understanded in the first post , converting, copying etc is not an option. I need a blank .shp (from the features' and geometries' point of view)

Also, another thing I have missed to mention, I don't use the SharpMap 2.0 from googlecode.com, but the old SharpMap 2.0 from here, from codeplex

 

Here is a piece of my code:

 

            public void LoadShapefileLayer(ShapeFile OriginalShapefile,ShapeFile NewShapeFile)
            {
                
                if (OriginalShapefile.ShapeType != ShapeType.Polygon && OriginalShapefile.ShapeType != ShapeType.PolyLine
                    && OriginalShapefile.ShapeType != ShapeType.MultiPoint && OriginalShapefile.ShapeType != ShapeType.Point)
                    return;

                int Count = OriginalShapefile.GetFeatureCount();

               //List<Geometry> geometry = new List<Geometry>();
                
                BoundingBox ext = OriginalShapefile.GetExtents();
                IEnumerable<Geometry> geometries = OriginalShapefile.GetGeometriesInView(ext);
                
                foreach (Geometry geom in geometries)
                {
                //    List<SharpMap.Geometries.Point> points = new List<SharpMap.Geometries.Point>();
                    FeatureDataSet fds = new FeatureDataSet();
                    OriginalShapefile.ExecuteIntersectionQuery(geom.GetBoundingBox(), fds);
                    FeatureDataTable fdt = fds.Tables[0];

                    if (fdt.Rows.Count > 0)
                    {
                        foreach (FeatureDataRow fdr in fdt.Rows)
                        {
                            if (geom.Equals(fdr.Geometry))
                            {
                                NewShapefile.Save(fdr);
                            }
                        }
                    }
                }

            }


Both files are opened; OriginalShapefile is enabled for reading and the NewShapefile is enabled for writing.

Ok, don't bother to test the code, it doesn't works.

<EDIT1> : // A strange thing is that count is 0 and I don't have any idea why, honestly..1

Count is not 0 anymore. Reason was... well... kind of stupid.. I've commented out some lines in Open(..) method, the ones that parsed the index and projection. Counting is ok now,,

</EDIT1>

<EDIT2>: well, it kind of works. I mean something like : both the original and the newly created have the same size, somewhere around 2 mb.. But.. When i open the new shape, with the debugger, some features aren't there. Like the envelope box. which is empty. also, i don't have the same number of features. In the original one, the variable Count is ~ 70k, while in the new one it's ~ 50. att: 50, not 50k.

What am I doing wrong?

I need to see this working, before I start adding color and style info directly in the .shp

</EDIT2>

 

I hope you understand what I want (I'm not really good in explaining to others what I want to do :) )

Thanks again for support and I expect to hear (or read :P ) from you :)

By the way, nice job that you are doing here, with SharpMap!

 

 

 

 

Coordinator
Aug 12, 2009 at 12:56 PM

Hi georgblaga, the code you have posted is not current v2 code, the real v2 is @ http://sharpmapv2.googlecode.com , also both the demo projects suggested above do create new shapefiles and populate them not convert an existing file (but they require the real v2)..  

Aug 12, 2009 at 1:01 PM

Thanks again johndiss. Only after editing my post I seen your message.

I'll switch to the real v2 code and try what you told me.

Thanks again!

 

If I have any doubts, which I can not solve in any manner, I'll ask for your help again!

 

Have a nice day :)