DS4PS / cpp-529-fall-2020

http://ds4ps.org/cpp-529-fall-2020/
0 stars 0 forks source link

Writing geojson error #32

Open JasonSills opened 3 years ago

JasonSills commented 3 years ago

Hi @lecy,

Wondering if you can help clarify the error Error in sp::SpatialPolygonsDataFrame(polys, data = input@data) : row.names of data and Polygons IDs do not match

  1. stop("row.names of data and Polygons IDs do not match")
  2. sp::SpatialPolygonsDataFrame(polys, data = input@data)
  3. write_ogr_sf(input, file, precision, overwrite, convert_wgs84 = convert_wgs84, crs = crs, ...)
  4. write_geojson(input, file, precision = precision, convert_wgs84 = convert_wgs84, crs = crs, ...)
  5. geojson_write.SpatialPolygonsDataFrame(sea, file = "sea_dorling.geojson", geometry = "polygon")
  6. geojson_write(sea, file = "sea_dorling.geojson", geometry = "polygon")

I receive this at the end of constructing my dataset to import to the dashboard.

Here is my code:

library( geojsonio )
seattle_dorling <- spTransform( sea, CRS("+proj=longlat +datum=WGS84") )
geojson_write( sea, file="sea_dorling.geojson", geometry="polygon" )

It seems that this is indicating a data merging error. However, I'm able to create a cartogram with the 2000 median home value calculation, so that appears to work.

I'm merging with:


sea <- merge( seattle_dorling, d, by.x="GEOID", by.y="geoid", all.x=TRUE )

Where the y="geoid" was created from the census data using a regex to match to the GEOID.

lecy commented 3 years ago

Sure, check your variable names before the geojson_write() step.

names( sea ) %>% sort()

Have you double-merged datasets, which results in some variable names with the suffix var.x and var.y?

That was creating this error for another student. I would revisit the dataset and make sure you are only adding census data once, and that you are not duplicating merge steps.

JasonSills commented 3 years ago

Interesting. I'm only seeing both one time for the variable mhmval12. There is also a pop00.x, but no pop00.y.

If this was a double-merging issue wouldn't I see it with multiple variables?

lecy commented 3 years ago

Can you email me your RMD for your data steps?

lecy commented 3 years ago

Alright, this error occurs when you drop a tract at some stage.

If you have N tracts in your shapefile then the polygon and data frame row IDs are both labeled 1:N.

If you drop a tract then either the data frame rows or the polygon IDs are getting renames 1:(N-1) while the other is just skipping a number. This causes a mis-alignment of IDs between the polygons and data frame (which is how the sp package matches data between the two).

So you might have to align IDs by assigning the polygon IDs as row names before writing to file:

library( geojsonio )

# data frame and polygon ID standardization in case a tract was dropped and IDs don't match
row.ids <- sapply( slot( phx_dorling, "polygons" ), function(x) slot( x, "ID" ) )
row.names( phx_dorling ) <- row.ids

phx <- spTransform( phx, CRS("+proj=longlat +datum=WGS84") )
geojson_write( phx, file="phx_dorling.geojson", geometry="polygon" )