Saturday, 11 February 2017

RESTFull GEOJson with Swagger and Slim

I often find myself having to write RESTFull api for small project.  So I have search and tried several tools. I finally choose to use Swagger to write the documentation and Slim micro framework to implements it.

The first step is writing the yalm file in the Swagger editor (there is a nice tutorial at Api Handyman blog). With the yalm specification file it's simple to build an html documentation. Swagger have two pre build schema in html (html and html). You can export it, from Swagger Editor with Generate Client button (where you can export other languages client code).

 To create the Slim skeleton, simply click on the Generate server button and choose Slim. Now you have two zip file that contains documentation and the skeleton code.

Usually my project folder is composed by three subfolders: app (with the api code), api_doc (with the documentation generated by Swagger) and then an db_doc (the database documentation with diagram).

To provide a geographic information the request retrive or send a GEOJson, so I have write a  yalm gist as example.

To store the data I use PostgresSQL/PostGIS, using this query to retrive the data:
 SELECT row_to_json(featuresCollection)
          FROM ( SELECT 'FeatureCollection' As type,
          array_to_json(array_agg(features)) As features
          FROM (SELECT 'Feature' As type
         ,ST_AsGeoJSON(data.the_geom, 4)::json As geometry
         , row_to_json((SELECT prop FROM (SELECT property1, property2.....) As prop         )) As properties
         FROM myTable As data ) As features ) As featuresCollectionc;

No comments:

Post a Comment