[gdal-dev] Problem with adding Attributes to a point shapefile
Henneke, Amanda M
amanda.m.henneke at boeing.com
Thu Oct 29 17:00:59 EDT 2009
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.
More information about the gdal-dev
mailing list