By Rupert
postgres
Buffering points
Jul 8th
Here is a post in buffering points and combining them using GeomUnion. Works in GEOS3.0.0, so note that your POSTGIS installation in windows contains a lower version of GEOS.
DROP FUNCTION combineGeometry(); CREATE FUNCTION combineGeometry() RETURNS Void AS' DECLARE --define datatypes here updateCount integer DEFAULT 0; geom_record RECORD; geom_current RECORD; geom_final RECORD; mytotal integer DEFAULT 0; BEGIN --get the total SELECT count(*) as mycount FROM busstopv1_buffer_20_temp INTO mytotal; -- 1,2,3,4,5 FOR geom_record IN SELECT * FROM busstopv1_buffer_20 LOOP --get the current geom of the record SELECT * FROM busstopv1_buffer_20_temp b WHERE b.gid = geom_record.gid INTO geom_current; RAISE INFO ''RECORD GID: %'', geom_record.gid; RAISE INFO ''===========TOTAL COUNT: %=============='', mytotal; --find the intersection of the current geom with other spatial entities --and loop through that. For each loop, update the geom. IF geom_current IS NULL THEN CONTINUE; END IF; FOR geom_final IN SELECT GeomUnion(b0.the_geom, geom_current.the_geom ) AS geom_current_union, b0.gid FROM busstopv1_buffer_20_temp b0 WHERE b0.gid <> geom_current.gid AND Intersects(b0.the_geom, geom_current.the_geom) = ''t'' ORDER BY gid ASC LOOP --geom_current.gid = geom_record.gid UPDATE busstopv1_buffer_20_temp SET the_geom = GeomUnion( the_geom, geom_final.geom_current_union ) WHERE gid = geom_current.gid; RAISE INFO ''UPDATED GID:%'', geom_current.gid; DELETE FROM busstopv1_buffer_20_temp WHERE gid = geom_final.gid; RAISE INFO ''DELETED GID:%'', geom_final.gid; mytotal = mytotal - 1; END LOOP; END LOOP; END;' LANGUAGE plpgsql; SELECT combineGeometry();
Next step.. trying out CGAL.
PostgresSQL / PostGIS CheatSheat
Jul 1st
This is a quick-command list of Postres. If you want detailed instructions, please visit the Postgres Manual.
1. How do I Show all databases?
postgres=# \l
List of databases
Name | Owner | Encoding
------------------+----------+----------
postgis | postgres | UTF8
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
template_postgis | postgres | UTF8
(5 rows)
Note: Do not drop template databases if not necessary.
2. How do I run a script from the prompt?
\i
OR
psql -d cybersoftbj -u user < myfile.sql
Its very usuful in reloading user-defined functions.
3. How do I create a user?
CREATE ROLE lbs WITH LOGIN PASSWORD 'tracking' SUPERUSER INHERIT CREATEDB CREATEROLE;
4. How to dump database in a text file?
pg_dump -U lbs -d cybersoftbjv1 -h 127.0.0.1 -W > cybersoftbjv1.sql
4. How to dump database cleanly?
pg_dump -c -d -E UTF8 -h 127.0.0.1 -U lbs -W platform_v1 > platform_v1.sql
5. How to update using two tables?
UPDATE road_for_update u
SET the_geom = r.the_geom
FROM roads r
WHERE r.rd_id = u.rd_id;
6. How to change a column type with Cast?
ALTER TABLE roads ALTER COLUMN class_new TYPE integer USING class_new::integer;
7. How to provide/restrict access privileges to tables?
GRANT SELECT ON TABLE table TO user;
REVOKE SELECT ON TABLE table FROM user;
8. How to add a geometry column to a table?
SYNTAX: AddGeometryColumn(
EXAMPLE: SELECT AddGeometryColumn(‘public’, ‘poi’, ‘the_geom’, 4326, ‘POINT’, 2)
ALTER TABLE class_aroundme RENAME "level 1" TO level_1; 10. Setting kernel shmmax for postgressysctl -w kernel.shmmax=134217728Note: For permanent changes see /etc/sysctl.cfg11. How to backup table(s) from pg_dump?
pg_dump poi_beijing -t class -t poi_class -f $BACKUPDIR/test_$MYDATE.sql
Preparing routing data for pgRouting
Apr 10th
1. It is important that you already have postgres, postgis, pgRouting installed in your machine.
A. The schema. Below is the sample schema that is a derivative of the kanagawa sample data from pgRouting. Take note of the source and target nodes, as well as the length and the node coordinates (x1,y1; x2,y2) of the line.
cybersoftbj=# \dt List of relations Schema | Name | Type | Owner --------+------------------+-------+---------- public | geometry_columns | TABLE | postgres public | roads | TABLE | postgres public | spatial_ref_sys | TABLE | postgres (3 rows)
cybersoftbj=# \d roads TABLE "public.roads" COLUMN | Type | Modifiers ------------+------------------------+----------------------------------------------------- gid | integer | NOT NULL DEFAULT NEXTVAL('roads_gid_seq'::regclass) rd_id | bigint yutu_id | integer block_id | bigint heirarchy | character varying(5) cn_name | character varying(75) py_name | character varying(100) source | bigint target | bigint x1 | numeric y1 | numeric x2 | numeric y2 | numeric costlength | numeric the_geom | geometry
A. Extracting the coordinates of the line segments from Mapinfo.
1. I have to format the data structure as follows…
…here is the roads table after weeding out some unnecessary columns…

… adding the source,target,x1,y1,x2,y2,costlength…

2. Using ObjectGeography. Download the MapBasic Reference
ObjectGeography( object, attribute )
ObjectGeography( object, “1″ ) <– gives you the beginning x coord of the point

3. Export the tab file to a shape file for ArchMap.

Loading Mapinfo table to PostGis
Apr 5th
AFAIK, there are only two ways to load data to PostGIS:
1. Using Insert statements
2. Using Utilities.
Utilities include shp2pgsql which is found in “C:\Program Files\PostgreSQL\8.2\bin\” or “/usr/local/pgsql/bin”. To load Mapinfo table, I have used the OGR utilities from FWTools for Windows. and used gdal1.3.2 (since it contains ogr) for Unix.
OGR2OGR CheatSheet should be a good kickstart for basic understanding. For the impatient..
ogr2ogr -f "PostGreSQL" -nlt LINESTRING -a_srs "EPSG:4326" PG:"host=localhost user=username password=mypassword dbname=mydb mytab.TAB -select columnName
or
ogr2ogr -f "PostgreSQL" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.tab -nln newtablename -select columnName
Bear in mind that you should select out the columns from your mapfile (mine is chinese) especially if you have a diffent encoding in your column which matches your database (postgres). You might get a “Terminating translation prematurely after failed translation of layer [layername]” error. Since Mapinfo stores the text to ASCII, my workaround is to export the tabfile to a UTF-8 textfile then upload it to PostGres. Hoping the primary ids would match to make the necessary updates…
An alternative to load Chinese text from Mapinfo to PostGIS is the ff:
1. In Mapinfo use the Universal Translator to export the table into a shape file.
2. Once you have the shape file, you can directly use the shp2pgsql.
shp2pgsql -W “gbk” -s 4326 lbjrdnt_small_polyline roads > roads.sql

Comments