Home arrow GIS Data & Resources arrow Scripts and Code
GIS-Related Scripts and Programming Code

This section provides a venue for sharing GIS-related programming code and custom scripts in a variety of languages. Posts can be viewed by category or as a whole in blog format (below).

Category Links:

GIS Scripts and Code (Weblog format, All Posts):




VBA: Hunting Duplicate Polygons in a Large Polygon Dataset PDF Print E-mail

Written by Bert Granberg,

It was recently brought to my attention that one of the SGID's large vegetation data layers (SWReGAP) had many duplicate polygons. Sure enough, in some locations spatial queries and identify operations would return duplicate features...same attributes, same geometry, same area, same perimeter, BUT two or more different object IDs.

These errors were likely introduced during the raster to vector conversion process after AGRC received the original data from its creators at Utah State.

As bad luck would have it, there was no discernable pattern to isolate the duplicates and the dataset had ONLY 3.6 million features. Since this is polygon dataset is supposed to be planar (no polygons should overlap with other polygons), I went ahead on the assumption that if two features had the same centroid, area, and perimeter, then one of them was a duplicate. Since the data was already in a geodatabase, I already had access to an area and perimeter (length) attribute values for each polygon. I just needed to add a text field to store the centroid coordinate, do a three tiered sort, and iterate through the sorted records to identify duplicates.

Not sure if this is the best approach but here is what I did to find these features.

  1. Add a field called LabelPt, (Text 100) and calculate its value to the concatenation of the X and Y coordinate resulting from the ArcObjects method IArea.LabelPoint which guarantees that the point returned (unlike IArea.Centroid) will be within the given polygon. My LabelPt values (in UTM coords) look like this --> "379891.217607 4182762.234845"
  2. Run a Visual Basic for Applications script that first sorts by LabelPt attribute value (from Step 1), then by area, then by perimeter. Then a looping structure looks through all the sorted features checking to see which features have the same LabelPt, area, and length attribute combination as the sorted feature before it. These are marked and added to a selected set.
  3. I had problems running out of memory so I run this script in batches of 1 million records and record the last LabelPt value after each batch of 1 million records. I stop the script after a million records have been processed and then right click on the layer and 'Create a Layer from Selected Records'. Then I open up the ArcGIS command line and use the DeleteFeatures command against the new selection layer.
  4. After the I then modify the filter on the TableSort object for the next run to look for everything greater than the LabelPt value for the previous million records.
The corrected SWReGAP vegetation layer will be available in the new SGID 9.3 Database which will go online in a week or so.
Comment on this Article
06, May. 2009
Last Updated ( 06, May. 2009 )
Read more...
 
Python! SQL With File Geodatabase... PDF Print E-mail

Written by Kevin Bell,

Have you ever wanted to simply plug a SQL statement into a GIS tool to get the results into your GIS, but the SQL that you need to use is 400 lines, and the existing tools in ArcGIS don't really accept standard SQL in a plug n chug manner? 

Here's some code that should get you started.  With this code I start out by deleting all of the features out of a fgdb, then reloading the data.

 pyodbc returns records as a list, so you index the list items to feed your insert cursor.

Comment on this Article
06, May. 2009
Last Updated ( 06, May. 2009 )
Read more...
 
C#: Dojo.io.iframe.send goodness with ASP PDF Print E-mail

Written by Steve Gourley,

Ever since ESRI built its RESTful API on top of dojo, I've been happily using the dojo framework regularly. Dojo is super fun and makes tedious javascript tasks really trivial. Instead of writing your own DHTML/javascript popup window or using ESRI floating panels dojo has a dijit dialog that handles all of that for you in 1 line of code. We've been using the dijit.dialog for showing instructions, tool sets, etc on our mapping pages. The last requirement I worked on was shapefile download and you can check that out here and the next requirement oddly enough is shapefile upload. 

I figured I'd throw an asp fileupload control in a dijit.dialog and be on my way in minutes. Turns out asp buttons don't like to postback when they're inside a dialog panel. I didn't spend any time trying to figure out why and moved on to the next outlet - dojox.form.FileInput.

Does anyone remember the iframe that was super popular in 1990?  Well, it turns out there is still a useful hack for iframes and that is posting a file to a server in an asynchronous manner. Dojo has a few built in libraries for doing asynchronous javascript calls and dojo.io.frame is one of them.  

In a nutshell, you put an input element of type file in a form element. Then you call the dojo.io.frame.send method passing the form node as a parameter and it posts asynchronously to the server.

dojo.io.iframe.send(

{

    url: "../FileUpload.ashx",

    handleAs: "json",

    method: "post",

    form: form,

    handle: function(data, ioArgs)

    {

        console.dir(data);

    },

    error: function(error)

    {

        console.debug(error);

    }

}

It wasn't as easy as that in my situation. The website I'm working on is using asp masterpages which wrap the content pages in a form. ASP pages like to only have one form per page at least with the runat='server' tag. So for the longest time my form data was not being posted to the server. After some firebug console.logging I realized that the form element I was posting was null. I did a little more firebug inspecting and noticed that the static html form that I placed in my content panel wasn't there. Again I didn't spend any time figuring out why and took the detour with javascript.

Dojo makes DOM manipulation really simple. I dynamically created the form element and inserted it where I wanted it. I also dynamically created the FileInput control and placed it inside the form along with a button to execute the iframe.send() request.

On the server side I just grab the HttpFileCollection and save the file.

HttpFileCollection fileCollection = context.Request.Files;

HttpPostedFile file = fileCollection["image"];

 

file.SaveAs(@"C:\arcgisserver\arcgisoutput\" + file.FileName);

 

context.Response.Write("<textarea>{file: \"" + file.FileName + "\"}</textarea>");

 If you think it's weird to return a json object within a textarea it is. It's for security reasons though so you'll just have to deal with it.

Comment on this Article
04, May. 2009
Last Updated ( 04, May. 2009 )
 
C# - Create Shapefile Programmatically PDF Print E-mail

Written by Steve Gourley,

When I first starting looking into creating a shapefile programmatically, I thought it would be a piece of cake since there is so much reference code out there from VBA GIS users. ArcGIS Server is not a VBA editor within ArcMap though and there were a few hurdles to jump over to make this work. Below is the code, I hope it makes your day. It would have made mine to find this somewhere.

Notes: If you get this error code System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040258 or System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80040351

try creating your objects with the IServerContext.CreateObject method.

if you get this error code System.Runtime.InteropServices.COMException: Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

Check that your CLSID string is correct. Good luck finding a list of these - it's mostly an educated guessing game.

 

 

Comment on this Article
28, Apr. 2009
Last Updated ( 29, Apr. 2009 )
Read more...
 
<< Start < Prev 1 2 3 4 5 6 7 8 9 10 Next > End >>

Results 17 - 24 of 130

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for