Wednesday, March 22, 2017

Catch Exception - Attempted to read or write protected memory in ESRI ArcObjects .Net 4.0 Framework

1 Comment
First - Don't write the code to throw "Attempted to read or write protected memory" is bad.

Second - If you are working on legacy codes and COM objects that throws "Attempted to read or write protected memory" while writing ArcMap add-ins, catch exceptions to prevent application crash, in my case ArcMap.

Step #1 - Add following snippet to config file - app.config for Arc-addin

      <legacyCorruptedStateExceptionsPolicy enabled="true" />
Step #2

Add -

on the top of function you are tying catch the exception

public IFeatureLayer GetOrCreateFeatureLayer(string path, esriGeometryType type, ISpatialReference sr)

Friday, December 30, 2016

ESRI FeatureClass vs Feature Layer

Be The First To Comment
Feature Layer: A layer that references a set of feature data. Feature data represents geographic entities as points, lines, and polygons.

Feature Class:In ArcGIS, a collection of geographic features with the same geometry type (such as point, line, or polygon), the same attributes, and the same spatial reference. Feature classes can be stored in geodatabases, shapefiles, coverages, or other data formats. Feature classes allow homogeneous features to be grouped into a single unit for data storage purposes. For example, highways, primary roads, and secondary roads can be grouped into a line feature class named "roads." In a geodatabase, feature classes can also store annotation and dimensions.

Rasterized Feature Layer: A feature layer in ArcGlobe that exists as points, lines and polygons but is rendered as cell data. When layers are added to ArcGlobe, they may automatically be rendered in raster format to retain their cartographic symbology.

Thursday, October 8, 2015

Enable ESRI ArcGIS extension licence from C#

Be The First To Comment
Code snippet on enabling ESRI ArcGIS extension licence for spatial analysis using C#

       UID pUid = new UIDClass();  
       pUid.Value = "esriSpatialAnalystUI.SAExtension";  
       // Add Spatial Analyst extension to the license manager.  
       object v = null;  
       IExtensionManagerAdmin extensionManagerAdmin = new ExtensionManagerClass();  
       extensionManagerAdmin.AddExtension(pUid, ref v);  
       // Enable the license.  
       IExtensionManager extensionManager = (IExtensionManager)extensionManagerAdmin;  
       IExtension extension = extensionManager.FindExtension(pUid);  
       IExtensionConfig extensionConfig = (IExtensionConfig)extension;  
       if (extensionConfig.State != esriExtensionState.esriESUnavailable)  
         extensionConfig.State = esriExtensionState.esriESEnabled;  
         Console.WriteLine("No Spatial Analyst License available");  

Wednesday, September 16, 2015

Feature to Raster Conversion Using C# and ArcGIS

Be The First To Comment
Import ArcGIS.ConversionTools and ArcGIS.Geoprocessor then pass the file paths as param into following function you will get the resulting raster out of shapefile.

 using ESRI.ArcGIS.ConversionTools;  
 using ESRI.ArcGIS.Geoprocessor;  
 public static void Rasterize(string inputFeature, string outRaster, string fieldName, int cellSize)  
         //Runtime manager to find the ESRI product installed in the system  
         Geoprocessor geoprocessor = new Geoprocessor();  
         geoprocessor.OverwriteOutput = true;  
         FeatureToRaster featureToRaster = new FeatureToRaster();  
         featureToRaster.cell_size = cellSize;  
         featureToRaster.in_features = inputFeature;  
         featureToRaster.out_raster = outRaster;  
         featureToRaster.field = fieldName;  
         geoprocessor.Execute(featureToRaster, null);  

Friday, September 11, 2015

Vector to Raster Conversion using GDAL & C#

Be The First To Comment
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 
(Copied here for future reference - )

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 to a location on your hard disk e.g. C:\Program Files\GDAL (I ran into a number of AccessViolationExceptions when I used the binaries from FWTools 2.4.7).
  • Include both the path to C:\Program Files\GDAL\bin\gdal\csharp as well as C:\Program Files\GDAL\bin in your PATH system variable.
Setting of the PATH variable
Creating a Console Application (VS 2010)
  • Add four of.the dll-files that can can be found at C:\Program Files\GDAL\bin\gdal\csharp (or wherever your installation path of the current binaries is) to your project references: gdal_csharp.dllgdalconst_csharp.dllogr_csharp.dll and osr_csharp.
Adding necessary references (VS 2010)
  • Build the solution. If you are on a 64bit system and you are using the 64bit GDAL binaries you have to make sure you actually build for 64bit (you will get errors otherwise when you try to run the program).
Setting of the platform target for 64bit (VS 2012)
  • Now you can run the program with some data. Include a reference to one of the raster files in the Command line arguments field of your Debug options (in the properties of your GDALInfo project; don't forget to put the path inside double quotes of it includes blanks).
  • Run the program (Ctrl-F5). It should show you something similar to the following:
Output of GDALInfo
That's it. Now you can use the other example C#-programs to open, copy, manipulate raster data in variety of file formats. For some of the other example files it is necessary to add some additional references (e.g. System.Drawing).
(End copy)

Step 2: Open visual studio and create C# console project

Import references 
  • gdal_csharp
  • gdalconst_csharp
  • ogr_csharp
  • osr_csharp
Import Namespaces

using OSGeo.GDAL;
using OSGeo.OGR;
using OSGeo.OSR;

Thursday, July 2, 2015

Building ArcMap’s Add-in using command line

Be The First To Comment
Couple of days I have been spending on creating a build automation for ArcMap’s Add-in using Jenkins . In order to accomplish that I need to able to build my visual studio add-in solution using MSBuild.exe.

When I try to build the solution using via command line –
msbuild.exe MyArcMapAddin.sln /t:Clean,Build

The console throws the following error- 
(PrePackageArcGISAddIn target) -> 
  C:\Program Files (x86)\MSBuild\ESRI\ESRI.ArcGIS.AddIns.targets(37,5): error MSB4062: The "ValidateAddInXMLTask" task could not be loaded from the assembly ESRI.ArcGIS.AddIns.SDK, Version=, Culture=neutral, PublicKeyToken=8fc3cc631e44ad86. Could not load file or assembly 'Microsoft.VisualStudio.Shell.9.0, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [SOLVED]

Monday, February 2, 2015

Getting into ESRI ArcGIS Add-in Development in .Net and ArcObjects

1 Comment
Recently, I landed into a project for ArcGIS/ArcMap Desktop add-in development using ArcObjects in C#. I am a newbie in both ArcObjects and add-in development. Googling through I got tons of snippets on the ArcMap add-in development, but I was not familiar with tailoring them to make a working application.

Then, I started to look after Youtube and Amazon to see if any good basic books or tutorials that gives me basic concepts on tailoring the  ArcObjects/.Net snippets. On youtube, I came across a video tutorials series on “.Net programming with ArcOjects”, an excellent tutorial on ArcObjects and .Net development with sample Gis data and codes to download  by Hussein Nasser. I watched all 15 episodes on add-in development using VB.Net.

From the Amazon, I bought a book entitled, “Beginning ArcGIS for Desktop Development using .NET ” by Pouria Amirian, with a blind believe on the reviewers, but it turned out the 
The book is wonderful! The author does an excellent job of explaining basic .NET concepts and tying them into ArcObjects.This book was long overdue and it seems like the author really took his time to ensure the content was organized in a logical way and concepts are thoroughly explained.

I would recommend this book and Youtube video to anyone who is interested in learning ArcObjects in .NET. The both author includes .NET code samples as well as the solutions in C#/VB. Lots of useful stuff in there and I think when you finished you will have a good starting point to ArcGIS Desktop development.

Friday, March 22, 2013

Good Book for GIS Beginners : Book II

Be The First To Comment

Understanding GIS: An ArcGIS Project Workbook, is a very user-friendly written book for those interested to begin using ESRI ArcGIS Desktop 10 or ArcGIS Desktop 10.1 using real data from the City of Los Angeles' Department of Public Works, Bureau of Engineering's Mapping Division, and Department of Recreation and Parks and to manipulate it using the power of GIS.

 Don't expect to become a GIS expert at the end because this is simply a good introduction to ArcGIS. The book guides the reader step-by-step, mouse-click-by mouse-click, decision-by-decision through a GIS project to determine for yourself which locations along the river are best suited for public recreational use in Los Angeles. At the end, you will have learned many of the fundamentals of GIS generally and ArcGIS specifically which aims at finding a suitable land parcel(s)for a new park area in Los Angeles .You use real data which comes in the companion DVD. When you reach the final stage in chapter 6 and follow all the careful steps to manually select the best areas and you learned why you do so, you discover in the following chapter (chapter 7) that you can do the same in a much quicker way by using a visual graphing tool, a marvel in my opinion of ArcGIS Desktop.

Tuesday, February 19, 2013

Python Scripting for ArcGIS

Be The First To Comment

Python Scripting for ArcGIS is a new text from Esri Press by Paul A. Zandbergen (2013). It isn’t the first Python book for the geospatial community or even focused on ArcGIS, but it is the first that has the Esri logo on it. Much like other recent books on Geo/Python we have seen, it focuses on integrating an introduction to Python with the industry specific materials. As Frank mentioned when he highlighted the book in a previous podcast, this allows users to gain exposure to Python, but it doesn’t fall back on the (in my opinion) bad habit of most programming texts of spending half of the book on the language and concepts before even getting to the application in the specific area. There is a time and place for that approach in Python specific books. When you add another software library to a book, then use it from the get go.

Sunday, January 13, 2013

Popular GIS Books

Be The First To Comment
Books Pro Cons
It provides solid guide to how geospatial analysis work, particularly with respect to GIS. The book emphasizes conceptual workflows and with basic math which is helpful for creating own code and also getting an understanding of what's happening under the hood in contemporary GIS. It is better to have an update because lots of changes in GIS software over last five years.
This book is for typical GIS user aspiring to design good maps. It is illustrating GIS map software and throughout with map samples in color which is especially useful for those who has little prior training or experience in map making. This is acceptable book for beginners but very little information of advanced users. It hardly touches on advanced cartographic representations.
This book explains the computational geometry and algorithms concisely and very readable. It emphasis on describing algorithms and data structures theoretically. It presents pseudo code with lots of figures that is very easy to understand and follow.

It's also worth reading for all computer scientists and mathematicians who are working on geometry.

This is good text/reference book for graduate course.
Focused on geometric computation and algorithm, very complicated for beginners, who does not have prior computer programming knowledge.

The various algorithms and concepts often used in this book are triangulation, indexing, calculating intersection, shortest paths etc.
The book illustrates the most common cartographic deceptions, and provides some excellent color guides. If you want to learn how to make influential maps for a cause, this is the book!. The reader can learn what to look for and how to avoid the inadvertent or unintentional 'lies'. Worth the effort! Basically, the book as an introduction to the science of cartography and targeted for prospective cartographer or decision making authority.
The book details the use of freely available open source mapping software and tools such as MapServer, GDAL, OpenEV, and PostGIS to create web gis and web maps.

Mostly focused on UMN Mapserver for web mapping and building web gis.
Not much technical discussion on how GPS databases work, how to decode GIS information.
The book is fairly shallow. It will give you a couple of basic examples of how to use some pieces of software, but for anything more complicated, you have to look elsewhere.

Wednesday, January 9, 2013

GIS Programming for GIS Analyst using Python

Be The First To Comment

Penn state has a nice collection of ArcGIS & Python programming step by step guide for their GEOG 485 students, which is also available for non-students via Penn state website.  This course’s is main aim is to make student able to use the Python scripting language to automate GIS tasks in ArcMap, assemble ArcView geoprocessing tools into models to solve GIS problems, and run the tools from scripts to automate GIS tasks. 

Although no previous programming experience is assumed, the course's target is to make a student as a GIS programmer/analyst in ArcGIS and Python environment. 

The course is basically divided into four parts:

Friday, December 21, 2012

Dissolve ERROR 999999: Error executing function. The Geometry has no Z values.

Be The First To Comment

Solution to Dissolve ERROR 999999: Error executing function. The Geometry has no Z values.
ArcToolbox>Repair Geometry and delete features with empty geometries
ArcToolbox > Conversion Tools>To Shapefile>Environments>General Settings>Output has Z values = Disabled, Output has M values = Disabled>OK>Input features/Output features>OK
ArcToolbox>Dissolve> happy :))


Thursday, October 11, 2012

Extract Raster Values from Points

The R blog article encourages me to write this solution to extract Raster values from points in R.

In geospatial analysis, extracting the raster value of a point is a common task. If you have few raster files or few points; you can extract the raster value by overlaying a point on the top of the raster using ArcGIS. What will you do, if you have hundreds of raster files and thousands of points? The easy solution is use loop in Python and ArcGIS. Is loop efficient to use? No. Can loop be avoided? Yes.

Then how? 

Follow the steps:
Step 1: Create a Raster stack or Raster brick of your raster files using “raster” package in R.
For example:
rasStack = stack(raster1, raster2,raster3 …rasterN)

Step 2:  Read point data, and convert them into spatial points data frame.
Sample: pointfile.csv
For example:
coordinates(pointCoordinates)= ~ LONGITUDE+ LATITUDE

Step 3: Extract raster value by points
For example:
rasValue=extract(rasStack, pointCoordinates)

Step 4:  Combine raster values with point and save as a CSV file.
For example:
write.table(combinePointValue,file=“combinedPointValue.csv”, append=FALSE, sep= ",", row.names = FALSE, col.names=TRUE)

Step 5: You should get the results as following table.
Result: combinedPointValue.csv
-0. 4


© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me