Skip to content

Commit 532599e

Browse files
committed
Merge branch '5605-types' of git://github.com/carolinux/QGIS into carolinux-5605-types
2 parents 250eb74 + 48591df commit 532599e

File tree

4 files changed

+94
-18
lines changed

4 files changed

+94
-18
lines changed

src/providers/delimitedtext/qgsdelimitedtextprovider.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ QStringList QgsDelimitedTextProvider::readCsvtFieldTypes( QString filename, QStr
245245
// not allowed in OGR CSVT files. Also doesn't care if int and string fields have
246246

247247
strTypeList = strTypeList.toLower();
248-
QRegExp reTypeList( "^(?:\\s*(\\\"?)(?:integer|real|string|date|datetime|time)(?:\\(\\d+(?:\\.\\d+)?\\))?\\1\\s*(?:,|$))+" );
248+
QRegExp reTypeList( "^(?:\\s*(\\\"?)(?:integer|real|long|longlong|string|date|datetime|time)(?:\\(\\d+(?:\\.\\d+)?\\))?\\1\\s*(?:,|$))+" );
249249
if ( ! reTypeList.exactMatch( strTypeList ) )
250250
{
251251
// Looks like this was supposed to be a CSVT file, so report bad formatted string
@@ -407,6 +407,7 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )
407407

408408
QList<bool> isEmpty;
409409
QList<bool> couldBeInt;
410+
QList<bool> couldBeLongLong;
410411
QList<bool> couldBeDouble;
411412

412413
while ( true )
@@ -561,27 +562,28 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )
561562
{
562563

563564
QString &value = parts[i];
564-
if ( value.isEmpty() )
565-
continue;
566-
567-
// try to convert attribute values to integer and double
568-
569-
while ( couldBeInt.size() <= i )
570-
{
565+
if ( value.isEmpty() ) {
571566
isEmpty.append( true );
572567
couldBeInt.append( false );
568+
couldBeLongLong.append( false );
573569
couldBeDouble.append( false );
570+
continue;
574571
}
575-
if ( isEmpty[i] )
576-
{
577-
isEmpty[i] = false;
578-
couldBeInt[i] = true;
579-
couldBeDouble[i] = true;
580-
}
572+
// try to convert attribute values to integer, long and double
573+
isEmpty.append( false );
574+
// try all possible formats
575+
couldBeInt.append( true );
576+
couldBeLongLong.append( true );
577+
couldBeDouble.append( true );
581578
if ( couldBeInt[i] )
582579
{
583580
value.toInt( &couldBeInt[i] );
584581
}
582+
583+
if ( couldBeLongLong[i] )
584+
{
585+
value.toLongLong( &couldBeLongLong[i] );
586+
}
585587
if ( couldBeDouble[i] )
586588
{
587589
if ( ! mDecimalPoint.isEmpty() )
@@ -620,6 +622,11 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )
620622
fieldType = QVariant::Int;
621623
typeName = "integer";
622624
}
625+
else if ( csvtTypes[i] == "long" || csvtTypes[i]== "longlong" )
626+
{
627+
fieldType = QVariant::LongLong; //QVariant doesn't support long
628+
typeName = "longlong";
629+
}
623630
else if ( csvtTypes[i] == "real" )
624631
{
625632
fieldType = QVariant::Double;
@@ -633,6 +640,11 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )
633640
fieldType = QVariant::Int;
634641
typeName = "integer";
635642
}
643+
else if ( couldBeLongLong[i] )
644+
{
645+
fieldType = QVariant::LongLong;
646+
typeName = "longlong";
647+
}
636648
else if ( couldBeDouble[i] )
637649
{
638650
fieldType = QVariant::Double;
@@ -997,12 +1009,14 @@ bool QgsDelimitedTextProvider::setSubsetString( QString subset, bool updateFeatu
9971009

9981010
if ( valid )
9991011
{
1000-
1001-
if ( mSubsetExpression ) delete mSubsetExpression;
1012+
QgsExpression * tmpSubsetExpression = mSubsetExpression;
1013+
// using a tmp pointer to avoid the pointer being dereferenced by
1014+
// a friend class after it has been freed but before it has been
1015+
// reassigned
10021016
QString previousSubset = mSubsetString;
10031017
mSubsetString = subset;
10041018
mSubsetExpression = expression;
1005-
1019+
if ( tmpSubsetExpression ) delete tmpSubsetExpression;
10061020
// Update the feature count and extents if requested
10071021

10081022
// Usage of updateFeatureCount is a bit painful, basically expect that it

tests/src/python/test_qgsdelimitedtextprovider.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# This will get replaced with a git SHA1 when you do a git archive
1313
__revision__ = '$Format:%H$'
1414

15-
# This module provides unit test for the delimtied text provider. It uses data files in
15+
# This module provides unit test for the delimited text provider. It uses data files in
1616
# the testdata/delimitedtext directory.
1717
#
1818
# New tests can be created (or existing ones updated), but incorporating a createTest
@@ -650,6 +650,12 @@ def test_037_csvt_file_invalid_file(self):
650650
requests=None
651651
runTest(filename,requests,**params)
652652

653+
def test_038_long_type(self):
654+
# Skip lines
655+
filename='testlongs.csv'
656+
params={'yField': 'lat', 'xField': 'lon', 'type': 'csv'}
657+
requests=None
658+
runTest(filename,requests,**params)
653659

654660
if __name__ == '__main__':
655661
unittest.main()

tests/src/python/test_qgsdelimitedtextprovider_wanted.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,3 +2165,54 @@ def test_037_csvt_file_invalid_file():
21652165
}
21662166
wanted['log']=[]
21672167
return wanted
2168+
2169+
def test_038_long_type():
2170+
wanted={}
2171+
filename="testlongs.csv"
2172+
wanted['uri']=u'file://{}?yField={}&xField={}&type={}'.format(filename,"lat","lon","csv")
2173+
wanted['fieldTypes']=['longlong', 'text', 'double', 'double', 'integer']
2174+
# long is casted as longlong because of QVariant limitation
2175+
wanted['data']={
2176+
2L: {
2177+
'id': u'9189304972279762602',
2178+
'a_long': u'9189304972279762602',
2179+
'lon': u'1.0',
2180+
'lat': u'1.0',
2181+
'an_integer': u'40',
2182+
'#geometry': 'POINT(1.0 1.0)',
2183+
'#fid':2L,
2184+
'description': 'line1'
2185+
},
2186+
3L: {
2187+
'id': u'9189304972279762602',
2188+
'a_long': u'9189304972279762602',
2189+
'lon': u'2.2',
2190+
'lat': u'2.5',
2191+
'an_integer': u'5',
2192+
'#geometry': 'POINT(2.2 2.5)',
2193+
'#fid':3L,
2194+
'description': 'line2'
2195+
},
2196+
4L: {
2197+
'id': u'-3123724580211819352',
2198+
'a_long': u'-3123724580211819352',
2199+
'lon': u'1.0',
2200+
'lat': u'1.0',
2201+
'an_integer': u'7',
2202+
'#geometry': 'POINT(1.0 1.0)',
2203+
'#fid':4L,
2204+
'description': 'line3'
2205+
},
2206+
5L: {
2207+
'id': u'-3',
2208+
'a_long': u'-3',
2209+
'lon': u'1.0',
2210+
'lat': u'1.0',
2211+
'an_integer': u'7',
2212+
'#geometry': 'POINT(1.0 1.0)',
2213+
'#fid':5L,
2214+
'description': 'line4'
2215+
}
2216+
}
2217+
wanted['log']=[]
2218+
return wanted
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a_long,description,lon,lat,an_integer
2+
9189304972279762602,line1,1.0,1.0,40
3+
9189304972279762602,line2,2.2,2.5,5
4+
-3123724580211819352,line3,1.0,1.0,7
5+
-3,line4,1.0,1.0,7

0 commit comments

Comments
 (0)