Skip to content

Commit ce8a560

Browse files
committed
Additional documentation
1 parent b501c9a commit ce8a560

3 files changed

Lines changed: 88 additions & 16 deletions

File tree

resources/context_help/QgsDelimitedTextSourceSelect-en_US

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,22 @@ entering it twice. For example if ' is a quote character and an escape characte
125125
<h4><a name="regexp">How regular expression delimiters work</a></h4>
126126
<p>Regular expressions are mini-language used to represent character patterns. There are many variations
127127
of regular expression syntax - QGis uses the syntax provided by the <a href="http://qt-project.org/doc/qt-4.8/qregexp.html">QRegExp</a> class of the <a href="http://qt.digia.com">Qt</a> framework.</p>
128-
<p>In a regular expression delimited file each line is treated as a record. Each match of the regular expression in the line is treated as the end of a field.</p>
128+
<p>In a regular expression delimited file each line is treated as a record. Each match of the regular expression in the line is treated as the end of a field. If the regular expression contains capture groups
129+
then these are extracted as fields. </p>
130+
<p>The regular expression is treated slightly differently if it is anchored to the start of the line (that is, the pattern starts with &quot;^&quot;.
131+
In this case the regular expression is matched against each line. If the line does not match it is discarded
132+
as an invalid record. Each capture group in the expression is treated as a field. The regular expression
133+
is invalid if it does not have capture groups. As an example this can be used as a (somewhat
134+
unintuitive) means of loading data with fixed width fields. For example if the data has fields of 5
135+
characters, 10 characters, and 2 fields of 20 characters, then this can be loaded with a regular
136+
expression such as
137+
<pre>
138+
^(.{5})(.{10})(.{20})(.{20}).*
139+
</pre>
140+
<p>
141+
(If the records are possibly not completely filled then the counts could be entered as {,5}, meaning
142+
up to 5 characters, so that the regular expression will not fail).
143+
</p>
129144

130145
<h4><a name="wkt">How WKT text is interpreted</a></h4>
131146
<p>
@@ -172,3 +187,51 @@ id|wkt<br />
172187
</ul>
173188

174189
<h4><a name="python">Using delimited text layers in Python</a></h4>
190+
<p>Delimited text data sources can be creating from Python in a similar way to other vector layers.
191+
The pattern is:
192+
</p>
193+
<pre>
194+
from PyQt4.QtCore import QUrl, QString<br />
195+
from qgis.core import QgsVectorLayer, QgsMapLayerRegistry<br />
196+
<br />
197+
# Define the data source<br />
198+
filename="test.csv"<br />
199+
uri=QUrl.fromLocalFile(filename)<br />
200+
uri.addQueryItem("type","csv")<br />
201+
uri.addQueryItem("delimiter","|")<br />
202+
# ... other delimited text parameters<br />
203+
layer=QgsVectorLayer(QString(uri.toEncoded()),"Test CSV layer","delimitedtext")<br />
204+
# Add the layer to the map<br />
205+
if layer.isValid():<br />
206+
QgsMapLayerRegistry.instance().addMapLayer( layer )<br />
207+
</pre>
208+
<p>The configuration of the delimited text layer is defined by adding query items to the uri.
209+
The following options can be added
210+
</p>
211+
<ul>
212+
<li><i>encoding=..</i> defines the file encoding. The default is &quot;UTF-8&quot;</li>
213+
<li><i>type=(csv|regexp|whitespace)</i>< defines the delimiter type. Valid values are csv,
214+
regexp, and whitespace (which is just a special case of regexp). Default is csv.</li>
215+
<li><i>delimiter=...</i> defines the delimiters that will be used for csv formatted files,
216+
or the regular expression for regexp formatted files. Default is , for CSV files. There is
217+
no default for regexp files.</li>
218+
<li><i>quote=..</i> (for csv files) defines the characters used to quote fields. Default is &quot;</li>
219+
<li><i>escape=..</i> (for csv files) defines the characters used to escape the special meaning of the next character. Default is &quot;</li>
220+
<li><i>skipLines=#</i> defines the number of lines to discard from the beginning of the file. Default is 0.</li>
221+
<li><i>useHeader=(yes|no)</i> defines whether the first data record contains the names of the data fields. Default is yes.</li>
222+
<li><i>trimFields=(yes|no)</i> defines whether leading and trailing whitespace is to be removed from unquoted fields. Default is no.</li>
223+
<li><i>maxFields=#</i> defines the maximum number of fields that will be loaded from the file.
224+
Additional fields in each record will be discarded. Default is 0 - display all fields.
225+
(This option is not available from the delimited text layer dialog box).</li>
226+
<li><i>skipEmptyFields=(yes|no)</i> defines whether empty unquoted fields will be discarded if they are empty (applied after trimFields). Default is no.</li>
227+
<li><i>decimalPoint=.</i> specifies an alternative character that may be used as a decimal point in numeric fields. Default is a point (full stop) character.</li>
228+
<li><i>wktField=fieldname</i> specifies the name or number (starting at 1) of the field containing a well known text geometry definition</li>
229+
<li><i>xField=fieldname</i> specifies the name or number (starting at 1) of the field the X coordinate (only applies if wktField is not defined)</li>
230+
<li><i>yField=fieldname</i> specifies the name or number (starting at 1) of the field the Y coordinate (only applies if wktField is not defined)</li>
231+
<li><i>geomType=(auto|point|line|polygon|none)</i> specifies type of geometry for wkt fields, or none to load the file as an attribute-only table. Default is auto.</li>
232+
<li><i>crs=...</i> specifies the coordinate system to use for the vector layer, in a format accepted by QgsCoordinateReferenceSystem.createFromString (for example &quot;EPSG:4167&quot;). If this is not
233+
specified then a dialog box may request this information from the user.</li>
234+
<li><i>quiet=(yes|no)</i> specifies whether errors encountered loading the layer are presented in a dialog box (they will be written to the QGis log in any case). Default is no.</li>
235+
</ul>
236+
237+

src/core/qgsvectorlayer.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,6 @@ struct CORE_EXPORT QgsVectorJoinInfo
310310
*
311311
* Defines the characters used to escape delimiter, quote, and newline characters.
312312
*
313-
* - skipEmptyFields=(yes|no)
314-
*
315-
* If yes then empty fields will be discarded (eqivalent to concatenating consecutive
316-
* delimiters)
317-
*
318-
* - trimFields=(yes|no)
319-
*
320-
* If yes then leading and trailing whitespace will be removed from fields
321-
*
322313
* - skipLines=n
323314
*
324315
* Defines the number of lines to ignore at the beginning of the file (default 0)
@@ -328,17 +319,31 @@ struct CORE_EXPORT QgsVectorJoinInfo
328319
* Defines whether the first record in the file (after skipped lines) contains
329320
* column names (default yes)
330321
*
331-
* - xField=column yField=column
322+
* - trimFields=(yes|no)
332323
*
333-
* Defines the name of the columns holding the x and y coordinates for XY point geometries.
334-
* If the useHeader is no (ie there are no column names), then this is the column
335-
* number (with the first column as 1).
324+
* If yes then leading and trailing whitespace will be removed from fields
325+
*
326+
* - skipEmptyFields=(yes|no)
327+
*
328+
* If yes then empty fields will be discarded (eqivalent to concatenating consecutive
329+
* delimiters)
330+
*
331+
* - maxFields=#
332+
*
333+
* Specifies the maximum number of fields to load for each record. Additional
334+
* fields will be discarded. Default is 0 - load all fields.
336335
*
337336
* - decimalPoint=c
338337
*
339338
* Defines a character that is used as a decimal point in the numeric columns
340339
* The default is '.'.
341340
*
341+
* - xField=column yField=column
342+
*
343+
* Defines the name of the columns holding the x and y coordinates for XY point geometries.
344+
* If the useHeader is no (ie there are no column names), then this is the column
345+
* number (with the first column as 1).
346+
*
342347
* - xyDms=(yes|no)
343348
*
344349
* If yes then the X and Y coordinates are interpreted as
@@ -383,8 +388,6 @@ struct CORE_EXPORT QgsVectorJoinInfo
383388
*
384389
* Provider to display vector data in a GRASS GIS layer.
385390
*
386-
*
387-
*
388391
*/
389392

390393

src/providers/delimitedtext/qgsdelimitedtextfile.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ bool QgsDelimitedTextFile::setFromUrl( QUrl &url )
151151
quote = "'\"";
152152
escape = "";
153153
}
154+
else if( type == "regexp ")
155+
{
156+
delimiter="";
157+
quote="";
158+
escape="";
159+
}
154160
}
155161
if ( url.hasQueryItem( "delimiter" ) )
156162
{

0 commit comments

Comments
 (0)