[GRASSLIST:704] Re: tiger2k questions (listproc is driving me nuts!)

Frank Warmerdam warmerdam at pobox.com
Fri Jul 11 10:53:32 EDT 2003


mdeneen at japple.org wrote:
> Ok, I ran v.in.ogr.
> 
> I used the directory containing the tiger data files as the OGR datasource 
> name.
> 
> The output vector name was a directory.  It tried to create database
> tables. I don't have a postgress schema created yet, and it failed trying
> to create a table with a / in it.
> 
> So, I just made the output vector a file name.  The process is running
> now, but I am getting a lot of output saying:
> 
> WARNING: Feature without geometry
> 
> I think I'm doing something wrong!  :-D
> 
> What does this mean?  I am extracting the Polygon layer.

M,

OGR's data model for Tiger is closely tied to the underlying tiger data model.
One downside of that is that the polygon layer consists of features with
the polygons attributes but no geometries.  The geometries have to be
built up from chains from the CompleteChain layer associated to the polygons
using the contents of the PolyChainLink layer.

It might be an interesting test of GRASS 5.1 to see if it is possible to import
the raw attribute layers Polygon, and PolyChainLink and the CompleteChain
line geometry layer, and use the information to build up the polygon
geometries.

However, practically speaking, I imagine it is better to do all this as a
pre-processing step before bring it into GRASS.  I have attached a Python
script that be used with GDAL/OGR to build a shapefile with the polygon
geometries.  If you have a full GDAL/OGR build installed you should be
able to use it to build the polygons.  If you have further questions about
getting the python script working it should likely be taken off this list
since it isn't really a GRASS issue.

Ideally the OGR TIGER/Line driver should do this for you (at least optionally)
but I have not implemented that.

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

-------------- next part --------------
#!/usr/bin/env python
###############################################################################
# $Id: tigerpoly.py,v 1.3 2003/07/11 14:52:13 warmerda Exp $
#
# Project:  OGR Python samples
# Purpose:  Assemble TIGER Polygons.
# Author:   Frank Warmerdam, warmerdam at pobox.com
#
###############################################################################
# Copyright (c) 2003, Frank Warmerdam <warmerdam at pobox.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
# 
#  $Log: tigerpoly.py,v $
#  Revision 1.3  2003/07/11 14:52:13  warmerda
#  Added logic to replicate all source polygon fields onto output file.
#
#  Revision 1.2  2003/07/11 14:31:17  warmerda
#  Use provided input filename.
#
#  Revision 1.1  2003/03/03 05:17:06  warmerda
#  New
#
#

import osr
import ogr
import string
import sys

#############################################################################
class Module:

    def __init__( self ):
        self.lines = {}
        self.poly_line_links = {}

#############################################################################
def Usage():
    print 'Usage: tigerpoly.py infile [outfile].shp'
    print
    sys.exit(1)

#############################################################################
# Argument processing.

infile = None
outfile = None

i = 1
while i < len(sys.argv):
    arg = sys.argv[i]

    if infile is None:
	infile = arg

    elif outfile is None:
	outfile = arg

    else:
	Usage()

    i = i + 1

if outfile is None:
    outfile = 'poly.shp'

if infile is None:
    Usage()

#############################################################################
# Open the datasource to operate on.

ds = ogr.Open( infile, update = 0 )

poly_layer = ds.GetLayerByName( 'Polygon' )

#############################################################################
#	Create output file for the composed polygons.

shp_driver = ogr.GetDriverByName( 'ESRI Shapefile' )
shp_driver.DeleteDataSource( outfile )

shp_ds = shp_driver.CreateDataSource( outfile )

shp_layer = shp_ds.CreateLayer( 'out', geom_type = ogr.wkbPolygon )

src_defn = poly_layer.GetLayerDefn()
poly_field_count = src_defn.GetFieldCount()

for fld_index in range(poly_field_count):
    src_fd = src_defn.GetFieldDefn( fld_index )
    
    fd = ogr.FieldDefn( src_fd.GetName(), src_fd.GetType() )
    fd.SetWidth( src_fd.GetWidth() )
    fd.SetPrecision( src_fd.GetPrecision() )
    shp_layer.CreateField( fd )

#############################################################################
# Read all features in the line layer, holding just the geometry in a hash
# for fast lookup by TLID.

line_layer = ds.GetLayerByName( 'CompleteChain' )
line_count = 0

modules_hash = {}

feat = line_layer.GetNextFeature()
geom_id_field = feat.GetFieldIndex( 'TLID' )
tile_ref_field = feat.GetFieldIndex( 'MODULE' )
while feat is not None:
    geom_id = feat.GetField( geom_id_field )
    tile_ref = feat.GetField( tile_ref_field )

    try:
        module = modules_hash[tile_ref]
    except:
        module = Module()
        modules_hash[tile_ref] = module

    module.lines[geom_id] = feat.GetGeometryRef().Clone()
    line_count = line_count + 1

    feat.Destroy()

    feat = line_layer.GetNextFeature()

print 'Got %d lines in %d modules.' % (line_count,len(modules_hash))

#############################################################################
# Read all polygon/chain links and build a hash keyed by POLY_ID listing
# the chains (by TLID) attached to it. 

link_layer = ds.GetLayerByName( 'PolyChainLink' )

feat = link_layer.GetNextFeature()
geom_id_field = feat.GetFieldIndex( 'TLID' )
tile_ref_field = feat.GetFieldIndex( 'MODULE' )
lpoly_field = feat.GetFieldIndex( 'POLYIDL' )
rpoly_field = feat.GetFieldIndex( 'POLYIDR' )

link_count = 0

while feat is not None:
    module = modules_hash[feat.GetField( tile_ref_field )]

    tlid = feat.GetField( geom_id_field )

    lpoly_id = feat.GetField( lpoly_field )
    rpoly_id = feat.GetField( rpoly_field )

    try:
        module.poly_line_links[lpoly_id].append( tlid )
    except:
        module.poly_line_links[lpoly_id] = [ tlid ]

    try:
        module.poly_line_links[rpoly_id].append( tlid )
    except:
        module.poly_line_links[rpoly_id] = [ tlid ]

    link_count = link_count + 1

    feat.Destroy()

    feat = link_layer.GetNextFeature()

print 'Processed %d links.' % link_count

#############################################################################
# Process all polygon features.

feat = poly_layer.GetNextFeature()
tile_ref_field = feat.GetFieldIndex( 'MODULE' )
polyid_field = feat.GetFieldIndex( 'POLYID' )

poly_count = 0

while feat is not None:
    module = modules_hash[feat.GetField( tile_ref_field )]
    polyid = feat.GetField( polyid_field )

    tlid_list = module.poly_line_links[polyid]

    link_coll = ogr.Geometry( type = ogr.wkbGeometryCollection )
    for tlid in tlid_list:
        geom = module.lines[tlid]
        link_coll.AddGeometry( geom )

    try:
        poly = ogr.BuildPolygonFromEdges( link_coll )

        #print poly.ExportToWkt()
        #feat.SetGeometryDirectly( poly )

        feat2 = ogr.Feature(feature_def=shp_layer.GetLayerDefn())

        for fld_index in range(poly_field_count):
            feat2.SetField( fld_index, feat.GetField( fld_index ) )

        feat2.SetGeometryDirectly( poly )

        shp_layer.CreateFeature( feat2 )
        feat2.Destroy()

        poly_count = poly_count + 1
    except:
        print 'BuildPolygonFromEdges failed.'

    feat.Destroy()

    feat = poly_layer.GetNextFeature()

print 'Built %d polygons.' % poly_count

#############################################################################
# Cleanup

shp_ds.Destroy()
ds.Destroy()


More information about the grass-user mailing list