library("ggplot2")
library("sf")
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library("mapSpain")
cat <- esp_get_prov("Catalunya", resolution = "01", epsg = 4326)
expl <-
  read.csv(
    "https://analisi.transparenciacatalunya.cat/resource/7bpt-5azk.csv",
    fileEncoding = "UTF-8"
  )

class(expl)
## [1] "data.frame"
expl$longitud <- expl$longitud / 10 ^ 6
expl$latitud <- expl$latitud / 10 ^ 6



# Convierte a un objeto sf
expl <- st_as_sf(expl, coords = c("longitud", "latitud"))

# Asigna Longitud /Latitud
expl <- st_set_crs(expl, 4326)

expl <- st_transform(expl, st_crs(cat))
st_crs(cat) == st_crs(expl)
## [1] TRUE
class(expl)
## [1] "sf"         "data.frame"
# Comprueba por provincia

 

ggplot() +
  geom_sf(data = cat) +
  geom_sf(data = expl[expl$demarcaci_territorial == "Tarragona",])

ggplot() +
  geom_sf(data = cat) +
  geom_sf(data = expl[expl$demarcaci_territorial == "Barcelona",])

ggplot() +
  geom_sf(data = cat) +
  geom_sf(data = expl[expl$demarcaci_territorial == "Lleida",])

ggplot() +
  geom_sf(data = cat) +
  geom_sf(data = expl[expl$demarcaci_territorial == "Girona",])

# Geos operation - identifica puntos no en CAT

geosunion <- st_contains( st_union(cat), expl, sparse = FALSE)
## although coordinates are longitude/latitude, st_contains assumes that they are planar
encat <- expl[geosunion,]
ggplot() +
  geom_sf(data = cat) +
  geom_sf(data = encat)

noencat <- expl[geosunion == FALSE,]
ggplot() +
  geom_sf(data = cat) +
  geom_sf(data = noencat)

test <- noencat[noencat$nom_explotaci == "LONG-LINES CASES ALCANAR",]
test[,c("adre_a_explotaci","municipi")]
## Simple feature collection with 2 features and 2 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 0.560373 ymin: 40.53868 xmax: 0.560373 ymax: 40.53868
## geographic CRS: WGS 84
##                   adre_a_explotaci municipi                  geometry
## 560 CARRETERA NACIONAL 340 KM 1064  Alcanar POINT (0.560373 40.53868)
## 561 CARRETERA NACIONAL 340 KM 1064  Alcanar POINT (0.560373 40.53868)

Comprueba las coordenadas en Google Maps:

https://goo.gl/maps/AkgU2iDQC4qbf6ar9

Es Alcanar, parece ok