Thinking in GIS

a blog about GIS from a urban geogeek living at the countryside

Feed, Categories, Archives


Connection MapFile layer to PostGIS (c# mapscript tutorial, part 7)

Posted: August 01, 2006
Categories: GIS, .NET, PostGIS, devs, MapServer, Tutorials
Feedback: View Comments

First I am going to show the difference between connecting the MapFile to a Shapefile layer and to a PostGIS layer, then I will show how to adapt your MapFile to work with PostGIS.

Shapefile Connection

Here is a sample connection from MapFile to a shapefile called "compfun".

First you need to declare the path to the shape data (SHAPEPATH).

Then for each shapefile layer you have to set the shapefile's data source (DATA) that is the name of the shapefile. Here the shapefile is named compfun.shp so DATA is set to "compfun".

Note that NAME can be anything, it means how in your MapFile want to call the shapefile layer.

MAP
...
SHAPEPATH "C:\training\mapServerTutorial\data"
...
LAYER
  NAME "compfun"
  TYPE POLYGON
  STATUS ON
  DATA "compfun"
  CLASS
    STYLE
      COLOR 255 235 190
      OUTLINECOLOR 0 0 0
      SYMBOL 0
    END
  END
END
...

PostGIS Connection

All the connection properties can be defined in the LAYER section of MapFile.

Obviously there is no need to set a SHAPEPATH attribute, unless you want to serve some shapefile layer togheter with PostGIS layers.

Basically in the LAYER section you need to set 3 attributes.

  • CONNECTIONTYPE has to be set to "PostGIS"
  • CONNECTION has to be set to the connection string needed for PostGIS
  • DATA has to be set with a SQL select to the geometry column from the layer table

The connection string needs some attribute to be set:

  • HOST hostname of the PostgreSQL server
  • DBNAME name of the database where your PostGIS data are stored
  • USER name of the user
  • PASSWORD
  • PORT you can omit this attribute if you installed PostgreSQL at the default port (5432)

Also for PostGIS layers, as for shapefile layers, NAME can be anything. It is how you want to call the layer in the MapServer context.

LAYER
  CONNECTIONTYPE postgis
  CONNECTION "host=localhost dbname=TUTORIAL user=psqluser password=psqluser port=5432"
  DATA "the_geom FROM compfun"
  NAME "compfun"
  TYPE POLYGON
    STATUS ON
    CLASS
    STYLE
      COLOR 255 235 190
      OUTLINECOLOR 0 0 0
      SYMBOL 0
    END
  END
END

Modify the MapFile tutorial's file

You can use the csharptutorial_postgis.map MapFile included with the tutorial data.

Or if you prefer you can copy the following text in a new text file named csharptutorial_postgis.map.

MAP
NAME "Zone Samples"
SHAPEPATH "C:\training\mapServerTutorial\data"
SIZE 400 400
STATUS ON
EXTENT 1143759 4417539 1146436 4420390
UNITS METERS 
FONTSET "fonts\fonts.list"  
WEB
  IMAGEPATH "C:\Inetpub\wwwroot\temp"
  IMAGEURL "C:\Inetpub\wwwroot\temp"
END

SYMBOL
  NAME "circle"
  TYPE ellipse
  FILLED true
  POINTS
    1 1
  END
END

LAYER
        CONNECTIONTYPE postgis
        CONNECTION "host=localhost dbname=TUTORIAL user=psqluser password=psqluser port=5432"
        DATA "the_geom FROM compfun"
    NAME "compfun"
    TYPE POLYGON
    STATUS ON
    CLASS
      STYLE
        COLOR 255 235 190
        OUTLINECOLOR 0 0 0
        SYMBOL 0
      END
    END
END

LAYER
    NAME "zone"
    CONNECTIONTYPE postgis
        CONNECTION "host=localhost dbname=TUTORIAL user=psqluser password=psqluser port=5432"
        DATA "the_geom FROM zone"
    TYPE POLYGON
    STATUS ON
    CLASSITEM "COD"
    LABELITEM "NUMLOTT"
    CLASS
                EXPRESSION ([COD]=103)
        STYLE
            COLOR 230 50 0
            OUTLINECOLOR 0 0 0
            SYMBOL 0
        END
    END
    CLASS
                EXPRESSION ([COD]=105 OR [COD]=106 OR [COD]=107 OR [COD]=108 OR [COD]=102)
        STYLE
            COLOR 255 235 230
            OUTLINECOLOR 0 0 0
            SYMBOL 0
        END
        LABEL
                    COLOR 0 0 0
                    FONT verdana
                    TYPE TRUETYPE
                    SIZE 7
                    POSITION CC
                END
    END
    CLASS
                EXPRESSION ([COD]=109 OR [COD]=101)
        STYLE
            COLOR 255 196 171
            OUTLINECOLOR 0 0 0
            SYMBOL 0
        END
    END
    CLASS
        STYLE
            COLOR 225 225 225
            OUTLINECOLOR 0 0 0
            SYMBOL 0
        END
    END
END

LAYER
        CONNECTIONTYPE postgis
        CONNECTION "host=localhost dbname=TUTORIAL user=psqluser password=psqluser port=5432"
        DATA "the_geom FROM vestizioni"
    NAME "vestizioni"
    TYPE LINE
    STATUS ON
    CLASS
        STYLE
            COLOR 0 0 0
            SYMBOL 0
        END
    END
END

LAYER
        CONNECTIONTYPE postgis
        CONNECTION "host=localhost dbname=TUTORIAL user=psqluser password=psqluser port=5432"
        DATA "the_geom FROM poi"
    NAME "POI"
    TYPE POINT
    STATUS ON
    LABELITEM "POI_TIME"
    CLASS
                SIZE 10
        STYLE
            COLOR 255 0 0
            OUTLINECOLOR 0 0 0
            SYMBOL "circle"
                END
                TEXT ([POI_USER], [POI_TIME])
                LABEL
                    COLOR 255 0 0
                    FONT verdana
                    TYPE TRUETYPE
                    SIZE 7
                    POSITION LC
                    WRAP " "
                END
        END
END

END

In the next step you will adapt the c# to work indifferently with shapefile or PostGIS data.

blog comments powered by Disqus