1

I have created an SpatialPointsDataFrame in R with some data. Here is the outputs from R:

> class(Fotos)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

> class(Fotos$Azimuth)
[1] "numeric"

> Fotos$Azimuth
  [1]  NA  NA  NA  NA   0  NA  NA  NA  NA  NA
 [11]  45  NA  90  NA  NA  NA 135  NA  NA  NA
 [21]  NA 180  NA  NA  NA  NA  NA  NA 225  NA
 [31]  NA  NA  NA  NA 270 315  NA  NA  NA  NA
 ...

> class(Fotos$labelpos)
[1] "numeric"

> Fotos$labelpos
  [1] NA NA NA NA  0 NA NA NA NA NA  0 NA  0 NA
 [15] NA NA  0 NA NA NA NA  0 NA NA NA NA NA NA
 [29]  0 NA NA NA NA NA  0  1 NA NA NA NA NA NA
 [43] NA NA NA NA NA NA  0 NA NA NA NA NA  1 NA
 ...

It seems that both Attributes have the same data structure and class but when I export this SPDF to .gpkg some attributes replace NA by NULL and others replace NA by nothing. Like here:

enter image description here

How could I homogeneize those null/na/empty values in the .gpkg exported from R? I tryed something like this without fourtune:

Fotos@data[is.na(Fotos@data)] <- NA

EDIT: I tried to homogenize those NA with this:

 # Homonegeneizar NAs
    storage.mode(Fotos$OrdenSel) <- "integer"
    storage.mode(Fotos$Plano) <- "integer"
    storage.mode(Fotos$Cluster) <- "integer"
    storage.mode(Fotos$labelpos) <- "integer"
    storage.mode(Fotos$Azimuth) <- "integer"

But QGIS read those values as integers, but some has NULL and others empty... enter image description here

  • Is that table a screengrab from QGIS? What does it think the types of the columns are? This is a bit strange and I dont think we can investigate without your Fotos data, or a sample of it... – Spacedman Nov 14 at 21:09
1

If I create a test spatial data frame and write to a GeoPackage:

 Fotos = data.frame(x=1:10,y=1:10, Azimuth=sample(180,10), labelpos=sample(c(0,1),10,TRUE))
 Fotos$labelpos[c(2,5,6)]=NA
 Fotos$Azimuth[c(2,5,6)]=NA
 coordinates(Fotos)=~x+y
 writeOGR(Fotos,"Test.gpkg",layer="Fotos",driver="GPKG")

Then I see that QGIS has interpreted the Azimuth column as "int" and the labelpos column as "double":

enter image description here

The missing values are then shown as you see them:

enter image description here

So I think its a difference in the way QGIS displays missing values in integer or real attribute columns. For an int it says NULL, and for a double it shows blank.

Looking at the storage mode of each column reveals why QGIS thinks this way:

> storage.mode(Fotos$labelpos)
[1] "double"
> storage.mode(Fotos$Azimuth)
[1] "integer"

that has gone through into the schema in the GeoPackage:

CREATE TABLE "Fotos" ( 
     "fid" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
     "geom" POINT,
     "Azimuth" MEDIUMINT,
     "labelpos" REAL);

Quite why R creates them with those storage modes, or why QGIS uses different text for missing data in ints and reals is another question...

But there is clearly no reason to try and clean up your data frame - its as good as it gets.

  • But I already tried to transform those attributes into integer (as.integer) and double and the result keeps being the same. Could I change that storage.mode with a function ? – César Arquero Nov 15 at 8:19
  • Yes, storage.mode(dataframe$column)="integer" will do that but I don't see the point in messing with the data purely to make it look different in QGIS attribute table. – Spacedman Nov 15 at 8:37
  • I've used that function but QGIS keeps bringing up the same result. The this here is that I need same NA values because I'm using simbology based on rules and empty and NULL rules different. I want to get rid of that thinking differently part and apply the the same system when value or not within an attribute. – César Arquero Nov 15 at 10:00
  • The conversion could be happening in the writing to geopackage (check the schema in the database) or in QGIS' interpretation of the data in the geopackage. This will require further investigation... – Spacedman Nov 15 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.