Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4332732
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:17:49+00:00 2026-05-21T10:17:49+00:00

My input raster consists of multiple layers, each with an image area surrounded by

  • 0

My input raster consists of multiple layers, each with an image area surrounded by no data values. These layers don’t overlap completely, and I am trying to output a file which consists of only the intersection of all bands (the zone which has no NoData values on any layer).

The following works for a few layers, but not for the 50+ that I have in my real files (at least 3000×3000 pixels):

library(raster)

fin = "D:\\temp\\all_modes.pix"
fout = "D:\\temp\\test.pix"

inbands = stack(fin, bands = c(3:20))
NAvalue(inbands) = 0

# Not great:
#out = all(is.na(inbands) == FALSE) * inbands
#writeRaster(out, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)

# A little better:
#mymask = all(as.logical(inbands))
#mask(inbands, mymask, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)

# Even better, don't need to keep everything (but still not efficient):
#trim(all(as.logical(inbands)) * inbands, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)

# Even better, calculations get smaller as we progress (is it possible to do even better?)
for(i in 1:nlayers(inbands)){
 band_i = subset(inbands, i)
 inbands = trim(as.logical(band_i) * inbands)
}
writeRaster(inbands, filename=fout, format="PCIDSK", dtype="INT2U", overwrite=TRUE, NAflag=0)

Any ideas on how to do this more efficiently / get it working with a large number of layers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:17:49+00:00Added an answer on May 21, 2026 at 10:17 am

    Thanks for the answers, they gave me good ideas. I came up with this, which is much faster:

    myraster = stack(fn, bands) # You get the idea
    NAvalue(myraster) = 0
    
    # Tranform to 1 where there is data    
    logical_raster = as.logical(myraster)
    
    # Make a raster with 1 in the zone of intersection
    a = subset(myraster, 1)
    values(a) = TRUE
    for(i in 1:nlayers(myraster)) {
      a = a & logical_raster[[i]]
    }
    
    # Apply the "mask" and trim to intersection extent
    myraster = myraster * a
    intersect_only = trim(myraster)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some input data that sometimes appears with &nbsp characters on the end.
Input: The customer claims that the application (.NET) when querying for some data returns
INPUT <logs> <logentry revision=648> <author>nshmyrev</author> <date>2008-09-21T19:43:10.819236Z</date> <paths> <path action=M>/trunk/po/ru.pi</path> </paths> <msg>2008-09-21 Nickolay V. Shmyrev
Input: 1) A huge sorted array of string SA; 2) A prefix string P;
$('input[type=checkbox]').unbind().click(function(e){ $(this).attr('checked', true) return false; }); I NEED to return false because I have
As input I get an int (well, actually a string I should convert to
here is the input i am getting from my flash file process.php?Q2=898&Aa=Grade1&Tim=0%3A0%3A12&Q1=908&Bb=lkj&Q4=jhj&Q3=08&Cc=North%20America&Q0=1 and in
If user input is inserted without modification into an SQL query, then the application
Which type of input is least vulnerable to Cross-Site Scripting (XSS) and SQL Injection
I have one text input and one button (see below). How can I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.