[Gdal-dev] Writing a polygon to a new shapefile

Frank Warmerdam warmerdam at pobox.com
Tue Mar 24 15:42:04 EDT 2009


Amanda,

I see a few issues.

On Tue, Mar 24, 2009 at 1:09 PM, AmandaH <amanda.m.henneke at boeing.com> wrote:
> OSGeo.OGR.Driver new_drv = Ogr.GetDriverByName("ESRI Shapefile");
> OSGeo.OGR.SpatialReference new_sr = new OSGeo.OGR.SpatialReference("");
> OSGeo.OGR.DataSource ds_out = new_drv.CreateDataSource("D:\\Vector\\Output\\new.shp", new string[] { });
>
> new_sr.IsSameGeogCS(old_sr);

IsSameGeogCS() is a test, returning a boolean.   Was this intended to do
something?

> OSGeo.OGR.Layer new_layer = ds_out.CreateLayer("new_polygons", new_sr,   OSGeo.OGR.wkbGeometryType.wkbPolygon, new string[] { });
>
> FeatureDefn new_def = new_layer.GetLayerDefn();
> OSGeo.OGR.Feature new_feat = new OSGeo.OGR.Feature(new_def);
>
> temp_geom = new Geometry(wkbGeometryType.wkbPolygon);
>
> dX = geom.GetX(iPoint);
> dY = geom.GetY(iPoint);
> dZ = geom.GetZ(iPoint);
>
> temp_geom.AddPoint(dX, dY, dZ);

Polygons consists of rings, and the rings consist of points.  So to add
a point to the polygon you really need to add it to the first ring.  I'm
not positive what the method names are in C# but the gist of it is
that you need to add a ring to the polygon.  Then add the point to the
ring.


> new_feat.SetGeometryDirectly(temp_geom);
> new_layer.CreateFeature(new_feat);
> temp_geom.Dispose();

Note that SetGeometryDirectly() normally transfers ownership
of the geometry to the feature, and you should not explicitly
destroy the geometry that is owned by the feature.  If you use
SetGeometry() instead of SetGeometryDirectly() then the feature
will make a copy of the passed geometry.

Best regards,
> --
> View this message in context: http://n2.nabble.com/Writing-a-polygon-to-a-new-shapefile-tp2527864p2527864.html
> Sent from the GDAL - Dev mailing list archive at Nabble.com.
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>



-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent


More information about the gdal-dev mailing list