1

I have a Postgres database with PostGIS extension and some geo-data in it. I need to generate tiles with custom SQL queries (using ST_... functions), that returns binary data (bytea, like \x1a320a0474657374121d12020000180322150946ec3f1a14453b0a09280f091413121e09091e0f1a026331220228012880207802). And I need to send this data from my Rails controller to the requester, with the headers like 'Content-Type: x-application/protobuf'.

So, my question is:

1) How to get the binary data from the Database (Sequel, ActiveRecord::Base.connection.execute ?) 2) How to return this data in the controller action? (send_data ..., tyle: '' ?)

Thanks!

4
  • Do you mean, that you need to use one of postgres function that will generate a binary data as an output and then you need to send this data to the client? Commented Nov 4, 2017 at 12:56
  • @AntonTkachov exactly! ActiveRecord::Base.connection.execute returns PG::Result object, and I don't sure that i can get raw binary data from it. Commented Nov 4, 2017 at 13:41
  • 1. Can you provide a sample of your query? 2. Does sql console return you a proper binary string, that you expect? Commented Nov 4, 2017 at 13:42
  • @AntonTkachov 1) SELECT ST_AsMVT(q, 'table', 512, 'geom') AS mvt FROM ( WITH a AS ( SELECT ST_AsMVTGeom( ST_Transform(public.table.wkb_geometry, 3857), TileBBox(13, 2411, 3078, 3857), 512, 256, true ) geom , columns FROM public.table WHERE ST_Intersects(TileBBox(13, 2411, 3078, 4326), public.table.wkb_geometry) ) SELECT * FROM a WHERE geom IS NOT NULL ) AS q , 2) yes, in form of Postgres (see example in my answer). How can I get raw data from PG::Result? Commented Nov 17, 2017 at 9:53

1 Answer 1

3

I just figured out the solution - hope it helps, although it's a long time ago..

records_array = ActiveRecord::Base.connection.execute(query)
result = records_array[0]['st_asmvt']
encoded = ActiveRecord::Base.connection.unescape_bytea(result)

ActiveRecord::Base.connection.unescape_bytea is the answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.