Summary of OGR Library read-write Mif/tab file Lonefox usage Experience

Source: Internet
Author: User

===================================

I log in all the unpublished articles and blog general for my original or finishing processing,
Original article copyright I (LONEFOX) all, reprint article copyright belongs to the original author all;

Http://blog.csdn.net/boythl

Welcome to reprint, but please specify the source, retain the author and copyright information.

===================================

Recent projects using the GDAL/OGR library read and write MIF and tab files, after several days of tossing, accumulated a little experience, and remember.

1. Ogrsfdriverregistrar::open (Path, FALSE); read-only files

For the Ogrsfdriverregistrar::open method, ogr built-in Mif/tab read-write library mitab is not supported by the update of the method, look at the following Mitab source:

Ogrdatasource *ogrtabdriver::open (const char * pszfilename, int bupdate) {Ogrtabdatasource *pods; if (bupdate)//Please note here, If bupdate is true, NULL {return null is returned directly;}//other code}

Again, you'll find that the way to access a file via the Open method is Tabread, not tabwrite, to the Imapinfofile class.

Perhaps you will say "This is Ogrtabdriver method, can see Ogrmifdriver method?", unfortunately, we can only use Registerogrtab (); Register MIF and tab-generic drivers, registerogrmif (); it's a beautiful lie that only defines and has no function to realize, and I don't know what the purpose of this definition is in OGR library. There is also no ogrmifdriver,ogrtabdriver-only MIF and tab file. And that's our 2nd.

2. Use only Registerogrtab (); Register tab/mif file read-write drive

The reason has been mentioned above. Registerogrtab () alone is sufficient.

The MIF and tab files are read through the open method, and the Smartopen method is called inside the Mitab library to differentiate between the Mif/mid suffix or the tab suffix.

3. Ways to write Mif/tab files

Ogrsfdriver *pdriver = Ogrsfdriverregistrar::getregistrar ()->getdriverbyname ("MapInfo File");
Ogrdatasource *pdstab = Pdriver->createdatasource (Strtabpath);//Create a file that automatically determines by extension to create a MIF file or a tab file

Lonefox Explanation: The Mitab library invokes the Ogrtabdatasource create method when implementing the CreateDataSource method, rather than the Open method

The Create method calls the Imapinfofile class's Open method internally, but the difference is that the access mode parameter passes the "WB".

And the pattern will mark M_eaccessmode = Tabwrite; is not the Tabread mode corresponding to the Ogrdatasource::open method;

Incidentally, there are only Tabwrite and Tabread in the Imapinfofile, which cannot be read and written.

typedef enum {tabread, tabwrite, tabreadwrite/* ReadWrite Not implemented yet note this comment} *///;

4. Methods of reading and writing Mif/tab

If I have a file with some data, but I want to open and continue to add data, how to do.

I took a curve to save the nation's way to solve the problem: I open the document; II. Create a new temporary file for writing in accordance with article III above; Use the Copylayer method of the Ogrdatasource class to paste the old data opened in I into the new layer,iv. Call the Ogrlayer::synctodisk method to save the new file; Delete old files to rename temporary files

If there is a better way to have a hero, you must feel free to be good ...

5. Mif/tab Format Interchange

The use of OGRDATASOURCE::COPYLAYER,MIF and tab format for each other is easy. Direct Source Code Bar

BOOL mif2tab (const cstring& mifpath, const cstring& tabpath) {registerogrtab (); Ogrsfdriver *pdriver = Ogrsfdriverregistrar::getregistrar ()->getdriverbyname ("MapInfo File"); if (Pdriver = = NULL) {return FALSE;}//Open original MIF//cnilayer::createemptymid (Mifpath);//empty Mid is not empty to avoid ogr load failure ogrdatasource* PDSMIF = Ogrsfdriverregistrar::open (Mifpath); if (pdsmif = = NULL) {TRACE1 ("warning:mif File open Failed%s/n", Mifpath); ogrlayer* playermif = pdsmif->getlayer (0); Create results tab Ogrdatasource *pdstab = Pdriver->createdatasource (Tabpath); if (Pdstab = = NULL) {TRACE1 ("Error:tab file created failed%s/n", Tabpath); Ogrdatasource::D estroydatasource (PDSMIF); return FALSE; }//Copy data ogrlayer* Playertab = Pdstab->copylayer (playermif, "test", NULL); Close MIF file Ogrdatasource::D estroydatasource (PDSMIF); if (Playertab = NULL) {TRACE2 ("error:copied layer to TAB failed%s->%s/n", Mifpath, Tabpath); Ogrdatasource::D estroydatasource (Pdstab); return FALSE; }//Save result OGrerr err = Playertab->synctodisk (); Close Tab file Ogrdatasource::D estroydatasource (Pdstab); if (Err!= ogrerr_none) {TRACE1 ("error:saved tab file failed%s/n", Tabpath); return FALSE;} return TRUE; }

The tab MIF is similar to the above code.

6. OGR Library Bugs

6.1. Failed to read the null MIF file limit

If a MIF file is an empty file (of course, the file format is in MIF format, but there is no feature in the layer), that is, the mid file size is 0; then the Ogr open method returns null, meaning that the read failed.

This is because the Mitab library will try to read a row while opening the mid file

* Put the ' MID ' file at the correct location, on the ' The ' the ' feature/if (M_eaccessmode = = Tabread && (m_pomidfile- >getline () = = null))//empty mid Getline returns null {close (); if (Btestopennoerror) Cplerrorreset (); return-1;}

This code is located inside the int miffile::open (const char *pszfname, const char *pszaccess, Gbool btestopennoerror/*=false*/) method.

There are two ways to solve the problem: 1. Modify OGR Library Source code, we have previously done so; 2. Insert a newline character in front of the empty mid file, and the character's existence can "cheat" the Getline method does not return null

6.2. Restrictions on character sets

OGR write the Mif/tab file, character sets are all neutral, regardless of the previous charset, I keenness, did not find a way to modify the character set, prawns know the words please be sure to feel free AH.

However, there is a Setcharset interface in the Imapinfofile class, which is implemented by the Imapinfofile default, is not override, and is not invoked inside the Mitab library.

The problem is that the OGR library does not provide a similar interface.

Solution: 1. Modify the OGR library to add an interface to set up CharSet. 2. Discard the OGR library and manipulate MIF files directly using the Imapinfofile class

6.3. Support only a small number of SQL queries, do not support space queries

SQL query related content can see Swq.h file, Space query in the OGR1.60 version has not seen the interface, look forward to.

7. Try to use getnextfeature instead of Getfeaturecount

Let's look at the implementation code for the Ogrlayer::getfeaturecount method

int ogrlayer::getfeaturecount (int bForce) {ogrfeature *pofeature; int nfeaturecount = 0; if (!bforce) return-1; Resetreading (); while ((Pofeature = Getnextfeature ())!= NULL) {nfeaturecount++; delete pofeature;} Resetreading (); return nfeaturecount; }

As you can see from the above, in most cases, the Getfeaturecount argument gets a direct-1, and if true, I'm sorry, it's all feature traversal of an entire layer.

Unfortunately, the Mitab library does not override the method, and there is no more efficient algorithm.

Therefore, if you want to determine whether the layer has feature, such as whether the query result of the SQL query is empty, do not explicitly require the number of elements, please use resetreading to determine whether the getnextfeature is null, Avoid using Getfeaturecount.

Remember, Ogr's getfeaturecount is different from the getrecordcount of the ADO library.

Time in a hurry, Lonefox to write on the gap, and then add a few days of use, experience is limited, the wrong place to ask treatise, another, GIS peers welcome to join friends to discuss.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.