This repository has been archived by the owner on Apr 25, 2021. It is now read-only.
forked from sbraden/circle-craters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circle_craters.py
executable file
·728 lines (590 loc) · 25.1 KB
/
circle_craters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# -*- coding: utf-8 -*-
"""
/***************************************************************************
DetectCraters
A QGIS plugin prototype
An experimental crater measurement and counting tool for planetary science
-------------------
begin : 2020-10-01
author : (C) 2020 by Michael Klear
email : michael.r.klear@gmail.com
Code in this project is forked from:
CircleCraters
A QGIS plugin
A crater counting tool for planetary science
-------------------
begin : 2015-01-28
git sha : $Format:%H$
copyright : (C) 2015 by Sarah E Braden
email : braden.sarah@gmail.com
***************************************************************************/
"""
from __future__ import absolute_import
#from PyQt5 import QtCore
from builtins import str
from builtins import range
from builtins import object
import pdb
import os.path
import datetime
from matplotlib.path import Path
from qgis.PyQt.QtCore import QCoreApplication, QSettings, QTranslator, QVariant, qVersion
#from qgis.PyQt.QtGui import (
# QAction,
# QIcon,
#)
from qgis.PyQt.QtWidgets import QApplication, QAction, QMessageBox
from PyQt5.QtGui import QIcon
#from qgis.PyQt import QIcon
from qgis.core import (
QgsDistanceArea,
QgsFeature,
QgsField,
QgsGeometry,
QgsMapLayer,
QgsCoordinateTransform,
QgsCoordinateTransformContext,
QgsCoordinateReferenceSystem,
QgsPoint,
QgsPointXY,
QgsProject,
QgsWkbTypes
)
# QgsMapLayerRegistry has been moved to QgsProject.
from qgis.core import QgsSettings, QgsMessageLog, QgsMapLayer, QgsProject, QgsWkbTypes
from qgis.gui import (
QgsMapToolEmitPoint,
QgsMessageBar,
)
from osgeo import osr
# Initialize Qt resources from file resources.py
#from . import resources_rc
import CircleCraters.resources_rc
from CircleCraters.errors import CircleCraterError
from CircleCraters.shapes import Point, Circle, DetectedCircle
from CircleCraters.export_dialog import ExportDialog
from CircleCraters.choose_layers_dialog import ChooseCountingLayerDialog, ChooseRasterLayerDialog
#from CircleCraters.find_crater import DetectedCircle
# TODO: put units on attribute table headings
# TODO: put polygon area in attribute table for that layer
class CircleCraters(object):
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.iface = iface
self.canvas = self.iface.mapCanvas()
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'CircleCraters_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.export_dlg = ExportDialog()
self.export_dlg.selected.connect(self.export)
self.choose_counting_dlg = ChooseCountingLayerDialog()
self.choose_counting_dlg.selected.connect(self.on_counting_layer_select)
self.choose_raster_dlg = ChooseRasterLayerDialog()
self.choose_raster_dlg.selected.connect(self.on_raster_layer_select)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&Circle Craters')
self.toolbar = self.iface.addToolBar(u'CircleCraters')
self.toolbar.setObjectName(u'CircleCraters')
self.tool = QgsMapToolEmitPoint(self.canvas)
self.tool.canvasClicked.connect(self.handle_click)
self.tool.deactivated.connect(self.reset_clicks)
self.clicks = []
self.layer = None
self.raster_layer = None
# noinspection PyMethodMayBeStatic
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('CircleCraters', message)
def show_error(self, message, title='Error', **kwargs):
# QgsMessageBar.CRITICAL
self.iface.messageBar().pushMessage(
title, message, level=4, **kwargs)
def show_info(self, message, **kwargs):
# QgsMessageBar.INFO -> Qgis::Info
self.iface.messageBar().pushMessage(
message, level=3, **kwargs)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None
):
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:type icon_path: str
:param text: Text that should be shown in menu items for this action.
:type text: str
:param callback: Function to be called when the action is triggered.
:type callback: function
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:type enabled_flag: bool
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:type add_to_menu: bool
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:type add_to_toolbar: bool
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:type status_tip: str
:param parent: Parent widget for the new action. Defaults None.
:type parent: QWidget
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self): # noqa
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
self.start_action = self.add_action(
':/plugins/CircleCraters/icons/ic_layers_48px.svg',
text=self.tr(u'Select Crater Counting Layer'),
callback=self.show_layer_select,
parent=self.iface.mainWindow(),
)
self.stop_action = self.add_action(
':/plugins/CircleCraters/icons/ic_layers_clear_48px.svg',
text=self.tr(u'Stop Crater Counting'),
enabled_flag=False,
callback=self.stop_tool,
parent=self.iface.mainWindow(),
)
self.circle_action = self.add_action(
':/plugins/CircleCraters/icons/ic_add_circle_outline_48px.svg',
text=self.tr(u'Circle Craters'),
enabled_flag=False,
callback=self.set_tool,
parent=self.iface.mainWindow(),
)
self.export_action = self.add_action(
':/plugins/CircleCraters/icons/ic_archive_48px.svg',
text=self.tr(u'Export Data'),
callback=self.show_export_dialog,
parent=self.iface.mainWindow(),
)
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&Circle Craters'),
action)
self.iface.removeToolBarIcon(action)
def reset_clicks(self):
self.clicks = []
def handle_click(self, point, button):
#self.clicks.append(Point(point.x(), point.y()))
x, y = point.x(), point.y()
r = 16
#if len(self.clicks) != 3:
# return
self.draw_circle(DetectedCircle(self.raster_layer, x, y, r_in=r))
#self.reset_clicks()
def set_tool(self):
"""Run method that performs all the real work"""
if not self.layer:
error = 'No crater counting layer selected. Please choose a layer.'
self.show_error(error)
self.canvas.setMapTool(self.tool)
def stop_tool(self):
"""Run method that deactivates the crater counting tool"""
self.canvas.unsetMapTool(self.tool)
self.stop_action.setEnabled(False)
self.circle_action.setEnabled(False)
self.layer = None
def is_polygon_vector_layer(self, layer):
if layer.type() != QgsMapLayer.VectorLayer:
return False
return layer.geometryType() == QgsWkbTypes.PolygonGeometry
def is_raster_layer(self, layer):
if layer.type() != QgsMapLayer.RasterLayer:
return False
else:
return True
def get_counting_layer_choices(self):
root = QgsProject.instance().layerTreeRoot()
layers = root.findLayers()
return [layer.layer() for layer in layers if self.is_polygon_vector_layer(layer.layer())]
def get_raster_layer_choices(self):
root = QgsProject.instance().layerTreeRoot()
layers = root.findLayers()
return [layer.layer() for layer in layers if self.is_raster_layer(layer.layer())]
def show_layer_select(self):
""" Run method that lets users choose raster layer.
Sets self.raster_layer
"""
try:
self.choose_counting_dlg.show(self.get_counting_layer_choices())
except CircleCraterError as error:
self.show_error(error.message)
try:
self.choose_raster_dlg.show(self.get_raster_layer_choices())
except CircleCraterError as error:
self.show_error(error.message)
def on_counting_layer_select(self, layer):
self.layer = layer
self.set_field_attributes()
msg = 'The layer "{!s}" is set as the crater counting layer'
self.show_info(msg.format(layer.name()))
self.stop_action.setEnabled(True)
self.circle_action.setEnabled(True)
self.set_tool()
def on_raster_layer_select(self, layer):
self.raster_layer = layer
msg = 'The layer "{!s}" is set as the raster layer'
self.show_info(msg.format(layer.name()))
self.stop_action.setEnabled(True)
self.circle_action.setEnabled(True)
self.set_tool()
def set_field_attributes(self):
self.layer.startEditing()
# fieldNameIndex has been renamed to lookupField.
if self.layer.fields().lookupField('diameter') == -1:
field_attribute = QgsField('diameter', QVariant.Double)
self.layer.dataProvider().addAttributes([field_attribute])
if self.layer.fields().lookupField('center_lon') == -1:
field_attribute = QgsField('center_lon', QVariant.Double)
self.layer.dataProvider().addAttributes([field_attribute])
if self.layer.fields().lookupField('center_lat') == -1:
field_attribute = QgsField('center_lat', QVariant.Double)
self.layer.dataProvider().addAttributes([field_attribute])
self.layer.updateFields()
self.layer.commitChanges()
def show_export_dialog(self):
""" Run method that exports data to a file"""
try:
self.export_dlg.show(self.get_layer_choices())
except CircleCraterError as error:
self.show_error(error.message)
def export(self, crater_layer, area_layer, filename):
try:
self.write_diam_file(crater_layer, area_layer, filename)
except CircleCraterError as error:
self.show_error(error.message)
def create_diam_header(self, total_area, crater_layer):
current_datetime = str(datetime.datetime.now())
# a,b = self.get_a_and_b(self.layer)
da = self.get_distance_area(self.layer)
if da.willUseEllipsoid():
header = [
'# Diam file for Craterstats',
'# Date of measurement export = {}'.format(current_datetime),
'',
'Ellipsoid {}'.format(da.ellipsoid()),
'',
'layer CRS: {}'.format(crater_layer.crs().description()),
'',
'Total Crater Area <km^2> = {}'.format(total_area),
'',
'# diameter(m), lon, lat',
'',
]
else:
header = [
'# Diam file for Craterstats',
'# Date of measurement export = {}'.format(current_datetime),
'',
'# Ellipsoid is not available, Area and Diameter unit may not right.',
'',
'Total Crater Area = {}'.format(total_area),
'',
'# diameter, lon, lat',
'',
]
return '\n'.join(header)
def write_diam_file(self, crater_layer, area_layer, filename):
"""Method writes crater data to a special formatted file."""
total_area = self.compute_area(area_layer)
km_squared = self.convert_square_meters_to_km(total_area)
header = self.create_diam_header(km_squared, crater_layer)
nested_list = self.format_diam_data(crater_layer, area_layer)
# tab delimited datafile
with open(filename, 'w') as fp:
fp.write(header)
fp.writelines('\t'.join(i) + '\n' for i in nested_list)
def get_distance_area(self, layer):
destination = layer.crs()
# Using the general purpose distance and area calculator,
# capable of performing ellipsoid based calculations.
distance_area = QgsDistanceArea()
c = QgsCoordinateTransformContext()
distance_area.setSourceCrs(layer.crs(), c )
ellips = destination.ellipsoidAcronym()
if ellips == '' :
ellips = QgsProject.instance().ellipsoid()
distance_area.setEllipsoid(ellips)
# sets whether coordinates must be projected to ellipsoid before measuring
# distance_area.setEllipsoidalMode(True)
return distance_area
def convert_meters_to_km(self, meters):
return meters * 0.001
def convert_square_meters_to_km(self, square_meters):
return square_meters * 1.0e-6
def measure(self, layer, geometry):
return self.get_distance_area(layer).measureLength(geometry)
def get_actual_area(self, feature, distance_area, xform):
# TODO: distance_area and xform should probably be class variables
QgsMessageLog.logMessage("message", "name")
print("======>",feature.geometry())
if feature.geometry().isMultipart(): # new part for multipolylines
points = feature.geometry().asMultiPolygon()
print("multipart:",len(points))
print("First point: ",points[0][0])
for p in points[0][0]:
print(p)
points = points[0][0]
else:
points = feature.geometry().asPolygon()
points = points[0]
transformed = [self.transform_point(xform, point) for point in points]
new_polygon = QgsGeometry.fromPolygonXY([transformed])
actual_area = distance_area.measureArea(new_polygon)
return actual_area
def compute_area(self, layer):
"""Returns values are in meters resp. square meters
http://qgis.org/api/2.8/classQgsDistanceArea.html
use measure() takes QgsGeometry as a parameter and calculates distance
or area
"""
destination = layer.crs()
source = layer.crs()
xform = self.crs_transform(source, destination)
distance_area = self.get_distance_area(layer)
features = list(layer.getFeatures())
return sum([self.get_actual_area(f, distance_area, xform) for f in features])
def get_fields(self, feature, diameter, lon, lat):
"""Retrieves fields from the attribute table in the order required
for .diam file: diameter, lon, lat
And casts as strings"""
# diameter is in units of km
attributes = feature.attributes()
# fraction is always 1
fraction = 1
# refer to an attribute by its index
field_list = [
str(self.convert_meters_to_km(attributes[diameter])),
#str(1),
# fraction was in old craterstats
# str(fraction),
str(attributes[lon]),
str(attributes[lat])
#str(1)
]
return field_list
def crater_center(self, crater, lat, lon):
print(crater)
print("ATT:",crater.attributes(),lat,lon,crater.attributes()[lon],crater.attributes()[lat] )
center_point = QgsPointXY(
float(crater.attributes()[lon]),
float(crater.attributes()[lat]),
)
return QgsGeometry.fromPointXY(center_point)
def experiment(self, feature_geom, point_geom):
"""
feature and point are geometrys
Is a QgsPoint within an arbitrary QgsPolygon?
"""
polygon = feature_geom.asPolygon()
point = point_geom.asPoint()
codes = []
codes.append(Path.MOVETO)
for i in range(0, len(polygon[0]) - 2):
codes.append(Path.LINETO)
codes.append(Path.CLOSEPOLY)
path = Path(polygon[0], codes)
if path.contains_point(point):
return True
else:
return False
def intersects(self, crater, area_geometries, lat, lon):
# This geometry is in units of degrees
center_geometry = self.crater_center(crater, lat, lon)
# temp = any(center_geometry.within(a) for a in area_geometries)
return any(self.experiment(a, center_geometry) for a in area_geometries)
def format_diam_data(self, crater_layer, area_layer):
"""Formats crater diameter data for export as .diam file
Checks to see if craters intersect with area polygons in area layer
"""
diameter = crater_layer.fields().indexFromName('diameter')
lon = crater_layer.fields().indexFromName('center_lon')
lat = crater_layer.fields().indexFromName('center_lat')
craters = list(crater_layer.getFeatures())
areas = list(area_layer.getFeatures())
# TODO: distance_area and xform should probably be class variables
destination = crater_layer.crs()
source = area_layer.crs()
xform = self.crs_transform(source, destination)
distance_area = self.get_distance_area(area_layer)
# Get area geometry in units of degrees
new_geometries = [self.get_transformed_polygon(a, distance_area, xform) for a in areas]
# WARNING INTERSECTS
craters = [c for c in craters if self.intersects(c, new_geometries, lat, lon)]
print("CRATERS: ",craters)
# Craterstats 2.0 line is:
# crater = {diam, fraction, lon, lat, topo_scale_factor
# 12.0185588932 1 159.43028979 16.9521753319 1
return [self.get_fields(c, diameter, lon, lat) for c in craters]
def get_transformed_polygon(self, feature, distance_area, xform):
"""Returns transformd polygon geometry"""
# TODO: distance_area and xform should probably be class variables
if feature.geometry().isMultipart(): # new part for multipolylines
points = feature.geometry().asMultiPolygon()
print("multipart:",len(points))
print("First point: ",points[0][0])
for p in points[0][0]:
print(p)
points = points[0][0]
else:
points = feature.geometry().asPolygon()
points = points[0]
transformed = [self.transform_point(xform, point) for point in points]
print("TRANSFORMED->",transformed)
return QgsGeometry.fromPolygonXY( [transformed] )
def crs_transform(self, source, destination):
print(source, destination, QgsProject.instance() )
return QgsCoordinateTransform(source, destination, QgsProject.instance())
def transform_point(self, xform, point):
return xform.transform(point)
def get_destination_crs(self):
# moon = '+proj=longlat +a=1737400 +b=1737400 +no_defs'
# destination = QgsCoordinateReferenceSystem()
# destination.createFromProj4(moon)
destination = self.layer.crs()
return destination
def get_latlong_srs(self):
p = QgsProject.instance()
e = p.ellipsoid()
crs = p.crs()
srs = osr.SpatialReference()
srs.ImportFromProj4(crs.toProj4())
a = srs.GetSemiMajor()
b = srs.GetSemiMinor()
proj4 = "+proj=latlong +a={} +b={}".format(a,b)
srs_ll = osr.SpatialReference()
srs_ll.ImportFromProj4(proj4)
return srs_ll
def get_srs(self):
p = QgsProject.instance()
e = p.ellipsoid()
crs = p.crs()
srs = osr.SpatialReference()
srs.ImportFromProj4(crs.toProj4())
return srs
def get_a_and_b(self,layer):
#this_crs = layer.crs()
#wkt = this_crs.toWkt()
#srs = osr.SpatialReference()
#srs.importFromWkt(wkt)
#print(srs)
#print(dir(this_crs))
p = QgsProject.instance()
e = p.ellipsoid()
crs = p.crs()
srs = osr.SpatialReference()
srs.ImportFromProj4(crs.toProj4())
print("******",e)
da = QgsDistanceArea()
da.willUseEllipsoid() # should be true
a = srs.GetSemiMajor()
b = srs.GetSemiMinor()
return a,b
def draw_circle(self, circle):
polygon = [QgsPointXY(*point) for point in circle.to_polygon()]
print(circle)
print(polygon)
print(type(polygon))
#gPnt = QgsGeometry.fromPointXY(QgsPointXY(1,1))
#gLine = QgsGeometry.fromPolyline([QgsPoint(1, 1), QgsPoint(2, 2)])
#gPolygon = QgsGeometry.fromPolygonXY([[QgsPointXY(1, 1), QgsPointXY(2, 2), QgsPointXY(2, 1)]])
#geometry = QgsGeometry.fromPolygon([polygon])
geometry = QgsGeometry.fromPolygonXY([polygon])
feature = QgsFeature()
feature.setGeometry(geometry)
feature.setFields(self.layer.fields())
destination = self.layer.crs()
source = self.layer.crs()
xform = self.crs_transform(source, destination)
#print circle.center.x, circle.center.y
#print(circle.center.x, circle.center.y)
#line = [
# QgsPointXY(circle.center.x, circle.center.y),
# QgsPointXY(circle.center.x + circle.radius, circle.center.y),
#]
line = [
QgsPointXY(circle.a, circle.b),
QgsPointXY(circle.a + circle.r, circle.b),
]
transformed = [
self.transform_point(xform, line[0]),
self.transform_point(xform, line[1]),
]
print("****",transformed)
#new_line_geometry = QgsGeometry.fromPolyline( [ QgsGeometry.fromPointXY(transformed[0]), QgsGeometry.fromPointXY(transformed[1]) ] )
new_line_geometry = QgsGeometry.fromPolyline([QgsPoint(transformed[0][0], transformed[0][1]), QgsPoint(transformed[1][0], transformed[1][1])])
distance_area = self.get_distance_area(self.layer)
actual_line_distance = distance_area.measureLength(new_line_geometry)
# Translate circle center to units of degrees
center_in_degrees = xform.transform(circle.a, circle.b)
# circle_feature.id() is NULL for .shp file
# and assigned automaticly for .gpkg
# order is id, diameter, lon, lat
feature.setAttribute('diameter',circle.diameter)
feature.setAttribute('center_lon',circle.a)
feature.setAttribute('center_lat',circle.b)
self.layer.startEditing()
self.layer.dataProvider().addFeatures([feature])
#self.layer.addFeature(feature, True)
self.layer.commitChanges()
# update layer's extent when new features have been added
# because change of extent in provider is not propagated to the layer
self.layer.updateExtents()