]> git.openstreetmap.org Git - nominatim.git/blob - utils/imports.php
tiger import code
[nominatim.git] / utils / imports.php
1 #!/usr/bin/php -Cq
2 <?php
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-cmd.php');
5         ini_set('memory_limit', '800M');
6
7         $aCMDOptions = array(
8                 "Create and setup nominatim search system",
9                 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
10                 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
11                 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
12
13                 array('parse-tiger', '', 0, 1, 1, 1, 'realpath', 'Convert tigger edge files to nominatim sql import'),
14         );
15         getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
16
17         $bDidSomething = false;
18
19         if (isset($aCMDResult['parse-tiger']))
20         {
21                 foreach(glob($aCMDResult['parse-tiger'].'/??_*', GLOB_ONLYDIR) as $sStateFolder)
22                 {
23                         preg_match('#([0-9]{2})_(.*)#',basename($sStateFolder), $aMatch);
24                         var_dump($aMatch);
25                         exit;
26                         foreach(glob($sStateFolder.'/?????_*', GLOB_ONLYDIR) as $sCountyFolder)
27                         {
28                                 set_time_limit(30);
29                                 preg_match('#([0-9]{5})_(.*)#',basename($sCountyFolder), $aMatch);
30                                 $sCountyID = $aMatch[1];
31                                 $sCountyName = str_replace('_', ' ', $aMatch[2]);
32                                 $sImportFile = $sCountyFolder.'/tl_2009_'.$sCountyID.'_edges.zip';
33                                 $sCountyName = str_replace("'", "''", $sCountyName);
34                                 $sCountyName = str_replace(" County", "", $sCountyName);
35                                 echo "'$sCountyID' : '$sCountyName' ,\n";
36                         }
37                 }
38 exit;
39
40                 if (!file_exists(CONST_BasePath.'/data/tiger2009')) mkdir(CONST_BasePath.'/data/tiger2009');
41
42                 $sTempDir = tempnam('/tmp', 'tiger');
43                 unlink($sTempDir);
44                 mkdir($sTempDir);
45
46                 foreach(glob($aCMDResult['parse-tiger'].'/??_*', GLOB_ONLYDIR) as $sStateFolder)
47                 {
48                         foreach(glob($sStateFolder.'/?????_*', GLOB_ONLYDIR) as $sCountyFolder)
49                         {
50                                 set_time_limit(30);
51                                 preg_match('#([0-9]{5})_(.*)#',basename($sCountyFolder), $aMatch);
52                                 $sCountyID = $aMatch[1];
53                                 $sCountyName = str_replace('_', ' ', $aMatch[2]);
54                                 $sImportFile = $sCountyFolder.'/tl_2009_'.$sCountyID.'_edges.zip';
55                                 echo "$sCountyID, $sCountyName\n";
56                                 if (!file_exists($sImportFile))
57                                 {
58                                         echo "Missing: $sImportFile\n";
59                                 }
60                                 $sUnzipCmd = "unzip -d $sTempDir $sImportFile";
61                                 exec($sUnzipCmd);
62                                 if (!file_exists($sTempDir.'/tl_2009_'.$sCountyID.'_edges.shp'))
63                                 {
64                                         echo "Failed unzip ($sCountyID)\n";
65                                 }
66                                 else
67                                 {
68                                         $sParseCmd = CONST_BasePath.'/utils/tigerAddressImport.py '.$sTempDir.'/tl_2009_'.$sCountyID.'_edges.shp';
69                                         exec($sParseCmd);
70                                         if (!file_exists($sTempDir.'/tl_2009_'.$sCountyID.'_edges.osm1.osm'))
71                                         {
72                                                 echo "Failed parse ($sCountyID)\n";
73                                         }
74                                         else
75                                         {
76                                                 copy($sTempDir.'/tl_2009_'.$sCountyID.'_edges.osm1.osm', CONST_BasePath.'/data/tiger2009/'.$sCountyID.'.sql');
77                                         }
78                                 }
79                                 // Cleanup
80                                 foreach(glob($sTempDir.'/*') as $sTmpFile)
81                                 {
82                                         unlink($sTmpFile);
83                                 }
84                         }
85                 }
86         }