Skip to main content
Tweeted twitter.com/StackGIS/status/1485220794187792387
deleted 4 characters in body
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

I am trying to create a new vector memory layer with attribute data populated as follows &and cannot figure out why it works with integer or string typed data but silently does not populate any rows with double typed data.

def create_layer(columns: List[QgsField], data: List[Tuple]):
   layer = QgsVectorLayer('multipoint?crs=epsg:3347', "TEST", "memory")
   provider = layer.dataProvider()
   provider.addAttributes(columns)
   with edit(layer):
      for row in data:
         wkb, attrs = row[0], row[1:]
         geom = QgsGeometry()
         geom.fromWkb(bytearray(wkb))
         feature = QgsFeature()
         feature.setGeometry(geom)
         feature.setAttributes(list(attrs))
         provider.addFeatures([feature])
   return layer

assert create_layer(
   columns=[QgsField('weight', QVariant.Int)],
   data=list(zip(my_geoms, [int(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.String)],
   data=list(zip(my_geoms, [str(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.Double)],  # also tried QgsField('weight', QVariant.Double, 'double' , 8, 6)
   data=list(zip(my_geoms, [round(w, 2) for w in my_weights]))
).featureCount() > 0     # FAILS!

(theThe length of my_geoms & my_weights are the same & the values of my_weights are Python floats)

I am trying to create a new vector memory layer with attribute data populated as follows & cannot figure out why it works with integer or string typed data but silently does not populate any rows with double typed data.

def create_layer(columns: List[QgsField], data: List[Tuple]):
   layer = QgsVectorLayer('multipoint?crs=epsg:3347', "TEST", "memory")
   provider = layer.dataProvider()
   provider.addAttributes(columns)
   with edit(layer):
      for row in data:
         wkb, attrs = row[0], row[1:]
         geom = QgsGeometry()
         geom.fromWkb(bytearray(wkb))
         feature = QgsFeature()
         feature.setGeometry(geom)
         feature.setAttributes(list(attrs))
         provider.addFeatures([feature])
   return layer

assert create_layer(
   columns=[QgsField('weight', QVariant.Int)],
   data=list(zip(my_geoms, [int(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.String)],
   data=list(zip(my_geoms, [str(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.Double)],  # also tried QgsField('weight', QVariant.Double, 'double' , 8, 6)
   data=list(zip(my_geoms, [round(w, 2) for w in my_weights]))
).featureCount() > 0     # FAILS!

(the length of my_geoms & my_weights are the same & the values of my_weights are Python floats)

I am trying to create a new vector memory layer with attribute data populated as follows and cannot figure out why it works with integer or string typed data but silently does not populate any rows with double typed data.

def create_layer(columns: List[QgsField], data: List[Tuple]):
   layer = QgsVectorLayer('multipoint?crs=epsg:3347', "TEST", "memory")
   provider = layer.dataProvider()
   provider.addAttributes(columns)
   with edit(layer):
      for row in data:
         wkb, attrs = row[0], row[1:]
         geom = QgsGeometry()
         geom.fromWkb(bytearray(wkb))
         feature = QgsFeature()
         feature.setGeometry(geom)
         feature.setAttributes(list(attrs))
         provider.addFeatures([feature])
   return layer

assert create_layer(
   columns=[QgsField('weight', QVariant.Int)],
   data=list(zip(my_geoms, [int(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.String)],
   data=list(zip(my_geoms, [str(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.Double)],  # also tried QgsField('weight', QVariant.Double, 'double' , 8, 6)
   data=list(zip(my_geoms, [round(w, 2) for w in my_weights]))
).featureCount() > 0     # FAILS!

(The length of my_geoms & my_weights are the same & the values of my_weights are Python floats)

fix code typo
Source Link
eliangius
  • 285
  • 1
  • 8

I am trying to create a new vector memory layer with attribute data populated as follows & cannot figure out why it works with integer or string typed data but silently does not populate any rows with double typed data.

def create_layer(columns: List[QgsField], data: List[Tuple]):
   layer = QgsVectorLayer('multipoint?crs=epsg:3347', "TEST", "memory")
   provider = layer.dataProvider()
   provider.addAttributes(columns)
   with edit(layer):
      for row in data:
         wkb, attrs = row[0], row[1:]
         geom = QgsGeometry()
         geom.fromWkb(bytearray(wkb))
         feature = QgsFeature()
         feature.setGeometry(geom)
         feature.setAttributes(list(attrs))
         provider.addFeatures([feature])
   return layer

assert create_layer(
   columns=[QgsField('weight', QVariant.Int)],
   data=list(zip(my_geoms, [int(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.String)],
   data=list(zip(my_geoms, [str(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.Double)],  # also tried QgsField('weight', QVariant.Double, 'double' , 8, 6)
   data=list(zip(my_geoms, [round(w, 2) for w in my_weights]))
).featureCountsfeatureCount() > 0     # FAILS!

(the length of my_geoms & my_weights are the same & the values of my_weights are Python floats)

I am trying to create a new vector memory layer with attribute data populated as follows & cannot figure out why it works with integer or string typed data but silently does not populate any rows with double typed data.

def create_layer(columns: List[QgsField], data: List[Tuple]):
   layer = QgsVectorLayer('multipoint?crs=epsg:3347', "TEST", "memory")
   provider = layer.dataProvider()
   provider.addAttributes(columns)
   with edit(layer):
      for row in data:
         wkb, attrs = row[0], row[1:]
         geom = QgsGeometry()
         geom.fromWkb(bytearray(wkb))
         feature = QgsFeature()
         feature.setGeometry(geom)
         feature.setAttributes(list(attrs))
         provider.addFeatures([feature])
   return layer

assert create_layer(
   columns=[QgsField('weight', QVariant.Int)],
   data=list(zip(my_geoms, [int(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.String)],
   data=list(zip(my_geoms, [str(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.Double)],  # also tried QgsField('weight', QVariant.Double, 'double' , 8, 6)
   data=list(zip(my_geoms, [round(w, 2) for w in my_weights]))
).featureCounts() > 0     # FAILS!

(the length of my_geoms & my_weights are the same & the values of my_weights are Python floats)

I am trying to create a new vector memory layer with attribute data populated as follows & cannot figure out why it works with integer or string typed data but silently does not populate any rows with double typed data.

def create_layer(columns: List[QgsField], data: List[Tuple]):
   layer = QgsVectorLayer('multipoint?crs=epsg:3347', "TEST", "memory")
   provider = layer.dataProvider()
   provider.addAttributes(columns)
   with edit(layer):
      for row in data:
         wkb, attrs = row[0], row[1:]
         geom = QgsGeometry()
         geom.fromWkb(bytearray(wkb))
         feature = QgsFeature()
         feature.setGeometry(geom)
         feature.setAttributes(list(attrs))
         provider.addFeatures([feature])
   return layer

assert create_layer(
   columns=[QgsField('weight', QVariant.Int)],
   data=list(zip(my_geoms, [int(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.String)],
   data=list(zip(my_geoms, [str(w) for w in my_weights]))
).featureCount() > 0   # PASS

assert create_layer(
   columns=[QgsField('weight', QVariant.Double)],  # also tried QgsField('weight', QVariant.Double, 'double' , 8, 6)
   data=list(zip(my_geoms, [round(w, 2) for w in my_weights]))
).featureCount() > 0     # FAILS!

(the length of my_geoms & my_weights are the same & the values of my_weights are Python floats)

edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
title case; naming
Source Link
Vince
  • 20.3k
  • 16
  • 48
  • 65
Loading
Source Link
eliangius
  • 285
  • 1
  • 8
Loading