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
***********************************************************************
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.
***********************************************************************
--
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 ---------------------------------------
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