]> git.openstreetmap.org Git - nominatim.git/blob - utils/setup.php
data partitioning
[nominatim.git] / utils / setup.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('create-db', '', 0, 1, 0, 0, 'bool', 'Build a blank nominatim db'),
14                 array('load-data', '', 0, 1, 1, 1, 'realpath', 'Import a osm file'),
15                 array('create-partitions', '', 0, 1, 0, 0, 'bool', 'Create required partition tables and triggers'),
16         );
17         getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
18
19         $bDidSomething = false;
20
21         if ($aCMDResult['create-db'])
22         {
23                 $bDidSomething = true;
24                 // TODO: path detection, detection memory, etc.
25 //              passthru('createdb nominatim');
26                 passthru('createlang plpgsql nominatim');
27                 passthru('psql -f '.CONST_Path_Postgresql_Contrib.'/_int.sql nominatim');
28                 passthru('psql -f '.CONST_Path_Postgresql_Contrib.'/hstore.sql nominatim');
29                 passthru('psql -f '.CONST_Path_Postgresql_Postgis.'/postgis.sql nominatim');
30                 passthru('psql -f '.CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql nominatim');
31                 passthru('psql -f '.CONST_BasePath.'/data/country_name.sql nominatim');
32                 passthru('psql -f '.CONST_BasePath.'/data/country_osm_grid.sql nominatim');
33                 passthru('psql -f '.CONST_BasePath.'/data/gb_postcode.sql nominatim');
34                 passthru('psql -f '.CONST_BasePath.'/data/us_statecounty.sql nominatim');
35                 passthru('psql -f '.CONST_BasePath.'/data/us_state.sql nominatim');
36                 passthru('psql -f '.CONST_BasePath.'/data/worldboundaries.sql nominatim');
37         }
38
39         if (isset($aCMDResult['load-data']) && $aCMDResult['load-data'])
40         {
41                 $bDidSomething = true;
42                 passthru(CONST_BasePath.'/osm2pgsql/osm2pgsql -lsc -O gazetteer -C 10000 --hstore -d nominatim '.$aCMDResult['load-data']);
43                 passthru('psql -f '.CONST_BasePath.'/sql/functions.sql nominatim');
44                 passthru('psql -f '.CONST_BasePath.'/sql/tables.sql nominatim');
45         }
46
47         if ($aCMDResult['create-partitions'])
48         {
49                 $bDidSomething = true;
50                 $sSQL = 'select distinct country_code from country_name order by country_code';
51                 $aPartitions = $oDB->getCol($sSQL);
52                 if (PEAR::isError($aPartitions))
53                 {
54                         fail($aPartitions->getMessage());
55                 }
56                 $aPartitions[] = 'none';
57
58                 $sTemplate = file_get_contents(CONST_BasePath.'/sql/partitions.src.sql');
59                 preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
60                 foreach($aMatches as $aMatch)
61                 {
62                         $sResult = '';
63                         foreach($aPartitions as $sPartitionName)
64                         {
65                                 $sResult .= str_replace('-partition-', $sPartitionName, $aMatch[1]);
66                         }
67                         $sTemplate = str_replace($aMatch[0], $sResult, $sTemplate);
68                 }
69                 echo $sTemplate;
70                 exit;
71         }
72
73         if (!$bDidSomething)
74         {
75                 showUsage($aCMDOptions, true);
76         }