[gdal-dev] Problem with adding Attributes to a point shapefile
Even Rouault
even.rouault at mines-paris.org
Thu Oct 29 18:33:11 EDT 2009
Selon "Henneke, Amanda M" <amanda.m.henneke at boeing.com>:
Amanda,
The fundamental reason is a call ordering problem. You currently do :
FeatureDefn feadefPt = lyrPt.GetLayerDefn();
Feature feaPt = new Feature(feadefPt);
lyrPt.CreateField(flddefLandUse, 0);
which is wrong as you change the feature definition after having instanciated a
feature. When you instanciate a feature, the feature definition must not change
after (arrays are created with the number of fields of the definition, so layer
the SetField() call will try to set an out-of-bound element). So the fix is to
simply move the feature instanciation after the CreateField() call
Best regards,
Even
> Hello-
>
> I'm trying to create a point shapefile of all the first points in the
> polygons (from a poly shapefile) as shown below. I also want to assign some
> attributes to the different point features. I am having some issues doing
> this. I get an error on the "feaPt.SetField("LAND_USE", sLandUse)" line that
> says "AccessViolationException was unhandled Attempted to read or write
> protected memory. This is often an indication that other memory is corrupt".
>
> If I take the attribute stuff out it runs fine and creates the shapefile.
> I've done attribute adding before and this way has always worked for me, any
> ideas what I'm doing wrong???
>
> public void BuildShapefile(string sPolyShapeFile)
> {
> Ogr.RegisterAll();
>
> // Get driver for the shapefiles
> Driver drvShpFile = Ogr.GetDriverByName("ESRI Shapefile");
>
> // Create a Spatial Reference
> OSGeo.OSR.SpatialReference srGeo = new
> OSGeo.OSR.SpatialReference("");
> srGeo.SetGeogCS("Geographic Coordinate System",
> OSGeo.OSR.Osr.SRS_DN_WGS84, OSGeo.OSR.Osr.SRS_DN_WGS84,
> OSGeo.OSR.Osr.SRS_WGS84_SEMIMAJOR, OSGeo.OSR.Osr.SRS_WGS84_INVFLATTENING,
> "Greenwich", 0.0, "degree", 0.0174532925199433);
>
> // Create a datasource for the polygon file and for the new point
> file
> DataSource dsPolyShpFile = Ogr.Open(sPolyShapeFile, 0);
> DataSource dsPtShpFile =
> drvShpFile.CreateDataSource("D:\\Vector\\Output", new string[] { });
>
> // Create layers for the polygon file and the point file
> Layer lyrPoly = dsPolyShpFile.GetLayerByIndex(0);
> Layer lyrPt = dsPtShpFile.CreateLayer("Junk", srGeo,
> wkbGeometryType.wkbPoint, new string[] { });
>
> // Create Field Definition, Feature Definition and Features to
> enable us writing attributes to the new shapefile
>
> FeatureDefn feadefPt = lyrPt.GetLayerDefn();
>
> Feature feaPt = new Feature(feadefPt);
> Feature feaPoly;
>
> FieldDefn flddefLandUse = new FieldDefn("LAND_USE",
> FieldType.OFTString);
>
> flddefLandUse.SetWidth(100);
>
> //// Create an attribute field
> lyrPt.CreateField(flddefLandUse, 0);
>
> flddefLandUse.Dispose();
>
> // Create the Geometry for the new point file
> Geometry geoPoint = new Geometry(wkbGeometryType.wkbPoint);
>
> Geometry geoPoly = new Geometry(wkbGeometryType.wkbPolygon);
> Geometry geoPolyRing = new Geometry(wkbGeometryType.wkbLinearRing);
>
> int iFeaCount = lyrPoly.GetFeatureCount(1);
>
> // Loop through the poly file to get the points
> for (int iFea = 0; iFea < iFeaCount; iFea++)
> {
> feaPoly = lyrPoly.GetFeature(iFea);
> string sLandUse = feaPoly.GetFieldAsString("PGLANDUSE");
> geoPoly = feaPoly.GetGeometryRef();
> geoPolyRing = geoPoly.GetGeometryRef(0);
>
> // Add a point
> geoPoint.AddPoint(geoPolyRing.GetX(0), geoPolyRing.GetY(0), 0.0);
> feaPt.SetGeometry(geoPoint);
>
> // Set the attribute
> feaPt.SetField("LAND_USE", sLandUse);
>
> lyrPt.CreateFeature(feaPt);
>
> geoPoint.Empty();
>
> if (iFea == (iFeaCount - 1))
> feaPoly.Dispose();
> }
> geoPolyRing.Dispose();
> geoPoly.Dispose();
> geoPoint.Dispose();
> feaPt.Dispose();
> feadefPt.Dispose();
> lyrPoly.Dispose();
> lyrPt.Dispose();
> dsPolyShpFile.Dispose();
> dsPtShpFile.Dispose();
> srGeo.Dispose();
> drvShpFile.Dispose();
> }
>
> Thanks,
> Amanda M. Henneke
>
>
>
>
> When One tugs at a single thing in Nature, he finds it attached to the rest
> of the world. -John Muir
> Please consider the environment before printing this e-mail.
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
More information about the gdal-dev
mailing list