What does the error in the title mean? I'm fairly new to GIS and can't understand which negative length vectors R is talking about? Here is what I'm doing:

I have a csv of lat/long locations of certain points and a multiPolygon Shapefile of buildings in a particular city . I want to find the nearest polygon to each point. First I have converted the points and polygons to planar projections by the following code:


buildings<- spTransform(buildings, CRS("+proj=utm +zone=45 +a=6377299.151
                                        +towgs84=295,736,257,0,0,0,0 +units=m +no_defs "))



locations <- SpatialPointsDataFrame(coords = locations, data = locations,
                                    proj4string = 
                                      CRS("+proj=longlat +datum=WGS84"))
locations<- spTransform(locations, CRS("+proj=utm +zone=45 +a=6377299.151
                                        +towgs84=295,736,257,0,0,0,0 +units=m +no_defs "))

Then I find the minimum distance using the following formula:

minimum_distance<- apply(gDistance(locations, buildings,byid=TRUE),2,min)

But it gives me the error:

Error in RGEOSDistanceFunc(spgeom1, spgeom2, byid, "rgeos_distance") : 
  negative length vectors are not allowed

What is going wrong?

  • is it possible some of the points are inside the polygons? – Ian Turton Jul 17 at 9:12
  • Yes it is possible. – Ron Howard Jul 17 at 9:13
  • Hard for us to track down without your data, and nothing obviously wrong with the code. Subset locations and buildings and see if its down to a particular item in one or the other. – Spacedman Jul 17 at 9:43

Your Answer

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

Browse other questions tagged or ask your own question.