private void createExporterFieldDefns(List <TypeAndName> fields) { FieldDefn fieldDefn; exporterFeatDefn = new FeatureDefn("ok"); for (int i = 0; i < fields.Count; i++) { var field = fields[i]; switch (field.TypeDef.ToString()) { case "System.Int32": fieldDefn = new FieldDefn(field.Name, FieldType.OFTInteger); break; case "System.DateTime": fieldDefn = new FieldDefn(field.Name, FieldType.OFTString); //fieldDefn = new FieldDefn(field.Name, FieldType.OFTDateTime); break; case "System.Double": fieldDefn = new FieldDefn(field.Name, FieldType.OFTReal); break; case "System.String": default: fieldDefn = new FieldDefn(field.Name, FieldType.OFTString); break; } exporterLayer.CreateField(fieldDefn, 1); exporterFeatDefn.AddFieldDefn(fieldDefn); fieldDefn.Dispose(); } }
public void CreateShapeFile(string projref, string shapefilepath) { //creates a shapefile with a separate geometry feature for each tile in the results list string drivertype = "ESRI Shapefile"; OSGeo.OGR.Driver shpdriver = Ogr.GetDriverByName(drivertype); if (shpdriver == null) { Console.WriteLine(" [-] Couldn't get shapefile driver."); return; } SpatialReference spatialref = new SpatialReference(projref); //a list of lists, each represent a separate tile name //create new datasource //DataSource shapefileds = shpdriver.CreateDataSource(Path.GetDirectoryName(shapefilepath), new string[] { }); DataSource shapefileds = shpdriver.CreateDataSource(shapefilepath, new string[] { }); if (shapefileds == null) { Console.WriteLine(" [-] Couldn't create shapefile datasource."); return; } //new field to add the tile name to FieldDefn newfield = new FieldDefn("TileName", FieldType.OFTString); string fname = Path.GetFileName(shapefilepath); //create new layer to add features to Layer newlayer = shapefileds.CreateLayer(Path.ChangeExtension(fname, null), spatialref, wkbGeometryType.wkbPolygon, new string[] { }); if (newlayer == null) { Console.WriteLine(" [-] Layer creation failed."); shapefileds.Dispose(); newlayer.Dispose(); return; } if (newlayer.CreateField(newfield, 1) != Ogr.OGRERR_NONE) { Console.WriteLine(" [-] Creating TileName field failed."); shapefileds.Dispose(); newfield.Dispose(); return; } foreach (string csvfile in ResultCoords.csvfiles) { IEnumerable <List <string> > coordsbytile = ReturnClusterCoords(csvfile); foreach (var tile in coordsbytile) { //create new point geometry for every element in each list Geometry clustergeom = new Geometry(wkbGeometryType.wkbMultiPoint); clustergeom.AssignSpatialReference(spatialref); string layername = (tile.First()).Split(',').Last(); int iter = 0; int currentsequence = 0; for (int i = 0; i < (tile.Count()); i++) { string point = tile[i]; string nextpoint; if (!(i == tile.Count() - 1)) { nextpoint = tile[i + 1]; } else { nextpoint = point; } double x = Convert.ToDouble(point.Split(',')[0]); double y = Convert.ToDouble(point.Split(',')[1]); int currw = Convert.ToInt32(point.Split(',')[2]); int nextrw = Convert.ToInt32(nextpoint.Split(',')[2]); int currcol = Convert.ToInt32(point.Split(',')[3]); int nextcol = Convert.ToInt32(nextpoint.Split(',')[3]); currentsequence = Convert.ToInt32(point.Split(',')[4]); int nextsequence = Convert.ToInt32(nextpoint.Split(',')[4]); int ydistance = nextrw - currw; int xdistance = nextcol - currcol; bool samesequence = (currentsequence == nextsequence) ? true : false; bool onsamerow = (nextrw - currw == 0) ? true : false; Geometry newpoint = new Geometry(wkbGeometryType.wkbPoint); newpoint.SetPoint(0, x, y, 0); clustergeom.AddGeometry(newpoint); ++iter; // this will create some "lines" if there are a lot of shadows in adjacent rows, but they will get buffered out if ((ydistance > 300 || (xdistance > 300 || xdistance < -300)) && ((!samesequence && onsamerow) || ydistance > 2)) { iter = 0; CreateHullFeature(newlayer, layername, clustergeom, currentsequence); clustergeom = null; clustergeom = new Geometry(wkbGeometryType.wkbMultiPoint); } } if (!clustergeom.IsEmpty() && iter > 0) { CreateHullFeature(newlayer, layername, clustergeom, currentsequence); } } } shapefileds.Dispose(); newfield.Dispose(); newlayer.Dispose(); }