There are two ways to do this. One is to use OGR/GDAL's tigerpoly.py script. Unfortunately, this sometimes produces bad polygons. I wrote a "how to fix up bad polygons" document as well but its long and very complex (but easy to follow).
I wrote a Java script (using Geotools and JTS) that does, basically, the same thing as the tigerpoly.py script, but is much more robust and does significantly more error checking. This is the recommended load procedure.
It assumes you have your TIGER data in a postgis database and have Java (1.4.2+). Loading TIGER basedata
If you want to see the old (OGR/GDAL/tigerpoly.py) load proceedure see here Loading TIGER polygons tigerpoly.pl.
This process is much faster than the OGR/GDAL process. This takes about 15seconds/county, and the ORG/GDAL takes about 2 minutes/county.
| TIGER 2005fe error The tiger2005fe has 2 errors - this will fix it: update pip set wkb_geometry = 'SRID=1;POINT (-82.06129672922174 39.53997601515791)'::geometry update pip set wkb_geometry = 'SRID=1;POINT (-92.5198529528275 36.10732487177976)'::geometry update pip set wkb_geometry = 'SRID=1;POINT (-90.5673377761759 29.986304278927115)'::geometry update pip set wkb_geometry = 'SRID=1;POINT (-90.38182741292844 29.92608334553412)'::geometry update pip set wkb_geometry = 'SRID=1;POINT (-82.32978809930067 31.20110701961316)'::geometry update pip set wkb_geometry = 'SRID=1;POINT (-82.84969652715823 30.88904911060222)'::geometry update pip set wkb_geometry = 'SRID=1;POINT (-83.42376615233437 33.86629106442862)'::geometry - major data error in Guam - now make 5157 polygon not have the upper left rectangle in it. - another major data error in Guam update completechain update completechain delete from polychainlink - another major data error in Guam 2 update completechain update completechain delete from polychainlink - another major data error in Guam 3 (poly1540) update completechain update completechain delete from polychainlink |
| Finding and correcting errors The above errors were found by running the polygonization program and then investigating the errors. There are three types: |
1. Geotools required a primary key on the table
alter table pip add primary key (ogc_fid); alter table completechain add primary key (ogc_fid); alter table polychainlink add primary key (ogc_fid);
2. Create the poly2 table
This is where the polygons will be put:
create table poly2 (the_geom geometry, polyid numeric(10,0), module char(8)) with oids;
insert into geometry_columns values ('','public','poly2','the_geom',2,1,'POLYGON');
3. Download the "tigerpolygonformation.zip" file and unzip it
Available here
This contains the java program (and jars) to run the load. Make sure you have java 1.4.2+ installed.
4. Edit the go.txt script
You may have to edit the postgresql connection string. Use a search-and-replace function.
NOTE: if you are running this on windows, you'll have to replace all the ":" with ";". Use a search-and-replace function.
5. run the go.txt script
source go.txt
6. QA/QC checks
--should be 0 (check for bad polys) -- see below!! SELECT module,polyid FROM poly2 WHERE not(isvalid(the_geom)) OR (the_geom isnull); --make sure all the polygons formed --- should be 3232 selct count(distinct module) from poly2; insert into qa_qc select module,'P',count(*) from poly2 group by module; SELECT module,count FROM qa_qc where filetype = 'P' EXCEPT SELECT county,nfeats FROM counts_txt WHERE type='p'; - should return none! (if it does return something, then there was a load error) SELECT county,nfeats FROM counts_txt WHERE type='p' except SELECT module,count FROM qa_qc where filetype = 'P' - should return none! (if it does return something, then there was a load error)
| isvalid() problem When you execute: You may get these three polygons: module | polyid These are not actually invalid() - its a bug in the GEOS/JTS isvalid() code. Its been reported and a fix will hopefully make it to PostGIS in the next few releases. |
this is probably because you do not have permission to either (1) write to the temp directory or (2) read the files you created.
The easiest thing to try is to login as "root" - if it works, then its a "simple" permission problem that you have to track down.
I'm trying to load tiger2005se and I'm running into a few issues. I'll post some infomation on the issues I'm running into once I have something substantial to share.
Sean, I'm hoping you're monitoring this article, as I can find no way to contact you directly. Perhaps we can collaborate on this? Email me at daniel@ceregatti.org if you're interested.
Thanks,
Daniel Ceregatti
I had a problem with the program telling me 'You must create the poly2 table in the postgis database'. Turns out that the program was putting a carriage return as part of the module name so I simply piped the go.txt file through perl to strip off the carraige returns and execute the commands.
cat go.txt | perl -n -e 's/\r//g; s/\n//g; system($_);'
Hope this helps.
Robert J Derr
Weatherflow, Inc.
The article says that it assumes you have the Tiger data loaded already. Which RT files and which layers need to be loaded?
OK, I was making the mistake of loading each RT file when i should have been loading them all at once by specifying the directory. I asked my first question because my database during this load was growing to huge capacities.
I was able to run TigerPoly.java on tiger2005se with GIJ.
Apparently GIJ 4.1.2 doesn't have support for java.util.regex.Pattern. So i just hardcoded my params. I included schema since my work is in the tiger schema.
Apparently JAI adepends on nonfree software. At least i couldn't find com.sun.media.jai.codec.SeekableStream. I downloaded the source for GeoTools 2.2.0. I editted a class named Hints to remove it's static dependency on JAI. Just commented out the one line.
GeoTools depens on Maven which doesn't work with GIJ, but you can just compile Hints.java manually. Three class files are produced. Hints, Hints$Key, and Utilities. Put them in gt2-referencing-2.2.0.jar.
I was getting this exception:
IllegalArgumentException: "FeatureReader allready produces contents with the correct schema"
from JDBC1DataStore.getFeatureReader( query, trans ) calling ReTypeFeatureReader(). That was occurring because (requestedNames.length < propertyNames.length). If you add "module" to requestedNames (called ps) for completechain, polychainlink,and pip, then propertyNames is set to requestedNames earlier and you avoid the retyping.
A few notes on running this process with tiger2006se. (Yeah, that's 2006 -- last TIGER evah!)
I encountered a few data problems, but not all those above.
Notably, I got a "poly has 0 pips!" error. To resolve it, I modified TigerPoly.java as follows:
line 578:
throw new Exception( "poly " + mainPolyID + " has 0 pips! area="+p.getArea() +
" (Try moving poly id " + mainPolyID + " pip to " + p.getInteriorPoint() + ")");
Recompile TigerPoly.java and run the offending modules again. It reported these polys to be in error:
TGR16039: 4882
TGR53031: 6599
The following SQL should fix those modules.
update pip set wkb_geometry = 'SRID=1;POINT (-115.70541555162382 43.1323975)'::geometry where module = 'TGR16039' and polyid = 4882; update pip set wkb_geometry = 'SRID=1;POINT (-123.6454775985327 47.5815755)'::geometry where module = 'TGR53031' and polyid = 6599;
Run those modules again and it should succeed.
Finally, I had problems with Guam which were solved according to the above procedure.
Problems here with Postgis 1.2.3 on Postgres 8.2.4: You must create the poly2 table in the postgis database:
create table poly2 (the_geom geometry, polyid numeric(10,0), module char(8)) with oids;
insert into geometry_columns values ('','public','poly2','the_geom',2,1,'POLYGON');
But I do have a poly2 table... Table "public.poly2"
Column | Type | Modifiers
--------------------------------
the_geom | geometry |
polyid | numeric(10,0) |
module | character(8) |
I'm thinking the version of GeoTools included in tigerpolygoninformation.zip doesn't play nice with this Postgres/PostGIS version, but unfortunately the newer GeoTools versions have significant API changes.
Any ideas?
Here's the stack trace for the exception leading to the "You must create the poly2 table" message:
org.geotools.data.SchemaNotFoundException: Feature type could not be found for poly2
at org.geotools.data.memory.MemoryDataStore.getSchema(MemoryDataStore.java:271)
at org.geotools.data.memory.MemoryDataStore$1.<init>(MemoryDataStore.java:320)
at org.geotools.data.memory.MemoryDataStore.getFeatureReader(MemoryDataStore.java:319)
at tigerpoly.TigerPoly.write(TigerPoly.java:424)
at tigerpoly.TigerPoly.main(TigerPoly.java:381)
Hi,
Thanks for your great work! Your instructions are helpful. Unfortunately I can not run java program you have written. I am not java developer and can not solve it myself. Below if the call stack. I have tried it with java 1.4.2 and 1.5.0.
Alexey
start module = TGR06001
loading completechains...
Exception in thread "main" sun.misc.ServiceConfigurationError: org.opengis.referencing.crs.CRSFactory: : java.util.zip.ZipException: Access is denied
at sun.misc.Service.fail(Service.java:129)
at sun.misc.Service.parse(Service.java:209)
at sun.misc.Service.access$100(Service.java:111)
at sun.misc.Service$LazyIterator.hasNext(Service.java:258)
at org.geotools.factory.FactoryRegistry.scanForPlugins(FactoryRegistry.java:419)
at org.geotools.factory.FactoryRegistry.getServiceProviders(FactoryRegistry.java:114)
at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:221)
at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:193)
at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:90)
at org.geotools.referencing.FactoryFinder.getCRSFactory(FactoryFinder.java:212)
at org.geotools.data.jdbc.referencing.JDBCAuthorityFactory.<init>(JDBCAuthorityFactory.java:60)
at org.geotools.data.postgis.referencing.PostgisAuthorityFactory.<init>(PostgisAuthorityFactory.java:43)
at org.geotools.data.postgis.PostgisDataStore.getPostgisAuthorityFactory(PostgisDataStore.java:965)
at org.geotools.data.postgis.PostgisDataStore.getGeometryAttribute(PostgisDataStore.java:947)
at org.geotools.data.postgis.PostgisDataStore.buildAttributeType(PostgisDataStore.java:878)
at org.geotools.data.jdbc.JDBC1DataStore.buildSchema(JDBC1DataStore.java:1150)
at org.geotools.data.postgis.PostgisDataStore.buildSchema(PostgisDataStore.java:593)
at org.geotools.data.jdbc.FeatureTypeHandler.getFeatureTypeInfo(FeatureTypeHandler.java:200)
at org.geotools.data.jdbc.FeatureTypeHandler.getSchema(FeatureTypeHandler.java:168)
at org.geotools.data.jdbc.JDBC1DataStore.getSchema(JDBC1DataStore.java:388)
at org.geotools.data.jdbc.JDBC1DataStore.getFeatureReader(JDBC1DataStore.java:626)
at tigerpoly.TigerPoly.getCC(TigerPoly.java:116)
at tigerpoly.TigerPoly.main(TigerPoly.java:349)