MapBasic export to Shapefile

79 views
Skip to first unread message

empirepd

unread,
Oct 6, 2021, 7:01:04 PM10/6/21
to MapInfo-L
I have a MI tab file with a bunch of individual regions in it. I want to export each of these records as individual Shapefiles. I was going to do this in MapBasic code in a loop, but I can't find the Save As/Export function for Shapefiles in the reference guide. I thought I had done this years ago in some code. Did MI remove this function or am I just missing something? Or is there another way to do this within MI? I only know of the Universal Translator which I don't believe I can do this in (other than creating individual.TAB for each region and then batch processing).

Thanks,
Sam

Bob Fortin

unread,
Oct 7, 2021, 5:16:46 PM10/7/21
to MapInfo-L
Hi Sam,

V2021 released this week can export to any GDAL/OGR format including shape using an updated Export mapbasic statement, so you could do it in a MapBasic program.

Prior to that in v2019.3 you could use a Python script to accomplish it via GDAL/OGR. There is a sample that shows exporting to GeoJSON "ExportToGeoJsonGDAL.py" in the PythonQuickStart app on the MapInfo Marketplace.
You would have to modify it to use the SHP driver instead and then insert your loop to pull out one feature a time to write to its own file, but it is do-able.

I am not aware of any other way but maybe others have some tricks.

-Bob

Peter Horsbøll Møller

unread,
Oct 8, 2021, 1:56:31 AM10/8/21
to mapi...@googlegroups.com

You can also use Universal Translator.

You can run UT, or the FME Quick Translator, from a bat file.

 

I haven’t tried that with shape but below is an example I have used to convert to DWG:

 

     'Translation is two stage process:

     '(1) Generate Semantic mapping\Control file

     '(2) Run Mapping file through translator (IMUT.exe)

 

     'To Generate the mapping file:

     If SystemInfo(SYS_INFO_MIVERSION) >= 1200 Then

           sCmdExe = Chr$(34) & ProgramDirectory$()+ "UT\fme.exe" & Chr$(34)

     Else

           sCmdExe = Chr$(34) & ProgramDirectory$()+ "UT\IMUT.exe" & Chr$(34)

     End If

 

     If FileExists(FILEWindowsTempFolder() & "__UT__.bat") Then

           Kill FILEWindowsTempFolder() & "__UT__.bat"

     End If

     If FileExists(FMEQTGetLogFileName()) Then

           Kill FMEQTGetLogFileName()

     End If

 

     Open File FILEWindowsTempFolder() & "__UT__.bat"

           For Append

           As 234

           CharSet "CodePage850" 'This will match the code page of the "dos prompt"

 

     Print #234, sCmdExe

           + " CFGenerate"

           + " " + sInputFormat + " " + sDestFormat

           + " " + chr$(34) + sInputFile + chr$(34)

           + " " + chr$(34) + sMappingFile + chr$(34)

           + " LOG_STANDARDOUT " + sLogFileOut

           + " LOG_FILENAME " + Chr$(34) + sLogFileName + chr$(34)

           + " LOG_APPEND " + chr$(34) + sLogAppend + chr$(34)

           + " --Dest_ATTR " + chr$(34) + sDestAttr + chr$(34)

           + " --Dest_ATTRKIND " + chr$(34) + sDestATTRKIND + chr$(34)

           + " --Dest_REL " + chr$(34) + sDestReleaseVer + chr$(34)

     Print #234, sCmdExe

           + " " + chr$(34) + sMappingFile + chr$(34)

           + " SourceDataset " + chr$(34) + sInputFile + chr$(34)

           + " --DestDataset " + Chr$(34) + sOutputFile + chr$(34)

           + " --_ATTRKIND " + sDestATTRKIND

           + " --_REL " + sDestReleaseVer

           + " --_EXTENSION " + sExtension

           + " --_BASENAME " + sBaseName

           + " --_FULLBASENAME " + sFullBaseName

           + " LOG_STANDARDOUT " + sLogFileOut

           + " LOG_FILENAME " + Chr$(34) + FMEQTGetLogFileName() + chr$(34)

     Close File #234

 

     If SYSExecuteAndWait(FILEWindowsTempFolder() & "__UT__.bat", SW_SHOWNORMAL) Then

     End If

 

You can find the full source code for the export to DWG on Github in the file FMEQuickTranslatorLib.MB

 

Safe has documentation for the Command Line here: https://docs.safe.com/fme/2020.0/html/FME_CommandLine/

There’s also an old documentation for Universal Translator with documentation for the Command Line tool here: https://docplayer.net/21398922-Universal-translator-11-0-user-guide.html

 

Be aware that these days we don’t use IMUT.exe but FME.exe.

And that the UT documentation is for v11 so parameters might have changed.

 

It is easier with our new GDAL/OGR support as Bob mentioned earlier

 

Peter Horsbøll Møller

www.precisely.com

 

Peter Horsbøll Møller
Principal Sales Engineer - Distinguished Engineer

 

From: mapi...@googlegroups.com <mapi...@googlegroups.com> On Behalf Of Bob Fortin
Sent: 7. oktober 2021 19:56
To: MapInfo-L <mapi...@googlegroups.com>
Subject: [MI-L] Re: MapBasic export to Shapefile

 

This message originated Externally. Use proper judgement and caution with attachments, links, or responses.

 

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/mapinfo-l/09808f50-0361-48c6-beff-020b260e6b51n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages