Friday, August 21, 2009

Disintegration Of Polygons Based On Vertex Count.

GIS folks may have bumped into the problem of cutting a polygon into pieces ,with each piece having no more than 'N' vertices.After googling I came across a PostGIS procedure to do this.

Markus Schaber's plpgsql procedure of 'Cutting a Polygon into Pieces with maximum Vertex number' worked fine even for extremely complex polygons.I experimented with complex polygon with over 800 vertices and could cut into pieces with no more than 50 vertices each.

Steps:-
------
1)Compile the PL/pgsql procedure of Markus Schaber. in the PostGres.

2)If successful,you would find _splitpolymaxvertex(geometry, integer) function in the functions list of your PostGIS database.

3)Use the following command to Split a Polygon into pieces based on suitable vertex count .
CREATE TABLE PolygonsAfterSplit WITH(OIDS=TRUE) AS ( SELECT splitpolymaxvertex(the_geom,50) FROM PolygonsBeforeSplit);

(Use Tables appropriate table names.)

4)Use QuantumGIS or JUMP to render the polygons.Thesetools have the ability to connect to PostGIS .

Sample Output:-
Polygon Before Splitting
Polygon After Splitting

Friday, July 10, 2009

Reprojection in PostGIS

In order to convert the Spatail data from one projection system to the other,PostGIS
provides ST_Transform(the_geom,targetSRID) function.While the first parameter holds the input geometry,the second parameter refers to the SRID number of the target projection system.The following example converts WGS84(SRID#4326) into UTM,Zone43N(SRID#32643).

Example:-

--DROP TABLE MyTable1_UTM;
CREATE TABLE MyTable1_UTM WITH (OIDS=TRUE) AS
(
SELECT ST_Transform(the_geom,32643) as the_geom FROM MyTable1_geom
)


You can find the SRID of all PostGIS supported projection systems in spatial_ref_sys
table of every spatially enabled table.

Converting NonSpatial to Spatial table in PostGres/PostGIS

I would assume that the user is familiar with PostGIS,the spatial extension to PostGres.
In this post ,we would create a Sptial Table by using geographic elements from a NonSpatial table.We would continue with the MyTable1 created in the previous post.

1) Install PostGIS extension

2) Create a geometry enabled table specifying the Spatial Reference System(SRID).In this example,POINT type geometry with WGS84 coordinate system is created.

--DROP TABLE MyTable1_geom;

CREATE TABLE MyTable1_geom
(
the_geom geometry,
CONSTRAINT "enforce_dims_the_geom" CHECK (ndims(the_geom) = 3),
CONSTRAINT "enforce_geotype_the_geom" CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL),
CONSTRAINT "enforce_srid_the_geom" CHECK (srid(the_geom) = 4326)

)
WITH (OIDS=TRUE);
ALTER TABLE MyTable1_geom OWNER TO postgres;

3) Copy data from Non-Spatial table-MyTable1

INSERT INTO MyTable1_geom(the_geom) SELECT ST_GeomFromEWKT('SRID=4326;POINT('|| drlong || ' ' || drlat || ' ' ||dralt || ')') as the_geom FROM MyTable1;

4)Check if updated in spatial table

SELECT ST_AsEWKT(the_geom) FROM MyTable1_geom;

Importing CSV file to PostGres

1) Ensure that the CSV file is in the following format with first row containing column names.

Eg:- MyData.csv

Long,Lat,Alt
77.554428,12.912127,837
77.554428,12.91212,836
77.55442,12.912113,836
77.55442,12.912107,837
77.554413,12.912102,837
77.554405,12.912099,836
77.554405,12.912093,837
77.554398,12.912087,837

2) Create a Table with desired fields in PostGres

Eg:-

CREATE TABLE MyTable1
(
drlat double precision,
drlong double precision,
dralt double precision
)
WITH (OIDS=TRUE);
ALTER TABLE MyTable1 OWNER TO postgres

3) Use the following command to import the data from CSV file into PostGres table

COPY MyTable1 FROM E'C:\\MyData.csv' USING DELIMITERS ',' CSV HEADER;

Saturday, April 18, 2009

Rendering ESRI shapefile

Follow these steps to parse ESRI shapefile and render it using any programming languages like C++,C# or Java.The procedure discussed assumes working knowledge of GIS and Qt/Visual Studio 6.Knowledge on Graphics programming using GDI/GDI+ or OpenGL is an advantage.There are plenty of opensource tools like JUMP,SharpMap FWTools etc to render various GIS Raster and Vector file formats.This article is to help beginner level GIS programmers who like to create their own stuff.Let's begin with the following steps.

1) Grab shepelib by Frank Warmerdam and build the library from source .You can also make use of pre-built binaries.

2) Use your shapefiles in your possession or those available at shepelib

3)I used OpenGL and Visual Studio 6 for rendering.The GLUT library is used for rendering context.

4)Include the shapelib.h in your C++ program for parsing shapefiles.Link to shapelib.dll and shapelib.lib

-Declare variables to store points,lines or polygons

-Query the bounding box of every shapefile that is being opened.Readjust the rendering area to accommodate the bounding box with largest area.

-Render the point,line polygon shapefiles using drawing primitives like GL_POINTS,GL_LINES,GL_LINELOOP.

That is all to to have a decent beginning.The same logic holds good for other GIS vector formats like MIF,DGN etc, provided the relevant libraries to parse them are used.

You can have a peek at my Article for the detailed version of the same topic.It includes sample VS6 workspace to experiment on.

Creating Custom Line Styles in GIS Applications

Have a look at an article on creating custom lines styles . http://www.codeproject.com/KB/GDI/AestheticLines.aspx.