Exemples d’API REST Importer

Configuration d’un répertoire des shapefiles de masse

Afin d’initier une importation de la c:\data\tasmania répertoire en l’workspace tasmania existant le JSON suivant sera affiché à GeoServer:

{
   "import": {
      "targetWorkspace": {
         "workspace": {
            "name": "tasmania"
         }
      },
      "data": {
        "type": "directory",
        "location": "C:/data/tasmania"
      }
   }
}

Cette commande curl peut être utilisée pour le but:

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @import.json "http://localhost:8080/geoserver/rest/imports"

L’importateur va rechercher les fichiers à importer et préparer automatiquement les tâches, en retournant la réponse suivante:

{
  "import": {
    "id": 9,
    "href": "http://localhost:8080/geoserver/rest/imports/9",
    "state": "PENDING",
    "archive": false,
    "targetWorkspace": {
      "workspace": {
        "name": "tasmania"
      }
    },
    "data": {
      "type": "directory",
      "format": "Shapefile",
      "location": "C:\\data\\tasmania",
      "href": "http://localhost:8080/geoserver/rest/imports/9/data"
    },
    "tasks": [
      {
        "id": 0,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/0",
        "state": "READY"
      },
      {
        "id": 1,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/1",
        "state": "READY"
      },
      {
        "id": 2,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/2",
        "state": "READY"
      },
      {
        "id": 3,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/3",
        "state": "READY"
      }
    ]
  }
}

Après avoir vérifié que chaque tâche est prête, l’importation peut être initiée en exécutant un POST sur les ressources d’importation:

curl -u admin:geoserver -XPOST "http://localhost:8080/geoserver/rest/imports/9"

La ressource peut alors être surveillée pour le progrès et les résultats définitifs par la suite:

curl -u admin:geoserver -XGET "http://localhost:8080/geoserver/rest/imports/9"

Qui dans le cas d’importation réussie va ressembler:

{
  "import": {
    "id": 9,
    "href": "http://localhost:8080/geoserver/rest/imports/9",
    "state": "COMPLETE",
    "archive": false,
    "targetWorkspace": {
      "workspace": {
        "name": "tasmania"
      }
    },
    "data": {
      "type": "directory",
      "format": "Shapefile",
      "location": "C:\\data\\tasmania",
      "href": "http://localhost:8080/geoserver/rest/imports/9/data"
    },
    "tasks": [
      {
        "id": 0,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/0",
        "state": "COMPLETE"
      },
      {
        "id": 1,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/1",
        "state": "COMPLETE"
      },
      {
        "id": 2,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/2",
        "state": "COMPLETE"
      },
      {
        "id": 3,
        "href": "http://localhost:8080/geoserver/rest/imports/9/tasks/3",
        "state": "COMPLETE"
      }
    ]
  }
}

Configuration d’un shapefile avec aucune information de projection

Dans ce cas, supposons que nous avons un seul shapefile, tasmania_cities.shp, qui n’a pas le fichier d’accessoires .prj (l’exemple est tout aussi bon pour tous les cas où le contenu du fichier prj ne peut pas correspondre à un code EPSG officiel).

Nous allons afficher la définition d’importation suivants:

{
   "import": {
      "targetWorkspace": {
         "workspace": {
            "name": "tasmania"
         }
      },
      "data": {
        "type": "file",
        "file": "C:/data/tasmania/tasmania_cities.shp"
      }
   }
}

Avec l’usuel curl commande:

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @import.json "http://localhost:8080/geoserver/rest/imports"

La réponse dans le cas où les CRS sont manquant sera:

{
  "import": {
    "id": 13,
    "href": "http://localhost:8080/geoserver/rest/imports/13",
    "state": "PENDING",
    "archive": false,
    "targetWorkspace": {
      "workspace": {
        "name": "tasmania"
      }
    },
    "data": {
      "type": "file",
      "format": "Shapefile",
      "file": "tasmania_cities.shp"
    },
    "tasks": [
      {
        "id": 0,
        "href": "http://localhost:8080/geoserver/rest/imports/13/tasks/0",
        "state": "NO_CRS"
      }
    ]
  }
}

Percer la couche de la tâche, nous pouvons voir les informations du srs sont manquantes:

{
  "layer": {
    "name": "tasmania_cities",
    "href": "http://localhost:8080/geoserver/rest/imports/13/tasks/0/layer",
    "title": "tasmania_cities",
    "originalName": "tasmania_cities",
    "nativeName": "tasmania_cities",
    "bbox": {
      "minx": 146.2910004483,
      "miny": -43.85100181689,
      "maxx": 148.2910004483,
      "maxy": -41.85100181689
    },
    "attributes": [
      {
        "name": "the_geom",
        "binding": "com.vividsolutions.jts.geom.MultiPoint"
      },
      {
        "name": "CITY_NAME",
        "binding": "java.lang.String"
      },
      {
        "name": "ADMIN_NAME",
        "binding": "java.lang.String"
      },
      {
        "name": "CNTRY_NAME",
        "binding": "java.lang.String"
      },
      {
        "name": "STATUS",
        "binding": "java.lang.String"
      },
      {
        "name": "POP_CLASS",
        "binding": "java.lang.String"
      }
    ],
    "style": {
      "name": "tasmania_tasmania_cities2",
      "href": "http://localhost:8080/geoserver/rest/imports/13/tasks/0/layer/style"
    }
  }
}

La demande PUT suivante met à jour le SRS:

curl -u admin:geoserver -XPUT -H "Content-type: application/json" -d @layerUpdate.json "http://localhost:8080/geoserver/rest/imports/13/tasks/0/layer/"

Où est la layerUpdate.json:

{
   layer : {
      srs: "EPSG:4326"
   }
}

Obtenir la définition d’importation encore une fois, nous le trouverons prêt à exécuter:

{
  "import": {
    "id": 13,
    "href": "http://localhost:8080/geoserver/rest/imports/13",
    "state": "PENDING",
    "archive": false,
    "targetWorkspace": {
      "workspace": {
        "name": "tasmania"
      }
    },
    "data": {
      "type": "file",
      "format": "Shapefile",
      "file": "tasmania_cities.shp"
    },
    "tasks": [
      {
        "id": 0,
        "href": "http://localhost:8080/geoserver/rest/imports/13/tasks/0",
        "state": "READY"
      }
    ]
  }
}

Une requête POST le fera exécuter:

curl -u admin:geoserver -XPOST "http://localhost:8080/geoserver/rest/imports/13"

Et finalement réussir:

{
  "import": {
    "id": 13,
    "href": "http://localhost:8080/geoserver/rest/imports/13",
    "state": "COMPLETE",
    "archive": false,
    "targetWorkspace": {
      "workspace": {
        "name": "tasmania"
      }
    },
    "data": {
      "type": "file",
      "format": "Shapefile",
      "file": "tasmania_cities.shp"
    },
    "tasks": [
      {
        "id": 0,
        "href": "http://localhost:8080/geoserver/rest/imports/13/tasks/0",
        "state": "COMPLETE"
      }
    ]
  }
}

Télécharger un fichier CSV vers PostGIS tout en transformant

Outil de télé détection génère des fichiers CSV avec des emplacements et des mesures, que nous voulons charger dans PostGIS comme une nouvelle table spatiale. Le fichier CSV se présente comme suit:

AssetID, SampleTime, Lat, Lon, Value
1, 2015-01-01T10:00:00, 10.00, 62.00, 15.2
1, 2015-01-01T11:00:00, 10.10, 62.11, 30.25
1, 2015-01-01T12:00:00, 10.20, 62.22, 41.2
1, 2015-01-01T13:00:00, 10.31, 62.33, 27.6
1, 2015-01-01T14:00:00, 10.41, 62.45, 12

Tout d’abord, nous allons créer une importation vide avec un magasin de postgis existant comme cible:

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @import.json "http://localhost:8080/geoserver/rest/imports"

Où import.json est:

{
   "import": {
      "targetWorkspace": {
         "workspace": {
            "name": "topp"
         }
      },
      "targetStore": {
         "dataStore": {
            "name": "gttest"
         }
      }
   }
}

Ensuite, nous allons POST du fichier csv à la liste des tâches, afin de créer une tâche d’importation pour elle:

curl -u admin:geoserver -F name=test -F filedata=@values.csv "http://localhost:8080/geoserver/rest/imports/0/tasks"

Et nous allons récupérer une nouvelle définition de la tâche, avec une notification que la CRS est manquante:

{
  "task": {
    "id": 0,
    "href": "http://localhost:8080/geoserver/rest/imports/16/tasks/0",
    "state": "NO_CRS",
    "updateMode": "CREATE",
    "data": {
      "type": "file",
      "format": "CSV",
      "file": "values.csv"
    },
    "target": {
      "href": "http://localhost:8080/geoserver/rest/imports/16/tasks/0/target",
      "dataStore": {
        "name": "values",
        "type": "CSV"
      }
    },
    "progress": "http://localhost:8080/geoserver/rest/imports/16/tasks/0/progress",
    "layer": {
      "name": "values",
      "href": "http://localhost:8080/geoserver/rest/imports/16/tasks/0/layer"
    },
    "transformChain": {
      "type": "vector",
      "transforms": [

      ]
    }
  }
}

Comme avant, nous allons forcer les CRS en mettant à jour la couche:

curl -u admin:geoserver -XPUT -H "Content-type: application/json" -d @layerUpdate.json "http://localhost:8080/geoserver/rest/imports/0/tasks/0/layer/"

Où est la layerUpdate.json:

{
   layer : {
      srs: "EPSG:4326"
   }
}

Ensuite, nous allons créer une transformation, mappez les colonnes de Lat/Lon jusqu’à un point situé:

{
  "type": "AttributesToPointGeometryTransform",
  "latField": "Lat",
  "lngField": "Lon"
}

Ce qui précède est téléchargé sur GeoServer comme suit:

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @toPoint.json "http://localhost:8080/geoserver/rest/imports/0/tasks/0/transforms"

Maintenant, l’importation est prête à fonctionner, et nous allons l’exécuter à l’aide de:

curl -u admin:geoserver -XPOST "http://localhost:8080/geoserver/rest/imports/0"

Si tout va bien le nouveau calque est créé dans PostGIS et enregistré dans GeoServer comme un nouveau calque.

Dans le cas où les caractéristiques dans le fichier CSV doivent être ajoutées à un calque existant une demande PUT contre la tâche peut être exécutée, changeant son updateMode de “Créer” à “Ajouter”. Changer pour “Remplacer” au lieu de cela va préserver la couche, mais enlever les matières anciennes et remplacez-les par ceux nouvellement téléchargés.

Télécharger et optimiser un GeoTiff avec points de contrôle au sol

Un fournisseur de données fournit périodiquement GeoTiffs dont nous avons besoin de configurer dans GeoServer. Le GeoTIFF est référencé par l’intermédiaire de Points de contrôle au sol, est organisée par des bandes et n’a aucuns aperçus. L’objectif est de réparer, optimiser et publier par l’intermédiaire de l’importateur.

Tout d’abord, nous allons créer une importation vide sans magasin comme cible:

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @import.json "http://localhost:8080/geoserver/rest/imports"

Où import.json est:

{
   "import": {
      "targetWorkspace": {
         "workspace": {
            "name": "sf"
         }
      }
   }
}

Ensuite, nous allons poster le fichier GeoTiff à la liste de tâches, afin de créer une tâche d’importation pour elle:

curl -u admin:geoserver -F name=test -F filedata=@box_gcp_fixed.tif "http://localhost:8080/geoserver/rest/imports/0/tasks"

Nous sommes donc ajouter les transformations pour rectifier (gdalwarp), retile (gdal_translate) et y ajouter les aperçus (gdaladdo):

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @warp.json "http://localhost:8080/geoserver/rest/imports/0/tasks/0/transforms"
curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @gtx.json "http://localhost:8080/geoserver/rest/imports/0/tasks/0/transforms"
curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @gad.json "http://localhost:8080/geoserver/rest/imports/0/tasks/0/transforms"

warp.json is:

{
  "type": "GdalWarpTransform",
  "options": [ "-t_srs", "EPSG:4326"]
}

gtx.json is:

{
  "type": "GdalTranslateTransform",
  "options": [ "-co", "TILED=YES", "-co", "BLOCKXSIZE=512", "-co", "BLOCKYSIZE=512"]
}

gad.json is:

{
  "type": "GdalAddoTransform",
  "options": [ "-r", "average"],
  "levels" : [2, 4, 8, 16]
}

Maintenant, l’importation est prête à fonctionner, et nous allons l’exécuter à l’aide de:

curl -u admin:geoserver -XPOST "http://localhost:8080/geoserver/rest/imports/0"

Une nouvelle couche de box_gcp_fixed apparaîtra dans GeoServer, avec un fichier GeoTiff sous-jacent prêt pour le service web.

Ajout d’un granule de nouveau en une mosaïque existante

Un fournisseur de données fournit périodiquement de nouvelles images de base dont nous avons besoin d’ajouter en une mosaïque existante dans GeoServer. L’imagerie est au format GeoTiff et manque d’une bonne structure interne, qui doit être alignée sur celle des autres images.

Tout d’abord, nous allons créer une importation en indiquant où se trouve le granule et la banque cible :

curl -u admin:geoserver - XPOST -H “Content-type : application/json” le @import.json -d “http://localhost:8080/geoserver/repos/imports”

Où import.json est:

{
   "import": {
      "targetWorkspace": {
         "workspace": {
            "name": "topp"
         }
      },
      "data": {
        "type": "file",
        "file": "/home/aaime/devel/gisData/ndimensional/data/world/world.200407.3x5400x2700.tiff"
      },
      "targetStore": {
         "dataStore": {
            "name": "bluemarble"
         }
      }
   }
}

Nous allons ensuite ajouter les transformations afin d’harmoniser le fichier avec le reste de la mosaïque:

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @gtx.json "http://localhost:8080/geoserver/rest/imports/0/tasks/0/transforms"
curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @gad.json "http://localhost:8080/geoserver/rest/imports/0/tasks/0/transforms"

gtx.json is:

{
  "type": "GdalTranslateTransform",
  "options": [ "-co", "TILED=YES"]
}

gad.json is:

{
  "type": "GdalAddoTransform",
  "options": [ "-r", "average"],
  "levels" : [2, 4, 8, 16, 32, 64, 128]
}

Maintenant, l’importation est prête à fonctionner, et nous allons l’exécuter à l’aide de:

curl -u admin:geoserver -XPOST "http://localhost:8080/geoserver/rest/imports/0"

La nouvelle granule sera ingérée dans la mosaïque et sera donc disponible pour les demandes de temps basé.

Extraction de façon asynchrone et importation de données depuis un serveur distant

Nous supposons qu’un serveur FTP distant contient des shapefiles multiples que nous devons importer dans GeoServer comme nouveaux calques. Les fichiers sont volumineux, et le serveur a une meilleure largeur de bande que le client, il est donc préférable si GeoServer effectue la récupération de donnéespar soi meme

Dans ce cas, une demande asynchrone à l’aide de données distantes sera le meilleur ajustement:

curl -u admin:geoserver -XPOST -H "Content-type: application/json" -d @import.json "http://localhost:8080/geoserver/rest/imports?async=true"

Où import.json est:

{
   "import": {
      "targetWorkspace": {
         "workspace": {
            "name": "topp"
         }
      },
      "data": {
        "type": "remote",
        "location": "ftp://myserver/data/bc_shapefiles",
        "username": "dan",
        "password": "secret"
      }
   }
}

La requête retournera immédiatement avec un contexte d’importation en l’État “INIT”, et il reste dans cet état jusqu’à ce que les données sont lues et les tâches créées. Une fois les commutateurs de l’Etat à “PENDING” l’importation sera prête pour l’exécution. Puisqu’il n’y a pas beaucoup de shapefiles à traiter, également l’importation exécutée se fera en mode asynchrone:

curl -u admin:geoserver -XPOST "http://localhost:8080/geoserver/rest/imports/0?async=true"

La réponse retournera immédiatement dans ce cas également, et les progrès peuvent être suivis comme les tâches dans l’état de commutation d’importation.