open ecw script

812 views
Skip to first unread message

nicholas....@tmr.qld.gov.au

unread,
Nov 3, 2011, 12:27:32 AM11/3/11
to mapi...@googlegroups.com

Hi all,

Someone asked me if I could share my script for generating a .tab file for an ecw, so here it is;

'File : open_ecw_v2
'Author : Nick Lawrence 14 Sept 2011
'Purpose : create a valid .tab file for a .ecw

Include "menu.def"
Include "mapbasic.def"
Include "icons.def"
Declare Sub Main
Declare Sub GetFileNames (byval sPath As String, byval sFilespec As String, sList() As String)
Define MAX_PATH 260
Type FILETIME
dwLowDateTime As Integer
dwHighDateTime As Integer
End Type
Type WIN32_FIND_DATA
dwFileAttributes As Integer
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Integer
nFileSizeLow As Integer
dwReserved0 As Integer
dwReserved1 As Integer
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Type ecw_record
s(6) As Integer
q As Logical
t(4) As Float
End Type
Define INVALID_HANDLE_VALUE -1
Declare Function FindFirstFile Lib "kernel32"
Alias "FindFirstFileA" (ByVal lpFileName As String, lpfindFileData As WIN32_FIND_DATA) As Integer
Declare Function FindNextFile Lib "kernel32"
Alias "FindNextFileA" (ByVal hFindFile As Integer, lpFindFileData As WIN32_FIND_DATA) As Integer
Declare Function FindClose Lib "kernel32"
Alias "FindClose" (ByVal hFindFile As Integer) As Integer

Sub Main
Dim in_file, in_dir, ecw_name, out_name, out_file As String
Dim q As String
Dim data_smallint As SmallInt
Dim data_int As Integer
Dim data_float As Float
Dim data_log As Logical
Dim datum, projection As String*10
Dim projchoice As SmallInt
Dim numcols, numrows As Integer
Dim sizex, sizey, posx, posy, blah As Float
Dim minx, miny, maxx, maxy As Float
Dim sTables() As String
Dim ecw_now, ecw_total As Integer


Print "************************************"
Print "create .tab files for ecw files"
Print "written by Nick Lawrence"
Print "Transport and Main Roads"
Print "************************************"

'set constants
q = Chr$(34)

'Dialog with user
in_file = FileOpenDlg("", "", "ECW", "Select ecw file to make a tab file")
If in_file = "" Then
Exit Sub
End If

in_dir = PathToDirectory$(in_file)
Call GetFileNames (in_dir, "*.ecw", sTables)

ecw_total = UBound(sTables)
For ecw_now = 1 To ecw_total
ecw_name = sTables(ecw_now)
If Left$(ecw_name, 1) In ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z") Then
out_name = Left$(ecw_name, Len(ecw_name)-4) + ".tab"
Else
out_name = "ecw" + Left$(ecw_name, Len(ecw_name)-4) + ".tab"
End If
out_file = in_dir + out_name
Print "ecw : " + ecw_name
Print "table : " + out_name
'extract data from ecw
Open File in_file
For Binary
Access Read
As #1
ByteOrder HIGHLOW
Get #1, 7, numcols
Get #1, 11, numrows
Get #1, 58, datum
Get #1, 74, projection
Close File #1
Open File in_file
For Binary
Access Read
As #1
ByteOrder LOWHIGH
Get #1, 26, sizex
Get #1, 34, sizey
Get #1, 42, posx
Get #1, 50, posy
Close File #1
'check that the returned results are not null
If (numcols = 0) Or (numrows = 0) Or (datum = " ") Or (projection = " ") Or (sizex = 0) Or (sizey = 0) Or (posx = 0) Or (posy = 0) Then
Print "The ecw file could not be processed."
Print "numcols : " + numcols
Print "numrows : " + numrows
Print "sizex : " + sizex
Print "sizey : " + sizey
Print "posx : " + posx
Print "posy : " + posy
Print "datum : " + datum
Print "projection : " + projection
Print "Please contact Geospatial Technologies."
Else
'check validity of datum and projection
projchoice = 0
If datum = "WGS84 " And projection = "GEODETIC " Then
projchoice = 1
End If
If datum = "GDA94 " And projection = "GEODETIC " Then
projchoice = 1
End If
If datum = "GDA94 " And projection = "MGA54 " Then
projchoice = 2
End If
If datum = "GDA94 " And projection = "SUTM54 " Then
projchoice = 2
End If
If datum = "GDA94 " And projection = "MGA55 " Then
projchoice = 3
End If
If datum = "GDA94 " And projection = "SUTM55 " Then
projchoice = 3
End If
If datum = "GDA94 " And projection = "MGA56 " Then
projchoice = 4
End If
If datum = "GDA94 " And projection = "SUTM56 " Then
projchoice = 4
End If
If projchoice = 0 Then
Print "The ecw file could not be processed."
Print "The datum/projection is not recognised."
Print "datum : " + datum
Print "projection : " + projection
Print "Please contact Geospatial Technologies."
Else
Print "numcols : " + numcols
Print "numrows : " + numrows
Print "sizex : " + sizex
Print "sizey : " + sizey
Print "posx : " + posx
Print "posy : " + posy
Print "datum : " + datum
Print "projection : " + projection
'create .tab file
minx = posx
miny = posy + (numrows * sizey)
maxx = posx + (numcols * sizex)
maxy = posy
Print "minx : " + minx
Print "miny : " + miny
Print "maxx : " + maxx
Print "maxy : " + maxy
Open File out_file
For Output
As #2
Print #2, "!table"
Print #2, "!version 300"
Print #2, "!charset WindowsLatin1"
Print #2, " "
Print #2, "Definition Table"
Print #2, " File " + q + ecw_name + q
Print #2, " "
Print #2, " Type " + q + "RASTER" + q
Print #2, "(" + minx + "," + maxy + ") ( 0, 0 ) Label " + q + "Pt 1" + q + ","
Print #2, "(" + maxx + "," + miny + ") (" + numcols + "," + numrows + ") Label " + q + "Pt 2" + q + ","
Print #2, "(" + maxx + "," + maxy + ") (" + numcols + ",0) Label " + q + "Pt 3" + q
Do Case projchoice
Case 1
Print #2, "CoordSys Earth Projection 1, 116"
Case 2
Print #2, "CoordSys Earth Projection 8, 33, " + q + "m" + q + ", 141, 0, 0.9996, 500000, 10000000 Bounds (-7745844.29605, 2035.05684549) (8745844.29605, 19997964.9432)"
Case 3
Print #2, "CoordSys Earth Projection 8, 33, " + q + "m" + q + ", 147, 0, 0.9996, 500000, 10000000 Bounds (-7745844.29605, 2035.05684549) (8745844.29605, 19997964.9432)"
Case 4
Print #2, "CoordSys Earth Projection 8, 33, " + q + "m" + q + ", 153, 0, 0.9996, 500000, 10000000 Bounds (-7745844.29605, 2035.05684549) (8745844.29605, 19997964.9432)"
End Case
Close File #2
End If
End If
Print "************************************"
Next

Print "Finished!"

End Sub

Sub GetFileNames (ByVal sPath As String, ByVal sFilespec As String, sList() As String)
Dim hFindFile, nStatus As Integer
Dim f As WIN32_FIND_DATA
Dim i As Integer

hFindFile = FindFirstFile (sPath + sFilespec, f)
If hFindFile <> INVALID_HANDLE_VALUE Then
Do
If (f.dwFileAttributes \ 16) Mod 2 = 0 Then
i = i + 1
ReDim sList(i)
sList(i) = f.cFilename
End If
nStatus = FindNextFile (hFindFile, f)
Loop While nStatus = 1
End If
nStatus = FindClose (hFindFile)
End Sub


Kind regards,



Nicholas Lawrence

Geographic Information Systems Coordinator
| Geospatial, Road Assets & Design
Engineering & Technology
Division | Department of Transport and Main Roads



Floor 6 | Spring Hill Office Complex | 477 Boundary Street | Spring Hill Qld 4000
GPO Box 1412 | Brisbane Qld 4001

P:
(07) 38342477 | F: (07) 38342998
E:
nicholas....@tmr.qld.gov.au
W:
www.tmr.qld.gov.au
Tomorrow's Queensland: strong, green, smart, healthy and fair – www.towardQ2.qld.gov.au

P
| Please consider the environment before printing this email

***********************************************************************
WARNING: This email (including any attachments) may contain legally
privileged, confidential or private information and may be protected by
copyright. You may only use it if you are the person(s) it was
intended to be sent to and if you use it in an authorised way. No one
is allowed to use, review, alter, transmit, disclose, distribute, print
or copy this email without appropriate authority.

If this email was not intended for you and was sent to you by mistake,
please telephone or email me immediately, destroy any hardcopies of
this email and delete it and any copies of it from your computer
system. Any right which the sender may have under copyright law, and
any legal privilege and confidentiality attached to this email is not
waived or destroyed by that mistake.

It is your responsibility to ensure that this email does not contain
and is not affected by computer viruses, defects or interference by
third parties or replication problems (including incompatibility with
your computer system).

Opinions contained in this email do not necessarily reflect the
opinions of the Department of Transport and Main Roads,
Maritime Safety Queensland or endorsed organisations utilising
the same infrastructure.
***********************************************************************

 

Peter Horsbøll Møller

unread,
Nov 3, 2011, 1:57:51 AM11/3/11
to mapi...@googlegroups.com
Thanks for sharing Nicholas!
I have had a few requests for exactly this over the past few weeks so this comes at the right time!

Peter Horsbøll Møller
Pitney Bowes Business Insight - MapInfo


Andy Harfoot

unread,
Nov 3, 2011, 8:50:11 AM11/3/11
to mapi...@googlegroups.com
Thanks Nicholas,

Andy

nicholas....@tmr.qld.gov.au wrote:
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en


-- 
Andy Harfoot

GeoData Institute
University of Southampton
Southampton
SO17 1BJ

Tel:  +44 (0)23 8059 2719
Fax:  +44 (0)23 8059 2849

www.geodata.soton.ac.uk

---------------------------------------
For further information about GeoData's
Training Courses, please visit:
           www.gis-train.com
---------------------------------------
Message has been deleted

college.atlas

unread,
Jul 2, 2012, 10:09:55 PM7/2/12
to mapi...@googlegroups.com
Taking absolutely nothing away from Nick's generosity but I have been attempting to use the posted code and believe there are a couple of typos. I've made one slight modification to make this code more durable.

I often think little typos like this are deliberate. Like when my Aunty would ask my Mum for her sponge cake recipe, Mum would slightly alter it so that no matter how hard my Aunty tried she could never bake the nicer cake. 

In the case of code it certainly makes you look carefully and understand it more so that you don't just take and use but also learn. I can respect that.

Below is a copy of the code with coloured alterations.

Again thanks for the generosity Nick. Greatly appreciated.

Open File ecw_name 'in_file if in_file is used you only get one set of registration coords 
'for all images in the directory. So this will work for one but not a batch. ecw_name is incremented by the ecw_now counter.

For Binary
Access Read
As #1
ByteOrder HIGHLOW
Get #1, 7, numcols
Get #1, 11, numrows
Get #1, 58, datum
Get #1, 74, projection
Close File #1
Open File ecw_name 'in_file
If datum like "%GDA94%" And projection like "%MGA55%" Then 'This test failed on my ECW's. The EXACT string was not be matched so I replaced it with a slightly more fuzzy match.

Ross Nixon

unread,
Sep 4, 2013, 2:27:12 AM9/4/13
to mapi...@googlegroups.com
I wonder if someone can help me interpret a hex view at the top of an .ecw file for me (image here: http://www.dropbox.com/s/jd4mc0c390zxa3i/hex_at_top_of_ecw.png ).
What datum and projection values do I need to add to Nick's MapBasic script above?
All I know is that I want the cartesian/flat NZTM(GD2000) values to use.
I think I'll manage the projection string further down the script, but not the datum and projection near the top.

Thanks
Ross

Tony Pilkington

unread,
Sep 5, 2013, 3:53:45 AM9/5/13
to mapi...@googlegroups.com
Ross
 
This is based on my original attempt (2002)  to get coordinates out of a ecw file. I never saw an ecw file with a coordsys clause filled in ; it was always RAW.
So I built a system in which the user had a table (vector or raster) open in the desired coordinate system. This was used by the MBX to get the coordsys clause.
It may nor work with certain projections; it has an inbuilt hatred of certain Australian datums; MI has since cured this paranoia.
Modus Operandi:
using the dos window build a list of the ecw files
dir / s /o /p  *.ecw  >  ecw_list.txt
 
check (and edit) this list as necessary.
 
Create and run the mbx, locate the ecw_list text file and the tab files will be written to the same folder at the ecw images.
 
Good luck.
 
Tony
--
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en
 
---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No virus found in this message.
Checked by AVG - www.avg.com
Version: 2013.0.3392 / Virus Database: 3222/6639 - Release Date: 09/04/13

Tabs_from_ECW_Images_v3.MB

Ross Nixon

unread,
Sep 5, 2013, 5:44:16 PM9/5/13
to mapi...@googlegroups.com, Tony Pilkington
Hi Tony

Great! You are still around!
I found your original message from 2004, but the attachment was no longer available.

Just one typo in your batch command. It is /b rather than /p.
Have compiled the script and run it over a folder of .ecw files, and it worked well. :-)

Regards
Ross
Reply all
Reply to author
Forward
0 new messages