tobinbradley/dirt-simple-postgis-http-api

Unrecognized response type; displaying content as text.

Closed this issue · 5 comments

Hi!
I am trying to serve some tiles from PostGIS. So I cloned the repo and installed dependencies, then set up DB config, and fired it up.
I can access the Swagger UI docs and can query the DB on the meta section: I can list tables and columns.
It is not working properly, however, when I query using /v1/mvt/{table}/{z}/{x}/{y} to return MVTs.
I am getting a 200 response of "Unrecognized response type; displaying content as text." while testing it out in the features section of the documentation.

/v1/geojson/{table} returns features when specifying bounds using a Z/X/Y tile (0,0,0).

But /v1/geobuf/{table} also returns "Unrecognized response type; displaying content as text." and it looks like:

"���
k
i���e���X������������������������������������������������������������������
��
�����}�ߙY�Ԓ�������������������������������������������������������������������������������
��
�������ǝ�X�ƒ������������������������������������������ ��������������������������������������������������������������������������������������������
��

I am getting the same response when trying to consume the MVT by setting up a tile layer in a mapboxgl.Map instance.

Any pointers?
Thanks a lot!

anneb commented

The mvt and geobuf responses look good. The geobuf and mvt response are of type application/x-protobuf. This (binary) type cannot be displayed as human text. If you want to use tiled data, you have to use mvt.

You could try (very alpha!) https://github.com/anneb/pgserver, which is based on dirt-simple-postgis-http-api. pgserver includes a mapbox-gl previewer which might work with your data if it meets requirements (srid properly set, not too many rows, type (multi-)point, linestring or polygon.
disclosure: I am the author of (very alpha!) pgserver

@tapiamcclung that's what pbf tile looks like when you hit the endpoint from the browser and should render just fine on any mapping client supports vector tiles mainly (mapboxGL)
you can use maputnik for styling and quick viewing of your end point.

What @anneb and @mapsgeek said. A 200 generally means everything went OK, or at least the server thinks everything went OK. Protobuf is a binary format, so when viewed raw it does look like hot garbage.

There is some basic documentation in the readme on viewing the MVT and GeoJSON outputs with Mapbox GL JS. One of these days I should figure out how to fiddle with Swagger and add a custom renderer for the MVT output.

@anneb Thanks! Yeah, I figured that probably I would not be able to read a protobuf response and also thought there could be some sort of mismatch between content-type: application/x-protobuf and application/json. I'll check out pgserver.
@mapsgeek Thanks, I actually had set up a mapbox basic map prior to digging into what the responses were. Here's the relevant code to set up the map (adapted from here), where myTable in the tile section is a valid table name from my DB. While changing some other things I had mistakenly set source-layer: myTable (I think this is why I was getting the response from above) but have now changed it to default. I don't get the same scrambled response anymore, but still cannot display my MVTs, only the carto-dark base layer. While inspecting the responses in the console, I can see that requests to http://localhost:3000/v1/mvt/myTable/z/x/y say there's "nothing to preview" and that "this request has no request data available".

var map = new mapboxgl.Map({
    'container': 'map',
    'zoom': 7,
    'center': [-92.28, 17.22],
    'style': {
        'version': 8,
        'sources': {
            'carto-dark': {
                'type': 'raster',
                'tiles': [
                    "http://a.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
                    "http://b.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
                    "http://c.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
                    "http://d.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png"
                ]
            },
            'postgis-tiles': {
                'type': 'vector',
                'tiles': [
                    "http://localhost:3000/v1/mvt/myTable/{z}/{x}/{y}"
                ]
            }
        },
        'layers': [{
            'id': 'carto-dark-layer',
            'type': 'raster',
            'source': 'carto-dark',
            'minzoom': 0,
            'maxzoom': 22
        },{
            'id': 'postgis-tiles-layer',
            'type': 'fill',
            'source': 'postgis-tiles',
            // ST_AsMVT() uses 'default' as layer name
            'source-layer': 'default',
            'minzoom': 0,
            'maxzoom': 22,
            'paint': {
                'fill-opacity': 0.7,
                'fill-color': 'rgba(0, 255, 0, 0.5)',
                'fill-outline-color': 'rgba(0, 255, 0, 0.5)'
            }
        }]
    }
});

Might this not be the proper way to add the layer and should instead go with the

map.on('load', function() {
  map.addLayer({
  ...
  })
...
})

route?

If I set http://localhost:3000/v1/mvt/myTable/{z}/{x}/{y}?geom_column=geom&columns=someCol so that I can style the MVTs on the client I get the same scrambled response on the console.

Thanks for your answer too @tobinbradley!

Got it working with the map.on('load', ...}). Thanks.