0

I am trying to use the nkde function of spNetworks to create a KDE of crashes in DC along its roadnetwork. In preparation for the function I am creating lixels for nkde, but running lines_center(lixels) always gives me an error.

lixels <- lixelize_lines(dc_lines,1000,mindist = 250)
samples <- lines_center(lixels)

Whenever I am trying to run lines_center(lixels) I get the following error:

Error in maptools::SpatialLinesMidPoints(with_length) : 
  is.projected(sldf) is not TRUE
In addition: Warning message:
In RGEOSMiscFunc(spgeom, byid, "rgeos_length") :
  Spatial object is not projected; GEOS expects planar coordinates

I tried looking up various techniques, such as turning the SpatialLinesDataFrame into a normal Dataframe, st_as_sf, and then projecting it, but nothing worked out thus far and I always get the same error.

I am loading the data like this:

dc <- readOGR("assessment/test/Roads_2013", "Roads_2013")

Since the uploaded file is a Large SpatialPolygonsDataFrame, I am transforming it into SpatialLines using this code:

dc_lines <- as(dc, "SpatialLinesDataFrame")

Any idea what I am doing wrong or how I can properly project the lines?

The shapefile used is here: https://opendata.dc.gov/datasets/roads

Luka K
  • 3
  • 3
  • Welcome to Stack Overflow! Please take the [tour] and read the [help], and ask on [meta] if you have any questions :) – Ekadh Singh - Reinstate Monica Dec 17 '21 at 17:34
  • 1
    @EkadhSingh-ReinstateMonica what are you suggesting they ask on meta? If they need to improve the question, they should do that here, not by asking either this question or general how-to questions on meta – camille Dec 17 '21 at 17:55
  • @camille if they have any questions about the rules, they should ask on meta is what I meant. I probably wasn't completely clear though. – Ekadh Singh - Reinstate Monica Dec 17 '21 at 18:01
  • 1
    @EkadhSingh-ReinstateMonica I wouldn't recommend blindly posting questions about site rules on meta though. The same guidelines of reading through the help center and existing posts to see if your question has been answered already, as well as putting a good question together apply on meta as they do here, but over there they're less gently enforced. i.e. that would be an easy way for a new user to catch a lot of downvotes – camille Dec 17 '21 at 18:07
  • 1
    Good that you've included a link to the data, but a fully [reproducible example](https://stackoverflow.com/q/5963269/5325862) could include how you read it in, especially since the issue seems to be with your CRS – camille Dec 17 '21 at 18:09
  • I've added the code I'm using to read it in, once I've created the dc_lines object I'm starting to use the code above. – Luka K Dec 17 '21 at 18:33

1 Answers1

0

I am the spNetwork developer. Please, consider posting your issue on the github for faster response. The problem here is that your dataset does not use a planar (X/Y coordinates in meters) CRS, but a geographical one (Lon/Lat in degrees). You need to reproject your data in an appropriate CRS with the function spTransform from sp package. Here is the link with some examples (https://www.rdocumentation.org/packages/rgdal/versions/1.5-28/topics/spTransform-methods).

Maybe the EPSG:2927 could be the CRS to use (I am not familiar with EPSG used in USA).

dc <- readOGR("assessment/test/Roads_2013", "Roads_2013")
dc_proj <- spTransform(dc, CRS("+init=epsg:2927"))