Skip to topic | Skip to bottom
Home
Main
Main.OGR_exampler1.7 - 02 Nov 2007 - 18:54 - JeremyCothrantopic end

Start of topic | Skip to actions
Here's an example of using command line(batch mode) type conversions from CSV to shapefile using the OGR 'ogr2ogr' command and a further conversion to a map image using 'shp2img'.

Converting from CSV to shapefile

Consider the following file sample1.csv located in /my_dir (all of the files listed below are assumed to be in the same /my_dir directory and commands run from /my_dir )

sample1.csv

long,lat,value
-81,32,13
-81,32,14
-81,32,15

To convert this file to a shapefile, download the OGR tools included with FWTools http://fwtools.maptools.org , the main command line utility of interest is ogr2ogr.

Using ogr2ogr to go from a csv to a shapefile is a two step process.

The first step creates the .dbf file sample1.dbf from the source sample1.csv file

ogr2ogr -f "ESRI Shapefile" /my_dir sample1.csv

The sample1.dbf file is used in conjunction with a descriptive sample1.vrt file in the second step to create the shapefile mylayer1.

ogr2ogr -f "ESRI Shapefile" /my_dir sample1.vrt

sample1.vrt file

<OGRVRTDataSource>
  <OGRVRTLayer name="mylayer1">
    <SrcDataSource relativeToVRT="1">/my_dir</SrcDataSource>
    <SrcLayer>sample1</SrcLayer>
    <GeometryType>wkbPoint</GeometryType>
    <LayerSRS>WGS84</LayerSRS>
    <GeometryField encoding="PointFromColumns" x="long" y="lat"/>
  </OGRVRTLayer>
</OGRVRTDataSource>

Note in the above .vrt file

  • the name for the output shapefile 'mylayer1'
  • the source directory argument
  • the source filename argument which does not include a suffix
  • the x and y column mappings

.vrt also additionally supports a sql query filter argument which can be used when converting data - .vrt file options better explained at http://www.gdal.org/ogr/drv_vrt.html

The mylayer1 shapefile created is actually the 4 following files: mylayer1.dbf, mylayer1.shp, mylayer1.shx, mylayer1.prj

The files can be zipped into a single mylayer1.zip file for catalog purposes.

Visualizing shapefiles

For users desiring to visualize and analyze shapefile data at the desktop, in addition to the usual ESRI GIS tools, there is also the freely available MapWindows? http://www.mapwindow.com or a number of other freely available desktop GIS tools such as

For developers trying to assemble pregenerated map images and animations for their websites, the command line shp2img tool included with the FWTools install can be used in conjunction with the freely available open source MapServer? toolkits for Linux (http://www.maptools.org/fgs/) and Windows (http://www.maptools.org/ms4w/index.phtml).

Familiarity with the 'mapfile' query and visualization options see http://mapserver.gis.umn.edu/docs/reference/mapfile ) can be used to support pregenerated map image generation via shp2img or a WMS(Web Mapping Service) call against the mapfile data layer. Dynamic query and visualization via further developed online MapServer? GIS based interfaces can also use this same mapfile where more dynamic online interaction might be wanted.

Using shp2img to generate images from shapefile

The below is the following command line used to produce a simple image from a sample mapfile using the earlier sample shapefile data(mylayer1).

shp2img -m my_mapfile.map > output_image.png

sample mapfile my_mapfile.map - settings which you may want to modify have 'modify' comments and some of the paths are hardcoded and should be changed. Note also that the shapefile references require the shapefile layer name prefix before column references (like mylayer1.value)

MAP
  NAME "Sample Map"
  STATUS ON
  #modify output image extents
  #Full SC state
  EXTENT -82 31 -77 34
  #modify output image size
  SIZE 500 500
  SHAPEPATH "/usr2/maps/seacoos/data"
  SYMBOLSET "/usr2/maps/ref/symbols.sym"
  FONTSET "/usr2/maps/ref/fonts.txt"
  IMAGECOLOR 210 252 241 # seacoos_key.png
  UNITS DD
  IMAGETYPE PNG
  OUTPUTFORMAT
    NAME PNG
    DRIVER "GD/PNG"
    MIMETYPE "image/png"
    IMAGEMODE PC256
    EXTENSION "png"
  END
  PROJECTION
    "init=epsg:4269"
  END
  LAYER
    NAME "sample"
    STATUS ON
    CONNECTIONTYPE OGR
    CONNECTION '/my_dir/mylayer1.shp'
    DATA "SELECT mylayer1.* FROM mylayer1"
    TEMPLATE "dummy"
    TYPE POINT
    PROJECTION
      "proj=latlong"
    END
    #modify data layer transparency
    #TRANSPARENCY 60
    METADATA 
      "wms_srs" "EPSG:4269 EPSG:4326"
      "wms_extent" "N/A"
      "wms_title" "N/A"
      "wms_abstract" "N/A" 
      "wms_keywordlist" "N/A"
    END
     CLASS
      SYMBOL 'square' SIZE 1
      COLOR 0 0 255 EXPRESSION (0 < [mylayer1.value] AND [mylayer1.value] <= 1)
    END
    CLASS
      SYMBOL 'square' SIZE 1
      COLOR 0 255 0 EXPRESSION (1 < [mylayer1.value] AND [mylayer1.value] <= 2)
    END
    CLASS
      SYMBOL 'square' SIZE 1
      COLOR 255 0 0 EXPRESSION (2 < [mylayer1.value] AND [mylayer1.value] <= 3)
    END
  END
END

Note in the above that the SQL Query could be much more complex or use user/script dynamically supplied arguments to effect the query resultset.
to top


You are here: Main > OGR_example

to top

Copyright © 1999-2014 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding DMCC? Send feedback