Skip to content

Commit

Permalink
[FEATURE] Support Z/M dimensions in delimited WKT files
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 22, 2015
1 parent f994e71 commit 809c925
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ QgsGeometry* QgsDelimitedTextFeatureIterator::loadGeometryWkt( const QStringList
}

isNull = false;
geom = QgsDelimitedTextProvider::geomFromWkt( sWkt, mSource->mWktHasPrefix, mSource->mWktHasZM );
geom = QgsDelimitedTextProvider::geomFromWkt( sWkt, mSource->mWktHasPrefix );

if ( geom && geom->type() != mSource->mGeometryType )
{
Expand Down Expand Up @@ -482,7 +482,6 @@ QgsDelimitedTextFeatureSource::QgsDelimitedTextFeatureSource( const QgsDelimited
, mXFieldIndex( p->mXFieldIndex )
, mYFieldIndex( p->mYFieldIndex )
, mWktFieldIndex( p->mWktFieldIndex )
, mWktHasZM( p->mWktHasZM )
, mWktHasPrefix( p->mWktHasPrefix )
, mGeometryType( p->mGeometryType )
, mDecimalPoint( p->mDecimalPoint )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class QgsDelimitedTextFeatureSource : public QgsAbstractFeatureSource
int mXFieldIndex;
int mYFieldIndex;
int mWktFieldIndex;
bool mWktHasZM;
bool mWktHasPrefix;
QGis::GeometryType mGeometryType;
QString mDecimalPoint;
Expand Down
13 changes: 2 additions & 11 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ static const QString TEXT_PROVIDER_DESCRIPTION = "Delimited text data provider";
static const int SUBSET_ID_THRESHOLD_FACTOR = 10;

QRegExp QgsDelimitedTextProvider::WktPrefixRegexp( "^\\s*(?:\\d+\\s+|SRID\\=\\d+\\;)", Qt::CaseInsensitive );
QRegExp QgsDelimitedTextProvider::WktZMRegexp( "\\s*(?:z|m|zm)(?=\\s*\\()", Qt::CaseInsensitive );
QRegExp QgsDelimitedTextProvider::WktCrdRegexp( "(\\-?\\d+(?:\\.\\d*)?\\s+\\-?\\d+(?:\\.\\d*)?)\\s[\\s\\d\\.\\-]+" );
QRegExp QgsDelimitedTextProvider::CrdDmsRegexp( "^\\s*(?:([-+nsew])\\s*)?(\\d{1,3})(?:[^0-9.]+([0-5]?\\d))?[^0-9.]+([0-5]?\\d(?:\\.\\d+)?)[^0-9.]*([-+nsew])?\\s*$", Qt::CaseInsensitive );

QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString& uri )
Expand All @@ -67,7 +65,6 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString& uri )
, mXFieldIndex( -1 )
, mYFieldIndex( -1 )
, mWktFieldIndex( -1 )
, mWktHasZM( false )
, mWktHasPrefix( false )
, mXyDms( false )
, mSubsetString( "" )
Expand Down Expand Up @@ -447,9 +444,7 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )
QgsGeometry *geom = nullptr;
if ( !mWktHasPrefix && sWkt.indexOf( WktPrefixRegexp ) >= 0 )
mWktHasPrefix = true;
if ( !mWktHasZM && sWkt.indexOf( WktZMRegexp ) >= 0 )
mWktHasZM = true;
geom = geomFromWkt( sWkt, mWktHasPrefix, mWktHasZM );
geom = geomFromWkt( sWkt, mWktHasPrefix );

if ( geom )
{
Expand Down Expand Up @@ -812,7 +807,7 @@ void QgsDelimitedTextProvider::rescanFile()
mUseSpatialIndex = buildSpatialIndex;
}

QgsGeometry *QgsDelimitedTextProvider::geomFromWkt( QString &sWkt, bool wktHasPrefixRegexp, bool wktHasZM )
QgsGeometry *QgsDelimitedTextProvider::geomFromWkt( QString &sWkt, bool wktHasPrefixRegexp )
{
QgsGeometry *geom = nullptr;
try
Expand All @@ -822,10 +817,6 @@ QgsGeometry *QgsDelimitedTextProvider::geomFromWkt( QString &sWkt, bool wktHasPr
sWkt.remove( WktPrefixRegexp );
}

if ( wktHasZM )
{
sWkt.remove( WktZMRegexp ).replace( WktCrdRegexp, "\\1" );
}
geom = QgsGeometry::fromWkt( sWkt );
}
catch ( ... )
Expand Down
21 changes: 2 additions & 19 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider

private:

static QRegExp WktZMRegexp;
static QRegExp WktCrdRegexp;

void scanFile( bool buildIndexes );
void rescanFile();
void resetCachedSubset();
Expand All @@ -231,7 +228,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
void setUriParameter( const QString& parameter, const QString& value );


static QgsGeometry *geomFromWkt( QString &sWkt, bool wktHasPrefixRegexp, bool wktHasZM );
static QgsGeometry *geomFromWkt( QString &sWkt, bool wktHasPrefixRegexp );
static bool pointFromXY( QString &sX, QString &sY, QgsPoint &point, const QString& decimalPoint, bool xyDms );
static double dmsStringToDouble( const QString &sX, bool *xOk );

Expand All @@ -258,13 +255,8 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
int mYFieldIndex;
int mWktFieldIndex;

// Handling of WKT types with .. Z, .. M, and .. ZM geometries (ie
// Z values and/or measures). mWktZMRegexp is used to test for and
// remove the Z or M fields, and mWktCrdRegexp is used to remove the
// extra coordinate values. mWktPrefix regexp is used to clean up
// mWktPrefix regexp is used to clean up
// prefixes sometimes used for WKT (postgis EWKT, informix SRID)

bool mWktHasZM;
bool mWktHasPrefix;

//! Layer extent
Expand Down Expand Up @@ -295,15 +287,6 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
//! Record file updates, flags rescan required
bool mRescanRequired;

struct wkbPoint
{
unsigned char byteOrder;
quint32 wkbType;
double x;
double y;
};
wkbPoint mWKBpt;

// Coordinate reference sytem
QgsCoordinateReferenceSystem mCrs;

Expand Down
10 changes: 5 additions & 5 deletions tests/src/python/test_qgsdelimitedtextprovider_wanted.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def test_011_read_wkt():
'id': u'10',
'description': u'Measure in point',
'#fid': 11,
'#geometry': 'Point (10 20)',
'#geometry': 'PointM (10 20 30)',
},
}
wanted['log'] = [
Expand Down Expand Up @@ -507,7 +507,7 @@ def test_012_read_wkt_point():
'id': u'10',
'description': u'Measure in point',
'#fid': 11,
'#geometry': 'Point (10 20)',
'#geometry': 'PointM (10 20 30)',
},
}
wanted['log'] = [
Expand Down Expand Up @@ -542,19 +542,19 @@ def test_013_read_wkt_line():
'id': u'11',
'description': u'Measure in line',
'#fid': 12,
'#geometry': 'LineString (10 20, 11 21)',
'#geometry': 'LineStringM (10 20 30, 11 21 31)',
},
13: {
'id': u'12',
'description': u'Z in line',
'#fid': 13,
'#geometry': 'LineString (10 20, 11 21)',
'#geometry': 'LineStringZ (10 20 30, 11 21 31)',
},
14: {
'id': u'13',
'description': u'Measure and Z in line',
'#fid': 14,
'#geometry': 'LineString (10 20, 11 21)',
'#geometry': 'LineStringZM (10 20 30 40, 11 21 31 41)',
},
15: {
'id': u'14',
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/delimitedtext/testwkt.csv
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ id|description|geom_wkt
8|EWKT prefix|SRID=1234;POINT(10 10)
9|Informix prefix|1 POINT(10 10)
10|Measure in point|POINTM(10 20 30)
11|Measure in line|LINESTRING(10.0 20.0 30.0, 11.0 21.0 31.0)
11|Measure in line|LINESTRINGM(10.0 20.0 30.0, 11.0 21.0 31.0)
12|Z in line|LINESTRING Z(10.0 20.0 30.0, 11.0 21.0 31.0)
13|Measure and Z in line|LINESTRING ZM(10.0 20.0 30.0 40.0, 11.0 21.0 31.0 41.0)
14|CircularString|CIRCULARSTRING(268 415,227 505,227 406)
Expand Down

0 comments on commit 809c925

Please sign in to comment.