55
55
QgsProcessingContext ,
56
56
QgsProcessingUtils )
57
57
58
- from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
59
- from processing .tools import dataobjects , spatialite , postgis
60
-
61
-
62
- TYPE_MAP = {
63
- str : QVariant .String ,
64
- float : QVariant .Double ,
65
- int : QVariant .Int ,
66
- bool : QVariant .Bool
67
- }
68
-
69
- TYPE_MAP_MEMORY_LAYER = {
70
- QVariant .String : "string" ,
71
- QVariant .Double : "double" ,
72
- QVariant .Int : "integer" ,
73
- QVariant .Date : "date" ,
74
- QVariant .DateTime : "datetime" ,
75
- QVariant .Time : "time"
76
- }
77
-
78
- TYPE_MAP_POSTGIS_LAYER = {
79
- QVariant .String : "VARCHAR" ,
80
- QVariant .Double : "REAL" ,
81
- QVariant .Int : "INTEGER" ,
82
- QVariant .Bool : "BOOLEAN"
83
- }
84
-
85
- TYPE_MAP_SPATIALITE_LAYER = {
86
- QVariant .String : "VARCHAR" ,
87
- QVariant .Double : "REAL" ,
88
- QVariant .Int : "INTEGER" ,
89
- QVariant .Bool : "INTEGER"
90
- }
58
+ from processing .tools import dataobjects
91
59
92
60
93
61
def resolveFieldIndex (layer , attr ):
@@ -305,12 +273,6 @@ def checkMinDistance(point, index, distance, points):
305
273
return True
306
274
307
275
308
- def _toQgsField (f ):
309
- if isinstance (f , QgsField ):
310
- return f
311
- return QgsField (f [0 ], TYPE_MAP .get (f [1 ], QVariant .String ))
312
-
313
-
314
276
def snapToPrecision (geom , precision ):
315
277
snapped = QgsGeometry (geom )
316
278
if precision == 0.0 :
@@ -447,10 +409,6 @@ def ogrLayerName(uri):
447
409
return name
448
410
449
411
450
- MEMORY_LAYER_PREFIX = 'memory:'
451
- POSTGIS_LAYER_PREFIX = 'postgis:'
452
- SPATIALITE_LAYER_PREFIX = 'spatialite:'
453
-
454
412
NOGEOMETRY_EXTENSIONS = [
455
413
u'csv' ,
456
414
u'dbf' ,
@@ -459,135 +417,6 @@ def ogrLayerName(uri):
459
417
]
460
418
461
419
462
- def createVectorWriter (destination , encoding , fields , geometryType , crs , context ):
463
- return QgsProcessingUtils .createFeatureSink (destination , encoding , fields , geometryType , crs , context )
464
- layer = None
465
- sink = None
466
-
467
- if encoding is None :
468
- settings = QgsSettings ()
469
- encoding = settings .value ('/Processing/encoding' , 'System' , str )
470
-
471
- if destination .startswith (MEMORY_LAYER_PREFIX ):
472
- uri = QgsWkbTypes .displayString (geometryType ) + "?uuid=" + str (uuid .uuid4 ())
473
- if crs .isValid ():
474
- uri += '&crs=' + crs .authid ()
475
- fieldsdesc = []
476
- for f in fields :
477
- qgsfield = _toQgsField (f )
478
- fieldsdesc .append ('field=%s:%s' % (qgsfield .name (),
479
- TYPE_MAP_MEMORY_LAYER .get (qgsfield .type (), "string" )))
480
- if fieldsdesc :
481
- uri += '&' + '&' .join (fieldsdesc )
482
-
483
- layer = QgsVectorLayer (uri , destination , 'memory' )
484
- sink = layer .dataProvider ()
485
- context .temporaryLayerStore ().addMapLayer (layer )
486
- destination = layer .id ()
487
- elif destination .startswith (POSTGIS_LAYER_PREFIX ):
488
- uri = QgsDataSourceUri (destination [len (POSTGIS_LAYER_PREFIX ):])
489
- connInfo = uri .connectionInfo ()
490
- (success , user , passwd ) = QgsCredentials .instance ().get (connInfo , None , None )
491
- if success :
492
- QgsCredentials .instance ().put (connInfo , user , passwd )
493
- else :
494
- raise GeoAlgorithmExecutionException ("Couldn't connect to database" )
495
- try :
496
- db = postgis .GeoDB (host = uri .host (), port = int (uri .port ()),
497
- dbname = uri .database (), user = user , passwd = passwd )
498
- except postgis .DbError as e :
499
- raise GeoAlgorithmExecutionException (
500
- "Couldn't connect to database:\n %s" % e .message )
501
-
502
- def _runSQL (sql ):
503
- try :
504
- db ._exec_sql_and_commit (str (sql ))
505
- except postgis .DbError as e :
506
- raise GeoAlgorithmExecutionException (
507
- 'Error creating output PostGIS table:\n %s' % e .message )
508
-
509
- fields = [_toQgsField (f ) for f in fields ]
510
- fieldsdesc = "," .join ('%s %s' % (f .name (),
511
- TYPE_MAP_POSTGIS_LAYER .get (f .type (), "VARCHAR" ))
512
- for f in fields )
513
-
514
- _runSQL ("CREATE TABLE %s.%s (%s)" % (uri .schema (), uri .table ().lower (), fieldsdesc ))
515
- if geometryType != QgsWkbTypes .NullGeometry :
516
- _runSQL ("SELECT AddGeometryColumn('{schema}', '{table}', 'the_geom', {srid}, '{typmod}', 2)" .format (
517
- table = uri .table ().lower (), schema = uri .schema (), srid = crs .authid ().split (":" )[- 1 ],
518
- typmod = QgsWkbTypes .displayString (geometryType ).upper ()))
519
-
520
- layer = QgsVectorLayer (uri .uri (), uri .table (), "postgres" )
521
- sink = layer .dataProvider ()
522
- context .temporaryLayerStore ().addMapLayer (layer )
523
- elif destination .startswith (SPATIALITE_LAYER_PREFIX ):
524
- uri = QgsDataSourceUri (destination [len (SPATIALITE_LAYER_PREFIX ):])
525
- try :
526
- db = spatialite .GeoDB (uri = uri )
527
- except spatialite .DbError as e :
528
- raise GeoAlgorithmExecutionException (
529
- "Couldn't connect to database:\n %s" % e .message )
530
-
531
- def _runSQL (sql ):
532
- try :
533
- db ._exec_sql_and_commit (str (sql ))
534
- except spatialite .DbError as e :
535
- raise GeoAlgorithmExecutionException (
536
- 'Error creating output Spatialite table:\n %s' % str (e ))
537
-
538
- fields = [_toQgsField (f ) for f in fields ]
539
- fieldsdesc = "," .join ('%s %s' % (f .name (),
540
- TYPE_MAP_SPATIALITE_LAYER .get (f .type (), "VARCHAR" ))
541
- for f in fields )
542
-
543
- _runSQL ("DROP TABLE IF EXISTS %s" % uri .table ().lower ())
544
- _runSQL ("CREATE TABLE %s (%s)" % (uri .table ().lower (), fieldsdesc ))
545
- if geometryType != QgsWkbTypes .NullGeometry :
546
- _runSQL ("SELECT AddGeometryColumn('{table}', 'the_geom', {srid}, '{typmod}', 2)" .format (
547
- table = uri .table ().lower (), srid = crs .authid ().split (":" )[- 1 ],
548
- typmod = QgsWkbTypes .displayString (geometryType ).upper ()))
549
-
550
- layer = QgsVectorLayer (uri .uri (), uri .table (), "spatialite" )
551
- sink = layer .dataProvider ()
552
- context .temporaryLayerStore ().addMapLayer (layer )
553
- else :
554
- formats = QgsVectorFileWriter .supportedFiltersAndFormats ()
555
- OGRCodes = {}
556
- for (key , value ) in list (formats .items ()):
557
- extension = str (key )
558
- extension = extension [extension .find ('*.' ) + 2 :]
559
- extension = extension [:extension .find (' ' )]
560
- OGRCodes [extension ] = value
561
- OGRCodes ['dbf' ] = "DBF file"
562
-
563
- extension = destination [destination .rfind ('.' ) + 1 :]
564
-
565
- if extension not in OGRCodes :
566
- extension = 'shp'
567
- destination = destination + '.shp'
568
-
569
- if geometryType == QgsWkbTypes .NoGeometry :
570
- if extension == 'shp' :
571
- extension = 'dbf'
572
- destination = destination [:destination .rfind ('.' )] + '.dbf'
573
- if extension not in NOGEOMETRY_EXTENSIONS :
574
- raise GeoAlgorithmExecutionException (
575
- "Unsupported format for tables with no geometry" )
576
-
577
- qgsfields = QgsFields ()
578
- for field in fields :
579
- qgsfields .append (_toQgsField (field ))
580
-
581
- # use default dataset/layer options
582
- dataset_options = QgsVectorFileWriter .defaultDatasetOptions (OGRCodes [extension ])
583
- layer_options = QgsVectorFileWriter .defaultLayerOptions (OGRCodes [extension ])
584
-
585
- sink = QgsVectorFileWriter (destination , encoding ,
586
- qgsfields , geometryType , crs , OGRCodes [extension ],
587
- dataset_options , layer_options )
588
- return sink , destination , layer
589
-
590
-
591
420
class TableWriter (object ):
592
421
593
422
def __init__ (self , fileName , encoding , fields ):
0 commit comments