I want to calculate the MBR for the column of table.

I have already install Postgis and postgresql.

the data is cvs enter image description here

and I load the data to my postgresql-table by [copy]

and my table scheme is enter image description here

and When I running

SELECT ST_Envelope (wkb_geometry::geometry) from test1;

OR running

SELECT PostGIS_ADDBBox(wek_geometry) from test1;

the shell prompt me

ERROR:Invalid endian flag value encountered.

I just want to create the Minimum bounding box(MBR) to my table(by postgis,and using web_geometry column),but I don't know what should I using the function and what the correct syntax


If i read it correct you import a WKT and then you try to cast wkb_geometry to geometry. You picture shows a text file with WKT and fields of your table called wkb_geometry. How do you imported the data? Either you have a real binary field where you threw in text or your field has a different name compared to its content.

WKT and WKB are completly different things, you can´t mix them. One is stored as text the other as binary format. Therefore different tools are needed. I don´t think that the normal cast function will work here anyway, as this is a postgresql standard function and has nothing to do with geometries, you need the PostGIS functions. If you really have a WKB you need the function ST_GeomFromWKB.

Select ST_Envelope(StGeomfromWKB(your field, your coordinate reference as srid))

Or for WKT you go with STGeomFromText:

SELECT ST_Envelope(STGeomFromText(your field, your coordinate reference as srid))
  • I load the data by ogr2ogr(.shp -----> .csv ) and when I running Select ST_Envelope(St_GeomFromWKB(wkb_geometry, 4326)) from test1; The prompt: ERROR: Invalid endian flag value encountered. and when I running: SELECT wkb_geometry From test1 where fnode_=1; the web_geometry is \x4c494e45535452494e4720283132312e32393636373932373136333234‌​39392033312e38353239‌​34383130313631323438‌​2c3132312e3239353838‌​333137383731 > 3039382033312e38343933353536393736333137393729 – user82538 Sep 14 '16 at 11:58
  • and when I running:` Select ST_GeomFromText(wkb_geometry,0) from test1;` the prompt > ERROR: function st_geomfromtext(bytea, integer) does not exist LINE > 1: Select ST_GeomFromText(wkb_geometry,0) from test1; > ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. I have already don't know what the wkb_geometry format – – user82538 Sep 14 '16 at 11:59
  • How did you bring the .csv into the database? Because it has WKT as geometry format and your column wkb. I guess that the error lies there. When you import the csv you need to have the same columns. Lets test the following: Create a new table with all columns named like the ones in your .csv file. The WKT-Column as text. Then COPY new_table FROM "file" . The copy command needs the same amount of columns in the input and output table. Try your query with the new table. – Matte Sep 14 '16 at 13:18
  • yep,I know use COPY should have same column, first I using ogr2ogr load the table(new table,named test),and I go to 【pgAdmin】to see the SQL of the table scheme. And I create new table(named test1) column and create column the same as the test. So I think the table(test1) have the same column .(Because the next step I want to using cities,so I must create new table) – user82538 Sep 15 '16 at 0:39
  • I already run CREATE table test2( WKT text,fnode_ integer,tnode_ integer,lpoly_ integer,rpoly_ integer,length double precision,riverf_ integer,riverf_id integer,code integer,gb integer,hydc text,tn integer,name text); and COPY test2 FROM '/home/pj/Desktop/test1.csv' CSV header; but prompt error: ERROR: extra data after last expected column CONTEXT: COPY test2, line 2: ""LINESTRING (121.28987121582 31.852741241455099,121.286529541015994 31.8526306152344,121.28036499023..." – user82538 Sep 15 '16 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.