I have a .bil file, which has interleave = bsq in the hdr file.

I'm having trouble finding any insight into how I would essentially do a lookup on the file.

I have no problem reading a specific chunk of bytes randomly from the file, but am unsure of how I would go about converting a lat/lon pair to a specific offset within the file. I've only seen solutions in R, but ideally I'd like to be able to do this in python or javascript, using proj4 or something to that effect.

Any advice?


BSQ is band sequential. The GDAL doc is here

Extract from ESRI whitepaper: Band sequential format stores information for the image one band at a time. In other words, data for all pixels for band one is stored first, then data for all pixels for band two, and so on. in http://downloads.esri.com/support/whitepapers/other_/eximgav.pdf on page 8.

If there's only one band there is no difference between BIL and BSQ.

If the NBITS isn't specified then it can be calculated by the size of file (in bytes) / (NROWS * NCOLS). This should give you either 8, 16 or 32. If it's 32 it may be float or 32 bit integer (long), you will need this in bytes 1, 2 or 4 respectively.

The hook point is usually upper left, so you find the difference from your coordinate to the reference point (in cells):

Row = int( (ULYMAP - Your.Y) / YDIM )
Col = int( (Your.X - ULXMAP) / XDIM )

That is assuming your point is in the same spatial reference as the image, if not project the point - or if all of your requests are going to be in the same spatial reference that's different to the raster consider projecting the raster.

With the row and column known calculating the location in file is (Col * NCOLS * bytesPerCell) + Row * bytesPerCell

  • This is information I've already come across, I'm aware that the grid is stored as bands, and my file in particular has only one band, and no bit size listed so I assume 8bit.My problem though is converting a particular coordinate to this grid system. i.e. converting 34° N, 118.25° W to a specific offset in the file. I have no idea how one would go about this. – derrrrrick May 14 '14 at 23:07
  • Oh, I see. I will edit the answer. – Michael Stimson May 14 '14 at 23:23
  • Ah that's perfect. I didn't realize it would be so simple, thanks! – derrrrrick May 15 '14 at 5:54
  • You're welcome. Be careful of 'out of bounds' requests: if row < 0 or row > NROWS or col < 0 or col > NCOLS - can cause some odd results! – Michael Stimson May 15 '14 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.