-- Name: word_frequencies; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
+DROP TABLE IF EXISTS word_frequencies;
+
CREATE TABLE word_frequencies (
word_token text,
count bigint
);
-
--
-- Data for Name: word_frequencies; Type: TABLE DATA; Schema: public; Owner: -
--
}
return $aAddress;
}
+
+ public function debugInfo()
+ {
+ return $this->aAddressLines;
+ }
}
$sPre = ', ';
}
}
+ } elseif (is_object($mVar) && method_exists($mVar, 'debugInfo')) {
+ Debug::outputVar($mVar->debugInfo(), $sPreNL);
} else {
Debug::outputSimpleVar($mVar);
}
$sSQL .= " 'P' as osm_type,";
$sSQL .= ' (SELECT osm_id from placex p WHERE p.place_id = lp.parent_place_id) as osm_id,';
$sSQL .= " 'place' as class, 'postcode' as type,";
- $sSQL .= ' null as admin_level, rank_search, rank_address,';
+ $sSQL .= ' null::smallint as admin_level, rank_search, rank_address,';
$sSQL .= ' place_id, parent_place_id,';
- $sSQL .= ' null as housenumber,';
+ $sSQL .= ' -1 as housenumber,';
$sSQL .= ' country_code,';
$sSQL .= $this->langAddressSql('-1');
$sSQL .= ' postcode as placename,';
$sSQL .= ' postcode as ref,';
- if ($this->bExtraTags) $sSQL .= 'null AS extra,';
- if ($this->bNameDetails) $sSQL .= 'null AS names,';
+ if ($this->bExtraTags) $sSQL .= 'null::text AS extra,';
+ if ($this->bNameDetails) $sSQL .= 'null::text AS names,';
$sSQL .= ' ST_x(geometry) AS lon, ST_y(geometry) AS lat,';
$sSQL .= ' (0.75-(rank_search::float/40)) AS importance, ';
$sSQL .= $this->addressImportanceSql('geometry', 'lp.parent_place_id');
- $sSQL .= ' null AS extra_place ';
+ $sSQL .= ' null::text AS extra_place ';
$sSQL .= 'FROM location_postcode lp';
$sSQL .= " WHERE place_id in ($sPlaceIDs) ";
$sSQL .= " AND lp.rank_address between $iMinRank and $iMaxRank";
$sSQL .= ' (SELECT osm_id from placex p WHERE p.place_id=blub.parent_place_id) as osm_id, ';
$sSQL .= " 'place' AS class, ";
$sSQL .= " 'house' AS type, ";
- $sSQL .= ' null AS admin_level, ';
+ $sSQL .= ' null::smallint AS admin_level, ';
$sSQL .= ' 30 AS rank_search, ';
$sSQL .= ' 30 AS rank_address, ';
$sSQL .= ' place_id, ';
$sSQL .= ' housenumber_for_place as housenumber,';
$sSQL .= " 'us' AS country_code, ";
$sSQL .= $this->langAddressSql('housenumber_for_place');
- $sSQL .= ' null AS placename, ';
- $sSQL .= ' null AS ref, ';
- if ($this->bExtraTags) $sSQL .= 'null AS extra,';
- if ($this->bNameDetails) $sSQL .= 'null AS names,';
+ $sSQL .= ' null::text AS placename, ';
+ $sSQL .= ' null::text AS ref, ';
+ if ($this->bExtraTags) $sSQL .= 'null::text AS extra,';
+ if ($this->bNameDetails) $sSQL .= 'null::text AS names,';
$sSQL .= ' st_x(centroid) AS lon, ';
$sSQL .= ' st_y(centroid) AS lat,';
$sSQL .= ' -1.15 AS importance, ';
$sSQL .= $this->addressImportanceSql('centroid', 'blub.parent_place_id');
- $sSQL .= ' null AS extra_place ';
+ $sSQL .= ' null::text AS extra_place ';
$sSQL .= ' FROM (';
$sSQL .= ' SELECT place_id, '; // interpolate the Tiger housenumbers here
$sSQL .= ' ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) AS centroid, ';
$sSQL .= ' osm_id, ';
$sSQL .= " 'place' AS class, ";
$sSQL .= " 'house' AS type, ";
- $sSQL .= ' 15 AS admin_level, ';
+ $sSQL .= ' null::smallint AS admin_level, ';
$sSQL .= ' 30 AS rank_search, ';
$sSQL .= ' 30 AS rank_address, ';
$sSQL .= ' place_id, ';
$sSQL .= ' housenumber_for_place as housenumber,';
$sSQL .= ' country_code, ';
$sSQL .= $this->langAddressSql('housenumber_for_place');
- $sSQL .= ' null AS placename, ';
- $sSQL .= ' null AS ref, ';
- if ($this->bExtraTags) $sSQL .= 'null AS extra, ';
- if ($this->bNameDetails) $sSQL .= 'null AS names, ';
+ $sSQL .= ' null::text AS placename, ';
+ $sSQL .= ' null::text AS ref, ';
+ if ($this->bExtraTags) $sSQL .= 'null::text AS extra, ';
+ if ($this->bNameDetails) $sSQL .= 'null::text AS names, ';
$sSQL .= ' st_x(centroid) AS lon, ';
$sSQL .= ' st_y(centroid) AS lat, ';
// slightly smaller than the importance for normal houses
$sSQL .= ' -0.1 AS importance, ';
$sSQL .= $this->addressImportanceSql('centroid', 'blub.parent_place_id');
- $sSQL .= ' null AS extra_place ';
+ $sSQL .= ' null::text AS extra_place ';
$sSQL .= ' FROM (';
$sSQL .= ' SELECT ';
$sSQL .= ' osm_id, ';
$sSQL .= ' place_id AS osm_id, ';
$sSQL .= " 'place' AS class,";
$sSQL .= " 'house' AS type, ";
- $sSQL .= ' null AS admin_level, ';
+ $sSQL .= ' null::smallint AS admin_level, ';
$sSQL .= ' 30 AS rank_search,';
$sSQL .= ' 30 AS rank_address, ';
$sSQL .= ' place_id,';
$sSQL .= ' housenumber,';
$sSQL .= " 'us' AS country_code, ";
$sSQL .= $this->langAddressSql('-1');
- $sSQL .= ' null AS placename, ';
- $sSQL .= ' null AS ref, ';
- if ($this->bExtraTags) $sSQL .= 'null AS extra, ';
- if ($this->bNameDetails) $sSQL .= 'null AS names, ';
+ $sSQL .= ' null::text AS placename, ';
+ $sSQL .= ' null::text AS ref, ';
+ if ($this->bExtraTags) $sSQL .= 'null::text AS extra, ';
+ if ($this->bNameDetails) $sSQL .= 'null::text AS names, ';
$sSQL .= ' ST_X(centroid) AS lon, ';
$sSQL .= ' ST_Y(centroid) AS lat, ';
$sSQL .= ' -1.10 AS importance, ';
'centroid',
'location_property_aux.parent_place_id'
);
- $sSQL .= ' null AS extra_place ';
+ $sSQL .= ' null::text AS extra_place ';
$sSQL .= ' FROM location_property_aux ';
$sSQL .= " WHERE place_id in ($sPlaceIDs) ";
if ($aPoly) {
$sCountryCode = $aPoly['country_code'];
- $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', geometry) as distance';
+ // look for place nodes with the given country code
+ $sSQL = 'SELECT place_id FROM';
+ $sSQL .= ' (SELECT place_id, rank_search,';
+ $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
$sSQL .= ' FROM placex';
$sSQL .= ' WHERE osm_type = \'N\'';
$sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
- $sSQL .= ' AND rank_address > 0';
- $sSQL .= ' AND rank_address <= ' .min(25, $iMaxRank);
+ $sSQL .= ' AND rank_search > 4';
+ $sSQL .= ' AND rank_search <= ' .min(25, $iMaxRank);
$sSQL .= ' AND type != \'postcode\'';
$sSQL .= ' AND name IS NOT NULL ';
$sSQL .= ' and indexed_status = 0 and linked_place_id is null';
- $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 1.0)';
- $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
+ $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, 5.0)) p ';
+ $sSQL .= 'WHERE distance <= reverse_place_diameter(rank_search)';
+ $sSQL .= ' ORDER BY rank_search DESC, distance ASC';
$sSQL .= ' LIMIT 1';
if (CONST_Debug) var_dump($sSQL);
if ($aPlacNode) {
return $aPlacNode;
}
+
+ // still nothing, then return the country object
+ $sSQL = 'SELECT place_id, ST_distance('.$sPointSQL.', centroid) as distance';
+ $sSQL .= ' FROM placex';
+ $sSQL .= ' WHERE country_code = \''.$sCountryCode.'\'';
+ $sSQL .= ' AND rank_search = 4 AND rank_address = 4';
+ $sSQL .= ' AND class in (\'boundary\', \'place\')';
+ $sSQL .= ' ORDER BY distance ASC';
+
+ if (CONST_Debug) var_dump($sSQL);
+ $aPlacNode = chksql(
+ $this->oDB->getRow($sSQL),
+ 'Could not determine place node.'
+ );
+ if ($aPlacNode) {
+ return $aPlacNode;
+ }
}
}
// polygon search begins at suburb-level
if ($iMaxRank > 25) $iMaxRank = 25;
// no polygon search over country-level
- if ($iMaxRank < 4) $iMaxRank = 4;
+ if ($iMaxRank < 5) $iMaxRank = 5;
// search for polygon
$sSQL = 'SELECT place_id, parent_place_id, rank_address, rank_search FROM';
$sSQL .= '(select place_id, parent_place_id, rank_address, rank_search, country_code, geometry';
$sSQL .= ' FROM placex';
$sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
- $sSQL .= ' AND rank_address Between 4 AND ' .$iMaxRank;
+ $sSQL .= ' AND rank_address Between 5 AND ' .$iMaxRank;
$sSQL .= ' AND geometry && '.$sPointSQL;
$sSQL .= ' AND type != \'postcode\' ';
$sSQL .= ' AND name is not null';
$iPlaceID = $aPoly['place_id'];
if ($iRankAddress != $iMaxRank) {
- //search diameter for the place node search
- if ($iMaxRank <= 4) {
- $fSearchDiam = 4;
- } elseif ($iMaxRank <= 8) {
- $fSearchDiam = 2;
- } elseif ($iMaxRank <= 10) {
- $fSearchDiam = 1;
- } elseif ($iMaxRank <= 12) {
- $fSearchDiam = 0.8;
- } elseif ($iMaxRank <= 17) {
- $fSearchDiam = 0.6;
- } elseif ($iMaxRank <= 18) {
- $fSearchDiam = 0.2;
- } elseif ($iMaxRank <= 25) {
- $fSearchDiam = 0.1;
- }
-
- $sSQL = 'SELECT place_id';
- $sSQL .= ' FROM (';
- $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
+ $sSQL = 'SELECT place_id FROM ';
+ $sSQL .= '(SELECT place_id, rank_search, country_code, geometry,';
$sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
$sSQL .= ' FROM placex';
$sSQL .= ' WHERE osm_type = \'N\'';
- if ($iRankAddress = 16) {
- // using rank_search because of a better differentiation for place nodes at rank_address 16
- $sSQL .= ' AND rank_search > '.$iRankSearch;
- $sSQL .= ' AND rank_search <= ' .$iMaxRank;
- $sSQL .= ' AND class = \'place\'';
- } else {
- $sSQL .= ' AND rank_address > '.$iRankAddress;
- $sSQL .= ' AND rank_address <= ' .$iMaxRank;
- }
- $sSQL .= ' AND ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
+ // using rank_search because of a better differentiation
+ // for place nodes at rank_address 16
+ $sSQL .= ' AND rank_search > '.$iRankSearch;
+ $sSQL .= ' AND rank_search <= ' .$iMaxRank;
+ $sSQL .= ' AND class = \'place\'';
$sSQL .= ' AND type != \'postcode\'';
$sSQL .= ' AND name IS NOT NULL ';
- $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
+ $sSQL .= ' AND indexed_status = 0 AND linked_place_id is null';
// preselection through bbox
$sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
$sSQL .= ' ORDER BY distance ASC,';
$sSQL .= ' rank_address DESC';
$sSQL .= ' limit 500) as a';
$sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
- $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
+ $sSQL .= ' AND distance <= reverse_place_diameter(rank_search)';
+ $sSQL .= ' ORDER BY distance ASC, rank_search DESC';
$sSQL .= ' LIMIT 1';
if (CONST_Debug) var_dump($sSQL);
$aDSNInfo = DB::parseDSN(CONST_Database_DSN);
if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
$sCMD = 'psql -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'];
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
+ }
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sCMD .= ' -U ' . $aDSNInfo['username'];
+ }
+ $aProcEnv = null;
+ if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
+ }
if (!$bVerbose) {
$sCMD .= ' -q';
}
2 => STDERR
);
$ahPipes = null;
- $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes);
+ $hProcess = @proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
if (!is_resource($hProcess)) {
fail('unable to start pgsql');
}
while (strlen($sScript)) {
- $written = fwrite($ahPipes[0], $sScript);
- if ($written <= 0) break;
- $sScript = substr($sScript, $written);
+ $iWritten = fwrite($ahPipes[0], $sScript);
+ if ($iWritten <= 0) break;
+ $sScript = substr($sScript, $iWritten);
}
fclose($ahPipes[0]);
$iReturn = proc_close($hProcess);
fail("pgsql returned with error code ($iReturn)");
}
}
+
+
+function runWithEnv($sCmd, $aEnv)
+{
+ $aFDs = array(
+ 0 => array('pipe', 'r'),
+ 1 => STDOUT,
+ 2 => STDERR);
+ $aPipes = null;
+ $hProc = @proc_open($sCmd, $aFDs, $aPipes, null, $aEnv);
+ if (!is_resource($hProc)) {
+ fail('unable to run command:' . $sCmd);
+ }
+
+ fclose($aPipes[0]); // no stdin
+
+ $iStat = proc_close($hProc);
+ return $iStat;
+}
void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile)
{
- struct index_thread_data * thread_data;
+ struct index_thread_data *thread_data;
PGconn *conn;
- PGresult * res;
+ PGresult *res;
+ int num_rows = 0, status_code = 0;
+ int db_has_locale = 0;
+ char *result_string = NULL;
int rank;
-
+
int i;
xmlTextWriterPtr writer;
exit(EXIT_FAILURE);
}
+ res = PQexec(conn, "SHOW lc_messages");
+ status_code = PQresultStatus(res);
+ if (status_code != PGRES_TUPLES_OK && status_code != PGRES_SINGLE_TUPLE) {
+ fprintf(stderr, "Failed determining database locale: %s\n", PQerrorMessage(conn));
+ exit(EXIT_FAILURE);
+ }
+ num_rows = PQntuples(res);
+ if (num_rows > 0)
+ {
+ result_string = PQgetvalue(res, 0, 0);
+ if (result_string && (strlen(result_string) > 0) && (strcasecmp(result_string, "C") != 0))
+ {
+ // non-default locale if the result exists, is non-empty, and is not "C"
+ db_has_locale = 1;
+ }
+ }
+
pg_prepare_params[0] = PG_OID_INT4;
res = PQprepare(conn, "index_sectors",
"select geometry_sector,count(*) from placex where rank_search = $1 and indexed_status > 0 group by geometry_sector order by geometry_sector",
}
PQclear(res);
- // Make sure the error message is not localized as we parse it later.
- res = PQexec(thread_data[i].conn, "SET lc_messages TO 'C'");
- if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ if (db_has_locale)
{
- fprintf(stderr, "Failed to set langauge: %s\n", PQerrorMessage(thread_data[i].conn));
- exit(EXIT_FAILURE);
+ // Make sure the error message is not localized as we parse it later.
+ res = PQexec(thread_data[i].conn, "SET lc_messages TO 'C'");
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ {
+ fprintf(stderr, "Failed to set langauge: %s\n", PQerrorMessage(thread_data[i].conn));
+ exit(EXIT_FAILURE);
+ }
+ PQclear(res);
}
- PQclear(res);
-
nominatim_exportCreatePreparedQueries(thread_data[i].conn);
}
-
fprintf(stderr, "Starting indexing rank (%i to %i) using %i threads\n", rank_min, rank_max, num_threads);
for (rank = rank_min; rank <= rank_max; rank++)
@define('CONST_Debug', false);
@define('CONST_Database_DSN', 'pgsql://@/nominatim'); // <driver>://<username>:<password>@<host>:<port>/<database>
@define('CONST_Database_Web_User', 'www-data');
+@define('CONST_Database_Module_Path', CONST_InstallPath.'/module');
@define('CONST_Max_Word_Frequency', '50000');
@define('CONST_Limit_Reindexing', true);
// Restrict search languages.
$$
LANGUAGE plpgsql IMMUTABLE;
+CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
+ RETURNS FLOAT
+ AS $$
+BEGIN
+ IF rank_search <= 4 THEN
+ RETURN 5.0;
+ ELSIF rank_search <= 8 THEN
+ RETURN 1.8;
+ ELSIF rank_search <= 12 THEN
+ RETURN 0.6;
+ ELSIF rank_search <= 17 THEN
+ RETURN 0.16;
+ ELSIF rank_search <= 18 THEN
+ RETURN 0.08;
+ ELSIF rank_search <= 19 THEN
+ RETURN 0.04;
+ END IF;
+
+ RETURN 0.02;
+END;
+$$
+LANGUAGE plpgsql IMMUTABLE;
CREATE OR REPLACE FUNCTION get_postcode_rank(country_code VARCHAR(2), postcode TEXT,
OUT rank_search SMALLINT, OUT rank_address SMALLINT)
END LOOP;
--DEBUG: RAISE WARNING 'address computed';
- -- for long ways we should add search terms for the entire length
- IF st_length(NEW.geometry) > 0.05 THEN
-
- location_rank_search := 0;
- location_distance := 0;
-
- FOR location IN SELECT * from getNearFeatures(NEW.partition, NEW.geometry, search_maxrank, isin_tokens) LOOP
-
- IF location.rank_address != location_rank_search THEN
- location_rank_search := location.rank_address;
- location_distance := location.distance * 1.5;
- END IF;
-
- IF location.rank_search > 4 AND location.distance < location_distance THEN
-
- -- Add it to the list of search terms
- nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
- INSERT INTO place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address)
- VALUES (NEW.place_id, location.place_id, true, false, location.distance, location.rank_address);
-
- END IF;
-
- END LOOP;
-
- END IF;
- --DEBUG: RAISE WARNING 'search terms for long ways added';
-
IF NEW.address is not null AND NEW.address ? 'postcode'
AND NEW.address->'postcode' not similar to '%(,|;)%' THEN
NEW.postcode := upper(trim(NEW.address->'postcode'));
CREATE INDEX idx_search_name_-partition-_centroid ON search_name_-partition- USING GIST (centroid) {ts:address-index};
CREATE INDEX idx_search_name_-partition-_name_vector ON search_name_-partition- USING GIN (name_vector) WITH (fastupdate = off) {ts:address-index};
+DROP TABLE IF EXISTS location_road_-partition-;
CREATE TABLE location_road_-partition- (
place_id BIGINT,
partition SMALLINT,
the test databases (db tests)
* `TEST_DB` - name of test database (db tests)
* `ABI_TEST_DB` - name of the database containing the API test data (api tests)
+ * `DB_HOST` - (optional) hostname of database host
+ * `DB_USER` - (optional) username of database login
+ * `DB_PASS` - (optional) password for database login
+ * `SERVER_MODULE_PATH` - (optional) path on the Postgres server to Nominatim
+ * module shared library file
* `TEST_SETTINGS_TEMPLATE` - file to write temporary Nominatim settings to
* `REMOVE_TEMPLATE` - if true, the template database will not be reused during
the next run. Reusing the base templates speeds up tests
Scenario: Location off the coast
When sending jsonv2 reverse coordinates 54.046489113,8.5546870529
+ Then results contain
+ | display_name |
+ | Freie und Hansestadt Hamburg, Deutschland |
+
+ Scenario: When slightly outside town, the town is not shown
+ When sending jsonv2 reverse coordinates -32.122,-56.114
| zoom |
- | 5 |
+ | 15 |
Then results contain
- | error |
- | Unable to geocode |
+ | display_name |
+ | Tacuarembó, Uruguay |
When sending json search query "restaurant"
| bounded | viewbox |
| 1 | 9.93027,53.61634,10.10073,53.54500 |
- Then result has bounding box in 53.54500,53.61634,9.93027,10.10073
+ Then result has centroid in 53.54500,53.61634,9.93027,10.10073
Scenario: Prefer results within viewbox
When sending json search query "25 de Mayo" with address
'BUILDDIR' : os.path.join(os.path.split(__file__)[0], "../../build"),
'REMOVE_TEMPLATE' : False,
'KEEP_TEST_DB' : False,
+ 'DB_HOST' : None,
+ 'DB_USER' : None,
+ 'DB_PASS' : None,
'TEMPLATE_DB' : 'test_template_nominatim',
'TEST_DB' : 'test_nominatim',
'API_TEST_DB' : 'test_api_nominatim',
'TEST_SETTINGS_FILE' : '/tmp/nominatim_settings.php',
+ 'SERVER_MODULE_PATH' : None,
'PHPCOV' : False, # set to output directory to enable code coverage
}
def __init__(self, config):
self.build_dir = os.path.abspath(config['BUILDDIR'])
self.src_dir = os.path.abspath(os.path.join(os.path.split(__file__)[0], "../.."))
+ self.db_host = config['DB_HOST']
+ self.db_user = config['DB_USER']
+ self.db_pass = config['DB_PASS']
self.template_db = config['TEMPLATE_DB']
self.test_db = config['TEST_DB']
self.api_test_db = config['API_TEST_DB']
+ self.server_module_path = config['SERVER_MODULE_PATH']
self.local_settings_file = config['TEST_SETTINGS_FILE']
self.reuse_template = not config['REMOVE_TEMPLATE']
self.keep_scenario_db = config['KEEP_TEST_DB']
self.template_db_done = False
+ def connect_database(self, dbname):
+ dbargs = {'database': dbname}
+ if self.db_host:
+ dbargs['host'] = self.db_host
+ if self.db_user:
+ dbargs['user'] = self.db_user
+ if self.db_pass:
+ dbargs['password'] = self.db_pass
+ conn = psycopg2.connect(**dbargs)
+ return conn
+
def next_code_coverage_file(self):
fn = os.path.join(self.code_coverage_path, "%06d.cov" % self.code_coverage_id)
self.code_coverage_id += 1
def write_nominatim_config(self, dbname):
f = open(self.local_settings_file, 'w')
- f.write("<?php\n @define('CONST_Database_DSN', 'pgsql://@/%s');\n" % dbname)
+ f.write("<?php\n @define('CONST_Database_DSN', 'pgsql://%s:%s@%s/%s');\n" %
+ (self.db_user if self.db_user else '',
+ self.db_pass if self.db_pass else '',
+ self.db_host if self.db_host else '',
+ dbname))
f.write("@define('CONST_Osm2pgsql_Flatnode_File', null);\n")
f.close()
pass # ignore missing file
def db_drop_database(self, name):
- conn = psycopg2.connect(database='postgres')
+ conn = self.connect_database('postgres')
conn.set_isolation_level(0)
cur = conn.cursor()
cur.execute('DROP DATABASE IF EXISTS %s' % (name, ))
if self.reuse_template:
# check that the template is there
- conn = psycopg2.connect(database='postgres')
+ conn = self.connect_database('postgres')
cur = conn.cursor()
cur.execute('select count(*) from pg_database where datname = %s',
(self.template_db,))
self.write_nominatim_config(self.template_db)
self.run_setup_script('create-db', 'setup-db')
# remove external data to speed up indexing for tests
- conn = psycopg2.connect(database=self.template_db)
+ conn = self.connect_database(self.template_db)
cur = conn.cursor()
cur.execute("""select tablename from pg_tables
where tablename in ('gb_postcode', 'us_postcode')""")
def setup_db(self, context):
self.setup_template_db()
self.write_nominatim_config(self.test_db)
- conn = psycopg2.connect(database=self.template_db)
+ conn = self.connect_database(self.template_db)
conn.set_isolation_level(0)
cur = conn.cursor()
cur.execute('DROP DATABASE IF EXISTS %s' % (self.test_db, ))
cur.execute('CREATE DATABASE %s TEMPLATE = %s' % (self.test_db, self.template_db))
conn.close()
- context.db = psycopg2.connect(database=self.test_db)
+ context.db = self.connect_database(self.test_db)
if python_version[0] < 3:
psycopg2.extras.register_hstore(context.db, globally=False, unicode=True)
else:
self.db_drop_database(self.test_db)
def run_setup_script(self, *args, **kwargs):
+ if self.server_module_path:
+ kwargs = dict(kwargs)
+ kwargs['module_path'] = self.server_module_path
self.run_nominatim_script('setup', *args, **kwargs)
def run_update_script(self, *args, **kwargs):
def after_scenario(context, scenario):
if 'DB' in context.tags:
context.nominatim.teardown_db(context)
-
LazyFmt("Bad place id in column %s. Expected: %s, got: %s.",
column, expected, PlaceObjName(result, context.db)))
+def check_database_integrity(context):
+ """ Check some generic constraints on the tables.
+ """
+ # place_addressline should not have duplicate (place_id, address_place_id)
+ cur = context.db.cursor()
+ cur.execute("""SELECT count(*) FROM
+ (SELECT place_id, address_place_id, count(*) as c
+ FROM place_addressline GROUP BY place_id, address_place_id) x
+ WHERE c > 1""")
+ eq_(0, cur.fetchone()[0], "Duplicates found in place_addressline")
+
+
class NominatimID:
""" Splits a unique identifier for places into its components.
As place_ids cannot be used for testing, we use a unique
and ST_GeometryType(geometry) = 'ST_LineString'""")
context.db.commit()
context.nominatim.run_setup_script('calculate-postcodes', 'index', 'index-noanalyse')
+ check_database_integrity(context)
@when("updating places")
def update_place_table(context):
if cur.rowcount == 0:
break
+ check_database_integrity(context)
+
@when("marking for delete (?P<oids>.*)")
def delete_places(context, oids):
context.nominatim.run_setup_script(
assert_greater_equal(bbox[2], coord[2])
assert_less_equal(bbox[3], coord[3])
+@then(u'result (?P<lid>\d+ )?has centroid in (?P<coords>[\d,.-]+)')
+def step_impl(context, lid, coords):
+ if lid is None:
+ context.execute_steps("then at least 1 result is returned")
+ bboxes = zip(context.response.property_list('lat'),
+ context.response.property_list('lon'))
+ else:
+ context.execute_steps("then more than %sresults are returned" % lid)
+ res = context.response.result[int(lid)]
+ bboxes = [ (res['lat'], res['lon']) ]
+
+ coord = [ float(x) for x in coords.split(',') ]
+
+ for lat, lon in bboxes:
+ lat = float(lat)
+ lon = float(lon)
+ assert_greater_equal(lat, coord[0])
+ assert_less_equal(lat, coord[1])
+ assert_greater_equal(lon, coord[2])
+ assert_less_equal(lon, coord[3])
+
@then(u'there are(?P<neg> no)? duplicates')
def check_for_duplicates(context, neg):
context.execute_steps("then at least 1 result is returned")
require_once(CONST_BasePath.'/lib/init-cmd.php');
ini_set('memory_limit', '800M');
+# (long-opt, short-opt, min-occurs, max-occurs, num-arguments, num-arguments, type, help)
+
$aCMDOptions
= array(
'Create and setup nominatim search system',
}
}
-
// by default, use all but one processor, but never more than 15.
$iInstances = isset($aCMDResult['threads'])
? $aCMDResult['threads']
$iInstances = 1;
warn("resetting threads to $iInstances");
}
-if ($iInstances > getProcessorCount()) {
- $iInstances = getProcessorCount();
- warn("resetting threads to $iInstances");
-}
// Assume we can steal all the cache memory in the box (unless told otherwise)
if (isset($aCMDResult['osm2pgsql-cache'])) {
$iCacheMemory = getCacheMemoryMB();
}
+$sModulePath = CONST_Database_Module_Path;
+info('module path: ' . $sModulePath);
+
$aDSNInfo = DB::parseDSN(CONST_Database_DSN);
if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
if (!PEAR::isError($oDB)) {
fail('database already exists ('.CONST_Database_DSN.')');
}
- passthruCheckReturn('createdb -E UTF-8 -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']);
+
+ $sCreateDBCmd = 'createdb -E UTF-8 -p '.$aDSNInfo['port'].' '.$aDSNInfo['database'];
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sCreateDBCmd .= ' -U ' . $aDSNInfo['username'];
+ }
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sCreateDBCmd .= ' -h ' . $aDSNInfo['hostspec'];
+ }
+
+ $aProcEnv = null;
+ if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
+ }
+
+ $result = runWithEnv($sCreateDBCmd, $aProcEnv);
+ if ($result != 0) fail('Error executing external command: '.$sCreateDBCmd);
}
if ($aCMDResult['setup-db'] || $aCMDResult['all']) {
exit(1);
}
- // Try accessing the C module, so we know early if something is wrong
- // and can simply error out.
- $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
- $sSQL .= CONST_InstallPath."/module/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
- $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
- $oResult = $oDB->query($sSQL);
-
- if (PEAR::isError($oResult)) {
- echo "\nERROR: Failed to load nominatim module. Reason:\n";
- echo $oResult->userinfo."\n\n";
- exit(1);
+ if (!checkModulePresence()) {
+ fail('error loading nominatim.so module');
}
if (!file_exists(CONST_ExtraDataPath.'/country_osm_grid.sql.gz')) {
// is only defined in the subsequently called create_tables.
// Create dummies here that will be overwritten by the proper
// versions in create-tables.
- pgsqlRunScript('CREATE TABLE place_boundingbox ()');
- pgsqlRunScript('create type wikipedia_article_match as ()');
+ pgsqlRunScript('CREATE TABLE IF NOT EXISTS place_boundingbox ()');
+ pgsqlRunScript('CREATE TYPE wikipedia_article_match AS ()', false);
}
if ($aCMDResult['import-data'] || $aCMDResult['all']) {
$osm2pgsql .= ' -lsc -O gazetteer --hstore --number-processes 1';
$osm2pgsql .= ' -C '.$iCacheMemory;
$osm2pgsql .= ' -P '.$aDSNInfo['port'];
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $osm2pgsql .= ' -U ' . $aDSNInfo['username'];
+ }
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $osm2pgsql .= ' -H ' . $aDSNInfo['hostspec'];
+ }
+
+ $aProcEnv = null;
+ if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
+ }
+
$osm2pgsql .= ' -d '.$aDSNInfo['database'].' '.$aCMDResult['osm-file'];
- passthruCheckReturn($osm2pgsql);
+ runWithEnv($osm2pgsql, $aProcEnv);
$oDB =& getDB();
if (!$aCMDResult['ignore-errors'] && !chksql($oDB->getRow('select * from place limit 1'))) {
if ($aCMDResult['create-functions'] || $aCMDResult['all']) {
info('Create Functions');
$bDidSomething = true;
- if (!file_exists(CONST_InstallPath.'/module/nominatim.so')) {
- fail('nominatim module not built');
+
+ if (!checkModulePresence()) {
+ fail('error loading nominatim.so module');
}
+
create_sql_functions($aCMDResult);
}
fail(pg_last_error($aDBInstances[$iLoadThreads]->connection));
}
- $bAnyBusy = true;
- while ($bAnyBusy) {
- $bAnyBusy = false;
- for ($i = 0; $i <= $iLoadThreads; $i++) {
- if (pg_connection_busy($aDBInstances[$i]->connection)) $bAnyBusy = true;
+ $bFailed = false;
+ for ($i = 0; $i <= $iLoadThreads; $i++) {
+ while (($hPGresult = pg_get_result($aDBInstances[$i]->connection)) !== false) {
+ $resultStatus = pg_result_status($hPGresult);
+ // PGSQL_EMPTY_QUERY, PGSQL_COMMAND_OK, PGSQL_TUPLES_OK,
+ // PGSQL_COPY_OUT, PGSQL_COPY_IN, PGSQL_BAD_RESPONSE,
+ // PGSQL_NONFATAL_ERROR and PGSQL_FATAL_ERROR
+ echo 'Query result ' . $i . ' is: ' . $resultStatus . "\n";
+ if ($resultStatus != PGSQL_COMMAND_OK && $resultStatus != PGSQL_TUPLES_OK) {
+ $resultError = pg_result_error($hPGresult);
+ echo '-- error text ' . $i . ': ' . $resultError . "\n";
+ $bFailed = true;
+ }
}
- sleep(1);
- echo '.';
+ }
+ if ($bFailed) {
+ fail('SQL errors loading placex and/or location_property_osmline tables');
}
echo "\n";
info('Reanalysing database');
$bDidSomething = true;
$sOutputFile = '';
$sBaseCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$iInstances.$sOutputFile;
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sBaseCmd .= ' -H ' . $aDSNInfo['hostspec'];
+ }
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sBaseCmd .= ' -U ' . $aDSNInfo['username'];
+ }
+ $aProcEnv = null;
+ if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
+ }
+
info('Index ranks 0 - 4');
- passthruCheckReturn($sBaseCmd.' -R 4');
+ $iStatus = runWithEnv($sBaseCmd.' -R 4', $aProcEnv);
+ if ($iStatus != 0) {
+ fail('error status ' . $iStatus . ' running nominatim!');
+ }
if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
info('Index ranks 5 - 25');
- passthruCheckReturn($sBaseCmd.' -r 5 -R 25');
+ $iStatus = runWithEnv($sBaseCmd.' -r 5 -R 25', $aProcEnv);
+ if ($iStatus != 0) {
+ fail('error status ' . $iStatus . ' running nominatim!');
+ }
if (!$aCMDResult['index-noanalyse']) pgsqlRunScript('ANALYSE');
info('Index ranks 26 - 30');
- passthruCheckReturn($sBaseCmd.' -r 26');
+ $iStatus = runWithEnv($sBaseCmd.' -r 26', $aProcEnv);
+ if ($iStatus != 0) {
+ fail('error status ' . $iStatus . ' running nominatim!');
+ }
info('Index postcodes');
$oDB =& getDB();
if (!$aCMDResult['verbose']) {
$sCMD .= ' -q';
}
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
+ }
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sCMD .= ' -U ' . $aDSNInfo['username'];
+ }
+ $aProcEnv = null;
+ if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
+ }
$ahGzipPipes = null;
if (preg_match('/\\.gz$/', $sFilename)) {
2 => array('file', '/dev/null', 'a')
);
$ahPipes = null;
- $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
+ $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes, null, $aProcEnv);
if (!is_resource($hProcess)) fail('unable to start pgsql');
-
// TODO: error checking
while (!feof($ahPipes[1])) {
echo fread($ahPipes[1], 4096);
$aDSNInfo = DB::parseDSN(CONST_Database_DSN);
if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432;
$sCMD = 'pg_restore -p '.$aDSNInfo['port'].' -d '.$aDSNInfo['database'].' -Fc --clean '.$sDumpFile;
-
- $aDescriptors = array(
- 0 => array('pipe', 'r'),
- 1 => array('pipe', 'w'),
- 2 => array('file', '/dev/null', 'a')
- );
- $ahPipes = null;
- $hProcess = proc_open($sCMD, $aDescriptors, $ahPipes);
- if (!is_resource($hProcess)) fail('unable to start pg_restore');
-
- fclose($ahPipes[0]);
-
- // TODO: error checking
- while (!feof($ahPipes[1])) {
- echo fread($ahPipes[1], 4096);
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sCMD .= ' -h ' . $aDSNInfo['hostspec'];
+ }
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sCMD .= ' -U ' . $aDSNInfo['username'];
+ }
+ $aProcEnv = null;
+ if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
}
- fclose($ahPipes[1]);
- $iReturn = proc_close($hProcess);
+ $iReturn = runWithEnv($sCMD, $aProcEnv);
}
-function passthruCheckReturn($cmd)
+function passthruCheckReturn($sCmd)
{
- $result = -1;
- passthru($cmd, $result);
- if ($result != 0) fail('Error executing external command: '.$cmd);
+ $iResult = -1;
+ passthru($sCmd, $iResult);
}
function replace_tablespace($sTemplate, $sTablespace, $sSql)
function create_sql_functions($aCMDResult)
{
+ global $sModulePath;
$sTemplate = file_get_contents(CONST_BasePath.'/sql/functions.sql');
- $sTemplate = str_replace('{modulepath}', CONST_InstallPath.'/module', $sTemplate);
+ $sTemplate = str_replace('{modulepath}', $sModulePath, $sTemplate);
if ($aCMDResult['enable-diff-updates']) {
$sTemplate = str_replace('RETURN NEW; -- %DIFFUPDATES%', '--', $sTemplate);
}
}
pgsqlRunScript($sTemplate);
}
+
+function checkModulePresence()
+{
+ // Try accessing the C module, so we know early if something is wrong
+ // and can simply error out.
+ global $sModulePath;
+ $sSQL = "CREATE FUNCTION nominatim_test_import_func(text) RETURNS text AS '";
+ $sSQL .= $sModulePath."/nominatim.so', 'transliteration' LANGUAGE c IMMUTABLE STRICT";
+ $sSQL .= ';DROP FUNCTION nominatim_test_import_func(text);';
+
+ $oDB =& getDB();
+ $oResult = $oDB->query($sSQL);
+
+ $bResult = true;
+
+ if (PEAR::isError($oResult)) {
+ echo "\nERROR: Failed to load nominatim module. Reason:\n";
+ echo $oResult->userinfo."\n\n";
+ $bResult = false;
+ }
+
+ return $bResult;
+}
require_once(CONST_BasePath.'/lib/init-cmd.php');
ini_set('memory_limit', '800M');
+# (long-opt, short-opt, min-occurs, max-occurs, num-arguments, num-arguments, type, help)
+
$aCMDOptions
= array(
'Import / update / index osm data',
array('init-updates', '', 0, 1, 0, 0, 'bool', 'Set up database for updating'),
array('check-for-updates', '', 0, 1, 0, 0, 'bool', 'Check if new updates are available'),
+ array('no-update-functions', '', 0, 1, 0, 0, 'bool', 'Do not update trigger functions to support differential updates (assuming the diff update logic is already present)'),
array('import-osmosis', '', 0, 1, 0, 0, 'bool', 'Import updates once'),
array('import-osmosis-all', '', 0, 1, 0, 0, 'bool', 'Import updates forever'),
array('no-index', '', 0, 1, 0, 0, 'bool', 'Do not index the new data'),
echo "WARNING: resetting cache memory to $iCacheMemory\n";
}
$sOsm2pgsqlCmd = CONST_Osm2pgsql_Binary.' -klas --number-processes 1 -C '.$iCacheMemory.' -O gazetteer -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'];
+if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sOsm2pgsqlCmd .= ' -U ' . $aDSNInfo['username'];
+}
+if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sOsm2pgsqlCmd .= ' -H ' . $aDSNInfo['hostspec'];
+}
+$aProcEnv = null;
+if (isset($aDSNInfo['password']) && $aDSNInfo['password']) {
+ $aProcEnv = array_merge(array('PGPASSWORD' => $aDSNInfo['password']), $_ENV);
+}
+
if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
$sOsm2pgsqlCmd .= ' --flat-nodes '.CONST_Osm2pgsql_Flatnode_File;
}
echo "and have set up CONST_Pyosmium_Binary to point to pyosmium-get-changes.\n";
fail('pyosmium-get-changes not found or not usable');
}
- $sSetup = CONST_InstallPath.'/utils/setup.php';
- $iRet = -1;
- passthru($sSetup.' --create-functions --enable-diff-updates', $iRet);
- if ($iRet != 0) {
- fail('Error running setup script');
+ if (!$aResult['no-update-functions']) {
+ $sSetup = CONST_InstallPath.'/utils/setup.php';
+ $iRet = -1;
+ passthru($sSetup.' --create-functions --enable-diff-updates', $iRet);
+ if ($iRet != 0) {
+ fail('Error running setup script');
+ }
}
$sDatabaseDate = getDatabaseDate($oDB);
// Import the file
$sCMD = $sOsm2pgsqlCmd.' '.$sNextFile;
echo $sCMD."\n";
- exec($sCMD, $sJunk, $iErrorLevel);
+ $iErrorLevel = runWithEnv($sCMD, $aProcEnv);
if ($iErrorLevel) {
fail("Error from osm2pgsql, $iErrorLevel\n");
// import generated change file
$sCMD = $sOsm2pgsqlCmd.' '.$sTemporaryFile;
echo $sCMD."\n";
- exec($sCMD, $sJunk, $iErrorLevel);
+ $iErrorLevel = runWithEnv($sCMD, $aProcEnv);
if ($iErrorLevel) {
fail("osm2pgsql exited with error level $iErrorLevel\n");
}
}
if ($aResult['index']) {
- passthru(CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$aResult['index-instances'].' -r '.$aResult['index-rank']);
+ $sCmd = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$aResult['index-instances'].' -r '.$aResult['index-rank'];
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sCmd .= ' -H ' . $aDSNInfo['hostspec'];
+ }
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sCmd .= ' -U ' . $aDSNInfo['username'];
+ }
+
+ runWithEnv($sCmd, $aProcEnv);
}
if ($aResult['import-osmosis'] || $aResult['import-osmosis-all']) {
$sCMDDownload = CONST_Pyosmium_Binary.' --server '.CONST_Replication_Url.' -o '.$sImportFile.' -s '.CONST_Replication_Max_Diff_size;
$sCMDImport = $sOsm2pgsqlCmd.' '.$sImportFile;
$sCMDIndex = CONST_InstallPath.'/nominatim/nominatim -i -d '.$aDSNInfo['database'].' -P '.$aDSNInfo['port'].' -t '.$aResult['index-instances'];
+ if (isset($aDSNInfo['hostspec']) && $aDSNInfo['hostspec']) {
+ $sCMDIndex .= ' -H ' . $aDSNInfo['hostspec'];
+ }
+ if (isset($aDSNInfo['username']) && $aDSNInfo['username']) {
+ $sCMDIndex .= ' -U ' . $aDSNInfo['username'];
+ }
while (true) {
$fStartTime = time();
$fCMDStartTime = time();
echo $sCMDImport."\n";
unset($sJunk);
- exec($sCMDImport, $sJunk, $iErrorLevel);
+ $iErrorLevel = runWithEnv($sCMDImport, $aProcEnv);
if ($iErrorLevel) {
echo "Error executing osm2pgsql: $iErrorLevel\n";
exit($iErrorLevel);
$fCMDStartTime = time();
echo "$sThisIndexCmd\n";
- exec($sThisIndexCmd, $sJunk, $iErrorLevel);
+ $iErrorLevel = runWithEnv($sThisIndexCmd, $aProcEnv);
if ($iErrorLevel) {
echo "Error: $iErrorLevel\n";
exit($iErrorLevel);