1

I’m looking for a ‘quick’ (in terms of my time, not computer time) solution to converting a whole bunch of ESRI Ascii Grid files into MapInfo TAB files consisting of a grid of points with XYZ fields. I’ve looked at GDAL/OGR but as far as I can tell GDAL deals specifically with Raster/Grid data (i.e. the source data I’ve got) and OGR deals with vector data (my desired output). I can’t seem to find any command to go from grid to vector though.

I have the following software available: MapInfo Pro, Global Mapper v15, QGIS, GDAL

I'm aware that both Global Mapper and QGIS are scriptable but I've little to no experience in writing scripts for either of these so am avoiding that given the time it would take to a) learn what I'm doing and b) actually write the script.

I have considered implementing the conversion in MapInfo's scripting language, MapBasic, with which I am quite familiar, but it tends to be a bit slow and given the large number of files I have I'd rather find something a bit quicker. I also don't have a huge amount of time to spend on writing up a script for this so would rather not reinvent the wheel if something already exists.

  • Interesting conversion, @T_Bacon. Don't you risk ending up with a massive tab file with point data? – Peter Horsbøll Møller Jan 25 '17 at 19:25
  • 1
    As the individual tiles are 1km squares I won't end up with a massive tab file, but I will end up with a whole lot of smaller ones. The conversion was requested by a client, not sure what they're ultimate plan is for the files. – T_Bacon Jan 26 '17 at 16:28
2

This is a common workflow to convert a raster to a point vector file. It should be easy enough to batch the workflow with bash or python. The raster is converted to a CSV with gdal2xyz.py, a VRT is constructed for the CSV, and that VRT is translated into the final point vector file with ogr2ogr.

$ gdal2xyz.py -csv input.ascii lat_lon_val.csv
$ # lat_lon_val.csv needs a header for GDAL's CSV driver
$ echo "Latitude,Longitude,value" > header.csv
$ # combine header and CSV into a new file
$ cat header.csv lat_lon_val.csv > input.csv
$ # create the VRT 
$ vim input.vrt


<OGRVRTDataSource>
    <OGRVRTLayer name="input">
        <SrcDataSource>input.csv</SrcDataSource>
        <GeometryType>wkbPoint</GeometryType>
        <LayerSRS>WGS84</LayerSRS>
        <GeometryField encoding="PointFromColumns" x="Longitude" y="Latitude"/>
        <Field name="value" src="value" type="Integer"/>
    </OGRVRTLayer>
</OGRVRTDataSource>


$ ogrinfo input.vrt
$ # all good if no errors!
$ ogr2ogr -f "MapInfo File" output.tab input.vrt
$ # remove middleman files
$ rm input.vrt input.csv lat_lon_val.csv

The major part of the process that need to be parameterized is the VRT construction. You need to replace the OGRVRTLayer name and SrcDataSource.

  • Thanks Logan, this looks like it could work. I've scripted this into a batch file but am having some trouble getting gdal2xyz to run. It's currently failing to import _gdal (a dll file from what I can see). Any ideas on how I can fix this error? – T_Bacon Jan 24 '17 at 8:48
  • Once I'd set all the environment variables correctly I managed to get a version of this working through the command line on a Windows machine. Will post the working code below. – T_Bacon Jan 24 '17 at 12:44
3

The final batch file, based on Logan Byers answer, which I managed to get working on a Windows machine.

I should note that I had to amend the output of gdal2xyz.py. For some reason the version I had (installed with FWTools 2.4.7) would not accept the -csv option as valid syntax and you can see it is omitted from the code below. I had to open the gdal2xyz.py file and amend the output code to delimit with commas rather than a space. Once I'd done this the script worked fine.

@ECHO OFF

setlocal EnableDelayedExpansion

cd "c:\"
cd "..somepath..\LiDAR\"

FOR /R %%F IN (*.asc) DO ( 
    gdal2xyz.py %%F lat_lon_val.csv

    rem lat_lon_val.csv needs a header for GDAL's CSV driver
    set "var=X,Y,value"
    echo !var! > header.csv

    rem combine header and CSV into a new file
    copy /B header.csv+lat_lon_val.csv input.csv

    rem create the VRT 
    set "var=<OGRVRTDataSource>"
    echo !var! > input.vrt

    set "var=    <OGRVRTLayer name="input">" >> input.vrt
    echo !var! >> input.vrt

    set "var=        <SrcDataSource>input.csv</SrcDataSource>"
    echo !var! >> input.vrt

    set "var=        <GeometryType>wkbPoint</GeometryType>"
    echo !var! >> input.vrt

    set "var=        <LayerSRS></LayerSRS>"
    echo !var! >> input.vrt

    set "var=        <GeometryField encoding="PointFromColumns" x="X" y="Y"/>"
    echo !var! >> input.vrt

    set "var=        <Field name="value" src="value" type="Real"/>"
    echo !var! >> input.vrt

    set "var=    </OGRVRTLayer>"
    echo !var! >> input.vrt

    set "var=</OGRVRTDataSource>"
    echo !var! >> input.vrt

    rem all good if no errors!
    ogrinfo input.vrt

    ogr2ogr -f "MapInfo File" -a_srs EPSG:27700 %%~nF.tab input.vrt    
)

rem remove middleman files
del input.vrt
del input.csv 
del lat_lon_val.csv

Pause

@ECHO ON

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.