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

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 DATA

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

##LATITUDES/LONGITUDES DATA

locations <- SpatialPointsDataFrame(coords = locations, data = locations,
proj4string =
CRS("+proj=longlat +datum=WGS84"))
locations<- spTransform(locations, CRS("+proj=utm +zone=45 +a=6377299.151
+b=6356098.145120132
+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 '19 at 9:12
• Yes it is possible. – Ron Howard Jul 17 '19 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 '19 at 9:43