X

NV5 Geospatial Blog

Each month, NV5 Geospatial posts new blog content across a variety of categories. Browse our latest posts below to learn about important geospatial information or use the search bar to find a specific topic or author. Stay informed of the latest blog posts, events, and technologies by joining our email list!



New ENVI Agent, IDL Agent, and GeoAgent Quick Guides

New ENVI Agent, IDL Agent, and GeoAgent Quick Guides

6/9/2026

The recent release of ENVI® Agent, IDL® Agent, and GeoAgent™ revolutionize how users interact with geospatial software. These agentic AI applications act as partners to plan, simplify, and execute complex workflows. Knowing where to start can be challenging for new users. To this end, we developed three new quick guides to... Read More >

Introducing NISAR Data Support

Introducing NISAR Data Support

6/5/2026

The release of ENVI® SARscape 6.3 in April 2026 includes preliminary support for NASA-ISRO SAR (NISAR) data. The NISAR mission is a joint Earth-observing satellite project between NASA and the Indian Space Research Organization designed to monitor changes in the planet’s land and ice surfaces using advanced radar imaging. It... Read More >

Monitoring Illegal Mining in the Amazon: Turning Persistent Data Into Actionable Insight

Monitoring Illegal Mining in the Amazon: Turning Persistent Data Into Actionable Insight

5/28/2026

Illegal mining over decades has constituted one of the most persistent and complex socio-environmental problems in the Brazilian Amazon. In recent years, with the increasingly intensive use of mechanized extraction, the associated environmental impacts—such as deforestation, intense soil disturbance, river siltation, and mercury... Read More >

From Answers to Action: Why ENVI and IDL Agents Go Beyond General AI

From Answers to Action: Why ENVI and IDL Agents Go Beyond General AI

4/20/2026

As generative AI tools like Claude and Gemini continue to gain traction, many organizations are asking the same question: Can general purpose AI actually support real geospatial workflows, or does it stop at surface-level answers? That question was front and center in our recent webinar, Meet Your New Partners in Science: ENVI... Read More >

Mapping Earthquake Deformation in Taiwan With ENVI

Mapping Earthquake Deformation in Taiwan With ENVI

12/15/2025

Unlocking Critical Insights With ENVI® Tools Taiwan sits at the junction of major tectonic plates and regularly experiences powerful earthquakes. Understanding how the ground moves during these events is essential for disaster preparedness, public safety, and building community resilience. But traditional approaches like field... Read More >

1345678910Last
«June 2026»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
21601 Rate this article:
No rating

Bridge to Bridge (R to Python to IDL)

Anonym

Within ENVI and IDL there are many different classification schemes and functions that can be used to aid in analyzing your data (ENVI classification documentation). However, on occasion you may notice that we have not had a chance to code in your favorite ones yet or perhaps the method you wish to use is so cutting edge that it is a little ahead of our development cycle. If that is the case then there is a chance that the method you desire may be found within the ever-expanding R statistical package.  As you may have read last week on the IDL Data Point blog we are now able to call R though the use of the IDL to Python Bridge! This week, I wanted to show you another example of how this can be done and some of the tricks to get you going.

If you have not followed the steps on setting up your IDL, Python, and R environments yet, it may be a good idea to do that now. Once you are all up and running we can get into the meat of this post. The purpose of this post is not to go into extensive detail on all of the nuances of these bridges but rather show a fun example of how to visualize a classification tree and point out a few pitfalls along the way.

To begin with, I simply pulled up some data, subsetted it and did a manual classification on the data.  I chose 4 different classes: NPV, VEG, Urban, and Water.

;Start ENVI

e = envi(/current)

if ~OBJ_VALID(e) then e = envi()

 

; Open the test image

file1 = FILEPATH('qb_boulder_msi', ROOT_DIR=e.ROOT_DIR, $

  SUBDIRECTORY = ['data'])

oRaster = e.OpenRaster(file1)

 

; Subset the image

oSubSet = oRaster.Subset(Sub_Rect = [207,705,420,838])

 

; Get the offset for the imagery

xo = oSubset.METADATA['X START']

yo = oSubset.METADATA['Y START']

 

; Pullout the ROI Data

Water = oSubset.GetData(Sub_Rect = [300-xo,800-yo,312-xo,808-yo], interleave='bip')

NPV = oSubset.GetData(Sub_Rect = [242-xo,758-yo,249-xo,765-yo], interleave='bip')

Urban1 = oSubset.GetData(Sub_Rect = [321-xo,736-yo,326-xo,740-yo], interleave='bip')

Urban2 = oSubset.GetData(Sub_Rect = [363-xo,713-yo,366-xo,718-yo], interleave='bip')

Veg = oSubset.GetData(Sub_Rect = [369-xo,738-yo,372-xo,742-yo], interleave='bip')

roi_data = list(Water,NPV,Urban1,Urban2,Veg)

I then structured the data in a way that allows for easier ingestion into the R environment.

; Get the size of the output array

npts = 0

for i = 0 , n_elements(roi_data)-1 do npts = npts + product((size(roi_data[i], /DIMENSIONS))[1:2])

training_data = intarr(oSubSet.nb,npts)

class = strarr(npts)

classes = ['Water', 'NPV', 'Urban', 'Urban', 'Veg']

 

;build the training data

count = 0

for i = 0 , n_elements(roi_data)-1 do begin

  training_data[*, count : count + product((size(roi_data[i], /DIMENSIONS))[1:2])-1] = reform(roi_data[i], oSubSet.nb, product((size(roi_data[i], /DIMENSIONS))[1:2]))

  class[count : count + product((size(roi_data[i], /DIMENSIONS))[1:2])-1] = classes[i]

  count = count + product((size(roi_data[i], /DIMENSIONS))[1:2])

endfor

 

; Collect the input info

classes = strjoin(class,",",/SINGLE)

band1 = training_data[0,*]

band2 = training_data[1,*]

band3 = training_data[2,*]

band4 = training_data[3,*]

Next comes the tricky part.  I called the rpy2 object from inside python and returned the robject to IDL.

; Get Python ready for the new R DataFrame

!null = Python.run('import rpy2.robjects as robjects')

robjects = Python.robjects

Once I had R all ready to go within Python, I wrote some simple R code on the fly within IDL that would allow me to visualize the different band thresholds that made up my manual image classification.

; Define the R function

train_rf = "robjects.r('''" + $

  "train_rf <- function(band1, band2, band3, band4, classes) { \n" + $

  "require(rpart) \n" + $

  "classes = c(unlist(strsplit(classes,','))) \n" + $

  "train_df = data.frame(band1, band2, band3, band4, classes) \n" + $

  "fit = rpart(classes ~ band1 + band2 + band3 + band4, method='class', data=train_df) \n" + $

  "plot(fit, uniform = TRUE, main = 'Classification Tree for Training Data') \n" + $

  "text(fit, use.n=TRUE, all=TRUE, cex=.8) \n" + $

      "}" + $

       "''')"

One possible hang-up here is that R must have the rpart package installed in order for this code to run. I installed this in the main R library directory to make sure that any user on my machine could find this package (i.e. C:\Program Files\R\R-3.1.3\library). As long as this package has been installed the “require(rpart)” portion of the code should not give you any trouble. 

Finally, you will need to publish the newly defined train_rf function up to Python.

!null = Python.run(train_rf)

 

Once it has been recognized by Python, you can bring it into IDL and run it as if it were a native function.

train_rf = robjects.globalenv['train_rf']

 

; Call the r function from within IDL

result = train_rf(band1, band2,Band3, band4, classes)

When you run the code, it should generate something that looks like this!

Please login or register to post comments.