Posts

Showing posts with the label GDAL

Code snippet: Identify to which polygon a point belong to?

Code sample to find point inside a polygon in shape file using GDAL/OGR 2.x and C#. using OSGeo.OGR; using OSGeo.OSR; using OSGeo.GDAL; 1: public void FindPolygonBelongsToPoint() 2: { 3: try 4: { 5: var shapePath = @"C:\Locations_WGS84.shp"; 6: Ogr.RegisterAll(); 7: OSGeo.OGR.Driver shapeDriver = Ogr.GetDriverByName("ESRI Shapefile"); 8: DataSource shape = shapeDriver.Open(shapePath, 0); 9: Layer layer = shape.GetLayerByIndex(0); 10: 11: Feature polygonFeature = layer.GetNextFeature(); 12: 13: long featureCountInLayer = layer.GetFeatureCount(1); 14: for (int i = 1; i <= featureCountInLayer; i++) { 15: 16: Geometry polygonGeom = polygonFeature.GetGeometryRef(); 17: 18: string coordinates = "POINT (-100.726 38.995)"; 19: 20: SpatialReference spatialRef = layer.GetSpatialRef(); ...

Code snippet : Create/update polygon attribute fields using Ogr and ESRI Arcobjects

A code snippet example to check if a feature is standalone ESRI Shape or featureclass inside ESRI Gdb, then add new filed or update attribute value in attribute table .... standalone shape/features are created/updated using GDAL/OGR implementation and feature/featureclass stored in ESRI personal GDB are created/updated using ESRI ArcObjects.  Any solution to replace later with GDAL/Ogr is highly appreciated.  Please comment below if you find one. public void AddUpdateAttributeField(string oldFeatureFile) { DriverUtils.RegisterOgrDriver(); DataSource dataSource; Layer layer; var isShapeFile = IsShapeInGdb(oldFeatureFile); if (isShapeFile) { dataSource = Ogr.Open(oldFeatureFile, 1); //second argument in open specifies mode of data, 1 RW & 0 readonly mode layer = dataSource.GetLayerByIndex(0); FieldDefn gdalFiedlDefn = new FieldDefn("FID_GDAL",FieldType.OFTInteger); la...

Code snippet: Create new Field in a Shape File using GDAL/OGR in C#

Add new field in existing shape file using OGR in C#. public void AddAttributeField(string oldShapeFile) { Ogr.RegisterAll(); DataSource dataSource = Ogr.Open(oldShapeFile, 1); //second argument in open specifies mode of data, 1 RW & 0 readonly mode Layer layer = dataSource.GetLayerByIndex(0); FieldDefn gdalFiedlDefn = new FieldDefn("NEW_FIELD",FieldType.OFTInteger); layer.CreateField(gdalFiedlDefn, 1); Feature feature = layer.GetNextFeature(); while (feature!= null) { feature.SetField("NEW_FIELD",feature.GetFID()); // Populate new field with feature FID layer.SetFeature(feature); feature = layer.GetNextFeature(); } dataSource.FlushCache(); }

Read ESRI File Gdodatabase (FileGDB) using GDAL & C#

If you are creating a new project- set up GDAL & C# environment as described here Code snippet to read ESRI File Geo-database (FileGDB) using GDAL and C#. By default it uses GDAL's ' OpenFileGDB ' (read only) driver with out any external dependencies. If you are interested in editing GDB feature class you should use ' FileGDB ' (read-write) driver, which had dependency on ESRI's FGDB API SDK . The  UCLA's internal testing showed that ESRI's FileGDB driver drags the performance than OpenFileGDB for read-only operation. So choose the driver according to your needs. However, both GDAL drivers and ESRI's API do not support the raster dataset inside the GDB till date. public static void ReadEsriGdb(string gdbPath, string featureLayerName) { //Register the vector drivers Ogr.RegisterAll(); //Reading the vector data DataSource dataSource = Ogr.Open(gdbPath, 0); Layer layer = dataSource.GetLa...

Read raster block by block or row by row using GDAL and C#

If you are creating a new project- set up GDAL & C# environment as described  here Code Snippet: Read raster block by block using GDAL and C# private static void ReadRasterBlocks(ref Dataset valueRaster) { Band bandValueRaster = valueRaster.GetRasterBand(1); int rasterRows = valueRaster.RasterYSize; int rasterCols = valueRaster.RasterXSize; const int blockSize = 1024; for(int row=0; row<rasterRows; row += blockSize) { int rowProcess; if(row + blockSize < rasterRows) { rowProcess = blockSize; } else { rowProcess = rasterRows - row; }

Read a raster file into an Array using C# and GDAL

If you are creating a new project- set up GDAL & C# environment as described  here Code snippet: Read raster into an Array using GDAL and C# //Register all drivers Gdal.AllRegister(); //Read dataset Dataset rasterDataset = Gdal.Open("rasterName.tif", Access.GA_ReadOnly); if (rasterDataset == null) { Console.WriteLine("Unable to read input raster.."); System.Environment.Exit(-1); } //raster bands int bandCount = rasterDataset.RasterCount; if (bandCount > 1) { Console.WriteLine("Input error, please provide single band raster image only.."); System.Environment.Exit(-1); }

Clip a raster with shapefile using C# and Gdal

Image
Over the week, I stumbled on problem clipping raster with feature using C# and Gdal and come up with following solution, which clips the features and fills no data value for missing raster  values inside the extent. If you are creating a new project- set up GDAL & C# environment as described  here

Vector to Raster Conversion using GDAL & C#

Image
I have been working on a .Net based project that requires feature to raster conversion without using ESRI ArcObjects. The GDAL seem's an obvious solution to us and wrote a small snippet for rasterize layer using Gdal and C#. Hope it will help someone someday... Step 1: Setup the environmental variables for GDAL  Setup the environment as described in   Getting Started with C# and GDAL OR  (Copied here for future reference - ) Getting started with C# and GDAL The Geospatial Data Abstraction Library (GDAL) is great if you want to process raster data, especially regarding format conversion. I want to use GDAL for a biodiversity modelling project, so I had a look at the C#-bindings of GDAL. The described steps work both with VS 2010 as well as VS 2012, what you need to do is: Download the latest version of the precompiled GDAL binaries from  here . Choose the ones that suit your system (32bit or 64bit). Extract the contents from the zip file ...

Tiler tools, an alternative scripts to Gdal2tiles.py for creating raster tiles from digital maps

Image
Other day I was working to generate the Google Map based raster tiles using Gdal2tiles for a large raster file, 30GB in size. Gdal2tiels complains about the following error, which I had no clue. Then fiddling with others tilers available I played with the Tilers-tools , python Scripts for raster tile sets from digital maps with GDAL dependency. After spending few days hit and trial, finally I am able to generate raster tiles compatible with Google Maps. Here I am sharing the steps that I follow to produce tiles using the Tiler tool. Hope it will help some of the OSGeo users. I have divided tile generation steps into two groups, dependency installation and data preparation with a presumption that you are already familiar with GDAL . A] DEPENDENCY INSTALLATION STEP 1. Install Python geospatial packages Install the dependencies as described- https://github.com/GitHubRGI/geopackage-python/wiki/Installing-dependencies-on-Windows For my project, I used 32 bit inst...

Raster Misalignment with Base Data in ArcMap10

Image
In the early Friday morning of mid May, I got an email from one of my team member about raster misalignment problem in ArcGIS10. I also tried to overlay couples of previously working Tiff and Grid raster files in ArcMap9.3 and ArcMap10 . The ArcMap9.3 overlay raster files perfectly aligned as we all desired, but ArcMap10 did not. Unaligned Aligned From the ESRI website, I got to know that the issue of misalignment of Tiff in ArcMap10 is a bug in ArcGIS 10. The ESRI team announced two solutions to solve Tiff shift into wrong geographic locations: