]> git.openstreetmap.org Git - nominatim.git/commitdiff
Merge remote-tracking branch 'markus/master' into master
authorSarah Hoffmann <lonvia@denofr.de>
Sat, 14 May 2016 19:09:57 +0000 (21:09 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Sat, 14 May 2016 19:09:57 +0000 (21:09 +0200)
21 files changed:
lib/Geocode.php
lib/PlaceLookup.php
lib/ReverseGeocode.php
lib/template/address-json.php
lib/template/address-jsonv2.php
lib/template/address-xml.php
lib/template/search-json.php
lib/template/search-jsonv2.php
lib/template/search-xml.php
nominatim/index.c
nominatim/index.h
sql/functions.sql
sql/indices.src.sql
sql/tables.sql
tests/features/api/reverse.feature
tests/features/db/import/interpolation.feature
tests/features/db/update/interpolation.feature
tests/steps/db_results.py
tests/steps/db_setup.py
tests/steps/terrain.py
utils/setup.php

index 611ca6de630f4252dad05dba6c4764c64077d336..e6e6e3eb7ec1208cd69a3ce264f3d2902f8db0c9 100644 (file)
 
                        if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank)
                        {
+                               //only Tiger housenumbers and interpolation lines need to be interpolated, because they are saved as lines 
+                               // with start- and endnumber, the common osm housenumbers are usually saved as points
+                               $sHousenumbers = "";
+                               $i = 0;
+                               $length = count($aPlaceIDs);
+                               foreach($aPlaceIDs as $placeID => $housenumber)
+                               {
+                                       $i++;
+                                       $sHousenumbers .= "(".$placeID.", ".$housenumber.")";
+                                       if($i<$length)
+                                               $sHousenumbers .= ", ";
+                               }
                                if (CONST_Use_US_Tiger_Data)
                                {
-                                       //query also location_property_tiger and location_property_aux
                                        //Tiger search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
-                                       //only Tiger housenumbers need to be interpolated, because they are saved as lines with start- and endnumber, the common osm housenumbers are usually saved as points
-                                       $sHousenumbers = "";
-                                       $i = 0;
-                                       $length = count($aPlaceIDs);
-                                       foreach($aPlaceIDs as $placeID => $housenumber)
-                                       {
-                                               $i++;
-                                               $sHousenumbers .= "(".$placeID.", ".$housenumber.")";
-                                               if($i<$length)
-                                                       $sHousenumbers .= ", ";
-                                       }
-
-                                       $sSQL .= "union ";
-                                       $sSQL .= "select 'T' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, 30 as rank_search, 30 as rank_address, min(place_id) as place_id, min(parent_place_id) as parent_place_id, 'us' as country_code";
+                                       $sSQL .= " union";
+                                       $sSQL .= " select 'T' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, 30 as rank_search, 30 as rank_address, min(place_id) as place_id, min(parent_place_id) as parent_place_id, 'us' as country_code";
                                        $sSQL .= ", get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress ";
                                        $sSQL .= ", null as placename";
                                        $sSQL .= ", null as ref";
                                        $sSQL .= ", null as extra_place ";
                                        $sSQL .= " from (select place_id";
                                        //interpolate the Tiger housenumbers here
-                                       $sSQL .= ", ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) as centroid, parent_place_id, housenumber_for_place ";
-                                       $sSQL .= "from (location_property_tiger ";
+                                       $sSQL .= ", ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) as centroid, parent_place_id, housenumber_for_place";
+                                       $sSQL .= " from (location_property_tiger ";
                                        $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
                                        $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
                                        $sSQL .= " group by place_id, housenumber_for_place"; //is this group by really needed?, place_id + housenumber (in combination) are unique
                                        if (!$this->bDeDupe) $sSQL .= ", place_id ";
                                }
+                               // osmline
+                               // interpolation line search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
+                               $sSQL .= " union ";
+                               $sSQL .= "select 'W' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, 30 as rank_search, 30 as rank_address, min(place_id) as place_id, min(parent_place_id) as parent_place_id, calculated_country_code as country_code, ";
+                               $sSQL .= "get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress, ";
+                               $sSQL .= "null as placename, ";
+                               $sSQL .= "null as ref, ";
+                               if ($this->bIncludeExtraTags) $sSQL .= "null as extra, ";
+                               if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
+                               $sSQL .= " avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
+                               $sSQL .= $sImportanceSQL."-0.1 as importance, ";  // slightly smaller than the importance for normal houses with rank 30, which is 0
+                               $sSQL .= " (select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p";
+                               $sSQL .= " where s.place_id = min(blub.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance,";
+                               $sSQL .= " null as extra_place ";
+                               $sSQL .= " from (select place_id, calculated_country_code ";
+                               //interpolate the housenumbers here
+                               $sSQL .= ", CASE WHEN startnumber != endnumber THEN ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) ";
+                               $sSQL .= " ELSE ST_LineInterpolatePoint(linegeo, 0.5) END as centroid";
+                               $sSQL .= ", parent_place_id, housenumber_for_place ";
+                               $sSQL .= " from (location_property_osmline ";
+                               $sSQL .= " join (values ".$sHousenumbers.") as housenumbers(place_id, housenumber_for_place) using(place_id)) ";
+                               $sSQL .= " where housenumber_for_place>=0 and 30 between $this->iMinAddressRank and $this->iMaxAddressRank) as blub"; //postgres wants an alias here
+                               $sSQL .= " group by place_id, housenumber_for_place, calculated_country_code "; //is this group by really needed?, place_id + housenumber (in combination) are unique
+                               if (!$this->bDeDupe) $sSQL .= ", place_id ";
 
                                if (CONST_Use_Aux_Location_data)
                                {
 
                        // Do we have anything that looks like a lat/lon pair?
                        if ( $aLooksLike = looksLikeLatLonPair($sQuery) )
-            {
+                       {
                                $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
                                $sQuery = $aLooksLike['query'];
                        }
                                                        if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
                                                        {
                                                                $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
-                                                               $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
+                                $aOrder[] = "";
+                                                               $aOrder[0] = " (exists(select place_id from placex where parent_place_id = search_name.place_id";
+                                $aOrder[0] .= " and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) ";
+                                                               // also housenumbers from interpolation lines table are needed
+                                                               $aOrder[0] .= " or exists(select place_id from location_property_osmline where parent_place_id = search_name.place_id";
+                                $aOrder[0] .= " and ".intval($aSearch['sHouseNumber']).">=startnumber and ".intval($aSearch['sHouseNumber'])."<=endnumber limit 1))";
+                                                               $aOrder[0] .= " desc";
                                                        }
 
                                                        // TODO: filter out the pointless search terms (2 letter name tokens and less)
                                                        //now search for housenumber, if housenumber provided
                                                        if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
                                                        {
+                                                               $searchedHousenumber = intval($aSearch['sHouseNumber']);
                                                                $aRoadPlaceIDs = $aPlaceIDs;
                                                                $sPlaceIDs = join(',',$aPlaceIDs);
 
-                                                               // Now they are indexed look for a house attached to a street we found
+                                                               // Now they are indexed, look for a house attached to a street we found
                                                                $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
                                                                $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
                                                                if (sizeof($this->aExcludePlaceIDs))
                                                                $sSQL .= " limit $this->iLimit";
                                                                if (CONST_Debug) var_dump($sSQL);
                                                                $aPlaceIDs = $this->oDB->getCol($sSQL);
+                                                               
+                                                               // if nothing found, search in the interpolation line table
+                                                               if(!sizeof($aPlaceIDs))
+                                                               {
+                                                                       // do we need to use transliteration and the regex for housenumbers???
+                                                                       //new query for lines, not housenumbers anymore
+                                                                       if($searchedHousenumber%2 == 0){
+                                                                               //if housenumber is even, look for housenumber in streets with interpolationtype even or all
+                                                                               $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='even' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
+                                                                       }else{
+                                                                               //look for housenumber in streets with interpolationtype odd or all
+                                                                               $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='odd' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
+                                                                       }
 
+                                                                       if (sizeof($this->aExcludePlaceIDs))
+                                                                       {
+                                                                               $sSQL .= " and parent_place_id not in (".join(',', $this->aExcludePlaceIDs).")";
+                                                                       }
+                                                                       //$sSQL .= " limit $this->iLimit";
+                                                                       if (CONST_Debug) var_dump($sSQL);
+                                                                       //get place IDs
+                                                                       $aPlaceIDs = $this->oDB->getCol($sSQL, 0);
+                                                               }
+                                                                       
                                                                // If nothing found try the aux fallback table
                                                                if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs))
                                                                {
                                                                        $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
                                                                        if (sizeof($this->aExcludePlaceIDs))
                                                                        {
-                                                                               $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
+                                                                               $sSQL .= " and parent_place_id not in (".join(',',$this->aExcludePlaceIDs).")";
                                                                        }
                                                                        //$sSQL .= " limit $this->iLimit";
                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                        $aPlaceIDs = $this->oDB->getCol($sSQL);
                                                                }
+
                                                                //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
-                                                               $searchedHousenumber = intval($aSearch['sHouseNumber']);
                                                                if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs))
                                                                {
                                                                        //new query for lines, not housenumbers anymore
                                                                        //set to -1, if no housenumbers were found
                                                                        $searchedHousenumber = -1;
                                                                }
-                                //else: housenumber was found, remains saved in searchedHousenumber
+                                                               //else: housenumber was found, remains saved in searchedHousenumber
                                                        }
 
 
                                        {
                                                // Need to verify passes rank limits before dropping out of the loop (yuk!)
                                                // reduces the number of place ids, like a filter
+                                               // rank_address is 30 for interpolated housenumbers
                                                $sSQL = "select place_id from placex where place_id in (".join(',',array_keys($aResultPlaceIDs)).") ";
                                                $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
                                                if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
                                                        $sSQL .= "and (30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
                                                        if ($this->aAddressRankList) $sSQL .= " OR 30 in (".join(',',$this->aAddressRankList).")";
                                                }
-                                               $sSQL .= ")";
+                                               $sSQL .= ") UNION select place_id from location_property_osmline where place_id in (".join(',',array_keys($aResultPlaceIDs)).")";
+                                               $sSQL .= " and (30 between $this->iMinAddressRank and $this->iMaxAddressRank)";
                                                if (CONST_Debug) var_dump($sSQL);
                                                $aFilteredPlaceIDs = $this->oDB->getCol($sSQL);
                                                $tempIDs = array();
                                                foreach($aFilteredPlaceIDs as $placeID)
-                        {
+                                               {
                                                        $tempIDs[$placeID] = $aResultPlaceIDs[$placeID];  //assign housenumber to placeID
                                                }
                                                $aResultPlaceIDs = $tempIDs;
index c1fec6496ef66ca3118f7b214b2c7ee6f85a7173..be60ff0782ba02221ae60ed369455b895a4c6bc8 100644 (file)
                        {
                                $this->setOSMID($details['osm_type'], $details['osm_id']);
                        }
-                       if (isset($details['fraction'])) $this->fTigerFraction = $details['fraction'];
+                       if (isset($details['fraction'])) $this->fInterpolFraction = $details['fraction'];
 
                        return $this->lookup();
                }
                                if ($this->bNameDetails) $sSQL .= " null as names,";
                                $sSQL .= " ST_X(point) as lon, ST_Y(point) as lat from (select *, ST_LineInterpolatePoint(linegeo, (housenumber-startnumber::float)/(endnumber-startnumber)::float) as point from ";
                                $sSQL .= " (select *, ";
-                               $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
-                               $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fTigerFraction."*(endnumber-startnumber)+startnumber+1)/2)::int*2";
-                               $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fTigerFraction."*(endnumber-startnumber)+startnumber)::int";
+                               $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
+                               $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
+                               $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)::int";
                                $sSQL .= " END as housenumber";
                                $sSQL .= " from location_property_tiger where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
                        }
+                       else if ($this->sType == 'interpolation')
+                       {
+                               $sSQL = "select place_id, partition, 'W' as osm_type, osm_id, 'place' as class, 'house' as type, null admin_level, housenumber, null as street, null as isin, postcode,";
+                               $sSQL .= " calculated_country_code as country_code, parent_place_id, null as linked_place_id, 30 as rank_address, 30 as rank_search,";
+                               $sSQL .= " (0.75-(30::float/40)) as importance, null as indexed_status, null as indexed_date, null as wikipedia, calculated_country_code, ";
+                               $sSQL .= " get_address_by_language(place_id, housenumber, $sLanguagePrefArraySQL) as langaddress,";
+                               $sSQL .= " null as placename,";
+                               $sSQL .= " null as ref,";
+                               if ($this->bExtraTags) $sSQL .= " null as extra,";
+                               if ($this->bNameDetails) $sSQL .= " null as names,";
+                               $sSQL .= " ST_X(point) as lon, ST_Y(point) as lat from (select *, ST_LineInterpolatePoint(linegeo, (housenumber-startnumber::float)/(endnumber-startnumber)::float) as point from ";
+                               $sSQL .= " (select *, ";
+                               $sSQL .= " CASE WHEN interpolationtype='odd' THEN floor((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2+1";
+                               $sSQL .= " WHEN interpolationtype='even' THEN ((".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)/2)::int*2";
+                               $sSQL .= " WHEN interpolationtype='all' THEN (".$this->fInterpolFraction."*(endnumber-startnumber)+startnumber)::int";
+                               $sSQL .= " END as housenumber";
+                               $sSQL .= " from location_property_osmline where place_id = ".(int)$this->iPlaceID.") as blub1) as blub2";
+                               // testcase: interpolationtype=odd, startnumber=1000, endnumber=1006, fInterpolFraction=1 => housenumber=1007 => error in st_lineinterpolatepoint
+                               // but this will never happen, because if the searched point is that close to the endnumber, the endnumber house will be directly taken from placex (in ReverseGeocode.php line 220)
+                               // and not interpolated
+                       }
                        else
                        {
                                $sSQL = "select placex.place_id, partition, osm_type, osm_id, class, type, admin_level, housenumber, street, isin, postcode, country_code, parent_place_id, linked_place_id, rank_address, rank_search, ";
 
                        if ($this->bAddressDetails)
                        {
-                               if(CONST_Use_US_Tiger_Data && $this->sType == 'tiger') // to get addressdetails for tiger data, the housenumber is needed
+                               if(CONST_Use_US_Tiger_Data && $this->sType == 'tiger' || $this->sType == 'interpolation') // to get addressdetails for tiger data, the housenumber is needed
                                        $aAddress = $this->getAddressNames($aPlace['housenumber']);
                                else
                                        $aAddress = $this->getAddressNames();
index 75a9b71c3fd902c1bbcc8a613afa5e2acd0ce162..05093af388cfaf36ddd192290f3d96409d8af7d3 100644 (file)
                        $fMaxAreaDistance = 1;
                        $bIsInUnitedStates = false;
                        $bPlaceIsTiger = false;
+                       $bPlaceIsLine = false;
                        while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
                        {
                                $fSearchDiam = $fSearchDiam * 2;
                                $iParentPlaceID = $aPlace['parent_place_id'];
                                $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
                        }
+                       // if a street or house was found, look in interpolation lines table
+                       if ($iMaxRank_orig >= 28 && $aPlace && $aPlace['rank_search'] >= 26)
+                       {
+                               // if a house was found, search the interpolation line that is at least as close as the house
+                               $sSQL = 'SELECT place_id, parent_place_id, 30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
+                               $sSQL .= ' FROM location_property_osmline';
+                               $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';
+                               $sSQL .= ' and indexed_status = 0 ';
+                               $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
+                               
+                               if (CONST_Debug)
+                               {
+                                       $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
+                                       var_dump($sSQL);
 
+                                       $aAllHouses = $this->oDB->getAll($sSQL);
+                                       foreach($aAllHouses as $i)
+                                       {
+                                               echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
+                                       }
+                               }
+                               $aPlaceLine = $this->oDB->getRow($sSQL);
+                               if (PEAR::IsError($aPlaceLine))
+                               {
+                                       failInternalError("Could not determine closest housenumber on an osm interpolation line.", $sSQL, $aPlaceLine);
+                               }
+                               if ($aPlaceLine)
+                               {
+                                       if (CONST_Debug) var_dump('found housenumber in interpolation lines table', $aPlaceLine);
+                                       if ($aPlace['rank_search'] == 30)
+                                       {
+                                               // if a house was already found in placex, we have to find out, 
+                                               // if the placex house or the interpolated house are closer to the searched point
+                                               // distance between point and placex house
+                                               $sSQL = 'SELECT ST_distance('.$sPointSQL.', house.geometry) as distance FROM placex as house WHERE house.place_id='.$iPlaceID;
+                                               $aDistancePlacex = $this->oDB->getRow($sSQL);
+                                               if (PEAR::IsError($aDistancePlacex))
+                                               {
+                                                       failInternalError("Could not determine distance between searched point and placex house.", $sSQL, $aDistancePlacex);
+                                               }
+                                               $fDistancePlacex = $aDistancePlacex['distance'];
+                                               // distance between point and interpolated house (fraction on interpolation line)
+                                               $sSQL = 'SELECT ST_distance('.$sPointSQL.', ST_LineInterpolatePoint(linegeo, '.$aPlaceLine['fraction'].')) as distance';
+                                               $sSQL .= ' FROM location_property_osmline WHERE place_id = '.$aPlaceLine['place_id'];
+                                               $aDistanceInterpolation = $this->oDB->getRow($sSQL);
+                                               if (PEAR::IsError($aDistanceInterpolation))
+                                               {
+                                                       failInternalError("Could not determine distance between searched point and interpolated house.", $sSQL, $aDistanceInterpolation);
+                                               }
+                                               $fDistanceInterpolation = $aDistanceInterpolation['distance'];
+                                               if ($fDistanceInterpolation < $fDistancePlacex)
+                                               {
+                                                       // interpolation is closer to point than placex house
+                                                       $bPlaceIsLine = true;
+                                                       $aPlace = $aPlaceLine;
+                                                       $iPlaceID = $aPlaceLine['place_id'];
+                                                       $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
+                                                       $fFraction = $aPlaceLine['fraction'];
+                                               }
+                                               // else: nothing to do, take placex house from above
+                                       }
+                                       else
+                                       {
+                                               $bPlaceIsLine = true;
+                                               $aPlace = $aPlaceLine;
+                                               $iPlaceID = $aPlaceLine['place_id'];
+                                               $iParentPlaceID = $aPlaceLine['parent_place_id']; // the street
+                                               $fFraction = $aPlaceLine['fraction'];
+                                       }
+                               }
+                       }
+                       
                        // Only street found? If it's in the US we can check TIGER data for nearest housenumber
                        if (CONST_Use_US_Tiger_Data && $bIsInUnitedStates && $iMaxRank_orig >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 ))
                        {
                                }
 
                                $aPlaceTiger = $this->oDB->getRow($sSQL);
-                               if (PEAR::IsError($aPlace))
+                               if (PEAR::IsError($aPlaceTiger))
                                {
                                        failInternalError("Could not determine closest Tiger place.", $sSQL, $aPlaceTiger);
                                }
                                if ($aPlaceTiger)
                                {
-                                       if (CONST_Debug) var_dump('found Tiger place', $aPlaceTiger);
+                                       if (CONST_Debug) var_dump('found Tiger housenumber', $aPlaceTiger);
                                        $bPlaceIsTiger = true;
                                        $aPlace = $aPlaceTiger;
                                        $iPlaceID = $aPlaceTiger['place_id'];
                                        $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
-                                       $iFraction = $aPlaceTiger['fraction'];
+                                       $fFraction = $aPlaceTiger['fraction'];
                                }
                        }
 
                        // The point we found might be too small - use the address to find what it is a child of
                        if ($iPlaceID && $iMaxRank < 28)
                        {
-                               if ($aPlace['rank_search'] > 28 && $iParentPlaceID && !$bPlaceIsTiger)
+                               if (($aPlace['rank_search'] > 28 || $bPlaceIsTiger || $bPlaceIsLine) && $iParentPlaceID)
                                {
                                        $iPlaceID = $iParentPlaceID;
                                }
                                        $iPlaceID = $aPlace['place_id'];
                                }
                        }
-
                        return array('place_id' => $iPlaceID,
-                                               'type' => $bPlaceIsTiger ? 'tiger' : 'osm',
-                                               'fraction' => $bPlaceIsTiger ? $iFraction : -1);
+                                               'type' => $bPlaceIsTiger ? 'tiger' : $bPlaceIsLine ? 'interpolation' : 'osm',
+                                               'fraction' => ($bPlaceIsTiger || $bPlaceIsLine) ? $fFraction : -1);
                }
                
        }
index 6fd101bbaac0f97c511c7250b99e62554c52e654..6254357a711b3727498a722b06c61640d04ca308 100644 (file)
@@ -12,7 +12,8 @@
        {
                if (isset($aPlace['place_id'])) $aFilteredPlaces['place_id'] = $aPlace['place_id'];
                $aFilteredPlaces['licence'] = "Data Â© OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright";
-               $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
+               $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':
+               ($aPlace['osm_type'] == 'T'?'tiger':($aPlace['osm_type'] == 'I'?'interpolation':'')))));
                if ($sOSMType)
                {
                        $aFilteredPlaces['osm_type'] = $sOSMType;
index 894038758411117edf6ed9a9a6350a3b3b3c0ec3..64c871c10c9d748c057a4df35f30d1bdad0e12bb 100644 (file)
@@ -12,7 +12,8 @@
        {
                if ($aPlace['place_id']) $aFilteredPlaces['place_id'] = $aPlace['place_id'];
                $aFilteredPlaces['licence'] = "Data Â© OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright";
-               $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
+               $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':
+               ($aPlace['osm_type'] == 'T'?'tiger':($aPlace['osm_type'] == 'I'?'interpolation':'')))));
                if ($sOSMType)
                {
                        $aFilteredPlaces['osm_type'] = $sOSMType;
index 109e66a17ef41d2e3426f6b4061c535817be6435..a3369d34890e3b0a95c3cd897e5bce34155b581d 100644 (file)
@@ -22,7 +22,8 @@
        {
                echo "<result";
                if ($aPlace['place_id']) echo ' place_id="'.$aPlace['place_id'].'"';
-               $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':'')));
+               $sOSMType = ($aPlace['osm_type'] == 'N'?'node':($aPlace['osm_type'] == 'W'?'way':($aPlace['osm_type'] == 'R'?'relation':
+               ($aPlace['osm_type'] == 'T'?'tiger':($aPlace['osm_type'] == 'I'?'interpolation':'')))));
                if ($sOSMType) echo ' osm_type="'.$sOSMType.'"'.' osm_id="'.$aPlace['osm_id'].'"';
                if ($aPlace['ref']) echo ' ref="'.htmlspecialchars($aPlace['ref']).'"';
                if (isset($aPlace['lat'])) echo ' lat="'.htmlspecialchars($aPlace['lat']).'"';
index 0bfbc90496e8bfd99346e9491b52db015b75994b..5d5ebfe81f9782706c4cb9ed24a74920c9baf15e 100644 (file)
@@ -9,7 +9,8 @@
                                'licence'=>"Data Â© OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
                        );
         
-               $sOSMType = ($aPointDetails['osm_type'] == 'N'?'node':($aPointDetails['osm_type'] == 'W'?'way':($aPointDetails['osm_type'] == 'R'?'relation':($aPointDetails['osm_type'] == 'T'?'tiger':''))));
+               $sOSMType = ($aPointDetails['osm_type'] == 'N'?'node':($aPointDetails['osm_type'] == 'W'?'way':($aPointDetails['osm_type'] == 'R'?
+                                       'relation':($aPointDetails['osm_type'] == 'T'?'tiger':($aPointDetails['osm_type'] == 'I'?'interpolation':'')))));
                if ($sOSMType)
                {
                        $aPlace['osm_type'] = $sOSMType;
index 6f23ae26b431e601ca242a8e0256a24a316a069b..251388b51a4157e6a7bd54871936eb79880d028f 100644 (file)
@@ -7,7 +7,7 @@
                                'licence'=>"Data Â© OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
                        );
 
-               $sOSMType = ($aPointDetails['osm_type'] == 'N'?'node':($aPointDetails['osm_type'] == 'W'?'way':($aPointDetails['osm_type'] == 'R'?'relation':($aPointDetails['osm_type'] == 'T'?'tiger':''))));
+               $sOSMType = ($aPointDetails['osm_type'] == 'N'?'node':($aPointDetails['osm_type'] == 'W'?'way':($aPointDetails['osm_type'] == 'R'?'relation':($aPointDetails['osm_type'] == 'T'?'tiger':($aPointDetails['osm_type'] == 'I'?'interpolation':'')))));
                if ($sOSMType)
                {
                        $aPlace['osm_type'] = $sOSMType;
index 54217933ef7d8962a5e3934b3186b6ae9cb8d57d..dd02b7c7a2364c87ed18a10854dbd6644c4300a5 100644 (file)
@@ -25,7 +25,8 @@
        foreach($aSearchResults as $iResNum => $aResult)
        {
                echo "<place place_id='".$aResult['place_id']."'";
-               $sOSMType = ($aResult['osm_type'] == 'N'?'node':($aResult['osm_type'] == 'W'?'way':($aResult['osm_type'] == 'R'?'relation':($aResult['osm_type'] == 'T'?'tiger':''))));
+               $sOSMType = ($aPointDetails['osm_type'] == 'N'?'node':($aPointDetails['osm_type'] == 'W'?'way':($aPointDetails['osm_type'] == 'R'?'relation':
+               ($aPointDetails['osm_type'] == 'T'?'tiger':($aPointDetails['osm_type'] == 'I'?'interpolation':'')))));
                if ($sOSMType)
                {
                        echo " osm_type='$sOSMType'";
index 13f292737082f5a4185c45086b6ff90d265c91eb..90a3abea52a357ecac550daf4593a48c038e23a8 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * triggers indexing (reparenting etc.) through setting resetting indexed_status: update placex/osmline set indexed_status = 0 where indexed_status > 0
+ * triggers placex_update and osmline_update
 */
 
 #include <stdio.h>
 
 extern int verbose;
 
-void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile)
+void run_indexing(int rank, int interpolation, PGconn *conn, int num_threads, 
+struct index_thread_data * thread_data, const char *structuredoutputfile)
 {
-    struct index_thread_data * thread_data;
-    pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
     int tuples, count, sleepcount;
-
+    pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
+    
     time_t rankStartTime;
     int rankTotalTuples;
     int rankCountTuples;
     float rankPerSecond;
-
-    PGconn *conn;
-    PGresult * res;
+    
     PGresult * resSectors;
     PGresult * resPlaces;
     PGresult * resNULL;
-
-    int rank;
+    
     int i;
     int iSector;
     int iResult;
-
+    
     const char *paramValues[2];
     int         paramLengths[2];
     int         paramFormats[2];
     uint32_t    paramRank;
     uint32_t    paramSector;
     uint32_t    sector;
+    
+    xmlTextWriterPtr writer;
+    pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
+    
+    // Create the output file
+    writer = NULL;
+    if (structuredoutputfile)
+    {
+        writer = nominatim_exportXMLStart(structuredoutputfile);
+    }
+    
+    if (interpolation)
+    {
+        fprintf(stderr, "Starting interpolation lines (location_property_osmline)\n");
+    }
+    else
+    {
+        fprintf(stderr, "Starting rank %d\n", rank);
+    }
+    
+    rankCountTuples = 0;
+    rankPerSecond = 0;
+
+    paramRank = PGint32(rank);
+    paramValues[0] = (char *)&paramRank;
+    paramLengths[0] = sizeof(paramRank);
+    paramFormats[0] = 1;
+    
+    if (interpolation)
+    {
+        resSectors = PQexecPrepared(conn, "index_sectors_osmline", 0, NULL, 0, NULL, 1);
+    }
+    else
+    {
+        resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
+    }
+    if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
+    {
+        fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
+        PQclear(resSectors);
+        exit(EXIT_FAILURE);
+    }
+    if (PQftype(resSectors, 0) != PG_OID_INT4)
+    {
+        fprintf(stderr, "Sector value has unexpected type\n");
+        PQclear(resSectors);
+        exit(EXIT_FAILURE);
+    }
+    if (PQftype(resSectors, 1) != PG_OID_INT8)
+    {
+        fprintf(stderr, "Sector value has unexpected type\n");
+        PQclear(resSectors);
+        exit(EXIT_FAILURE);
+    }
+    
+    rankTotalTuples = 0;
+    for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
+    {
+        rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
+    }
+
+    rankStartTime = time(0);
+    for (iSector = 0; iSector <= PQntuples(resSectors); iSector++)
+    {
+        if (iSector > 0)
+        {
+            resPlaces = PQgetResult(conn);
+            if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
+            {
+                fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
+                PQclear(resPlaces);
+                exit(EXIT_FAILURE);
+            }
+            if (PQftype(resPlaces, 0) != PG_OID_INT8)
+            {
+                fprintf(stderr, "Place_id value has unexpected type\n");
+                PQclear(resPlaces);
+                exit(EXIT_FAILURE);
+            }
+            resNULL = PQgetResult(conn);
+            if (resNULL != NULL)
+            {
+                fprintf(stderr, "Unexpected non-null response\n");
+                exit(EXIT_FAILURE);
+            }
+        }
+
+        if (iSector < PQntuples(resSectors))
+        {
+            sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
+//                fprintf(stderr, "\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
+
+            // Get all the place_id's for this sector
+            paramRank = PGint32(rank);
+            paramSector = PGint32(sector);
+            if (rankTotalTuples-rankCountTuples < num_threads*1000)
+            {
+                // no sectors
+                if (interpolation)
+                {
+                    iResult = PQsendQueryPrepared(conn, "index_nosector_places_osmline", 0, NULL, 0, NULL, 1);
+                }
+                else
+                {
+                    paramValues[0] = (char *)&paramRank;
+                    paramLengths[0] = sizeof(paramRank);
+                    paramFormats[0] = 1;
+                    iResult = PQsendQueryPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
+                }
+            }
+            else
+            {
+                if (interpolation)
+                {
+                    iResult = PQsendQueryPrepared(conn, "index_sector_places_osmline", 1, paramValues, paramLengths, paramFormats, 1);
+                    paramValues[0] = (char *)&paramSector;
+                    paramLengths[0] = sizeof(paramSector);
+                    paramFormats[0] = 1;
+                }
+                else
+                {
+                    paramValues[0] = (char *)&paramRank;
+                    paramLengths[0] = sizeof(paramRank);
+                    paramFormats[0] = 1;
+                    paramValues[1] = (char *)&paramSector;
+                    paramLengths[1] = sizeof(paramSector);
+                    paramFormats[1] = 1;
+                    iResult = PQsendQueryPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
+                }
+            }
+            if (!iResult)
+            {
+                fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
+                PQclear(resPlaces);
+                exit(EXIT_FAILURE);
+            }
+        }
+        if (iSector > 0)
+        {
+            count = 0;
+            rankPerSecond = 0;
+            tuples = PQntuples(resPlaces);
+
+            if (tuples > 0)
+            {
+                // Spawn threads
+                for (i = 0; i < num_threads; i++)
+                {
+                    thread_data[i].res = resPlaces;
+                    thread_data[i].tuples = tuples;
+                    thread_data[i].count = &count;
+                    thread_data[i].count_mutex = &count_mutex;
+                    thread_data[i].writer = writer;
+                    thread_data[i].writer_mutex = &writer_mutex;
+                    if (interpolation)
+                    {
+                        thread_data[i].table = 0;  // use interpolations table
+                    }
+                    else
+                    {
+                        thread_data[i].table = 1;  // use placex table
+                    }
+                    pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
+                }
+
+                // Monitor threads to give user feedback
+                sleepcount = 0;
+                while (count < tuples)
+                {
+                    usleep(1000);
+
+                    // Aim for one update per second
+                    if (sleepcount++ > 500)
+                    {
+                        rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
+                        if(interpolation)
+                        {
+                            fprintf(stderr, "  Done %i in %i @ %f per second - Interpolation lines ETA (seconds): %f\n", (rankCountTuples + count), (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - (rankCountTuples + count)))/rankPerSecond);
+                        }
+                        else
+                        {
+                            fprintf(stderr, "  Done %i in %i @ %f per second - Rank %i ETA (seconds): %f\n", (rankCountTuples + count), (int)(difftime(time(0), rankStartTime)), rankPerSecond, rank, ((float)(rankTotalTuples - (rankCountTuples + count)))/rankPerSecond);
+                        }
+                        
+                        sleepcount = 0;
+                    }
+                }
+
+                // Wait for everything to finish
+                for (i = 0; i < num_threads; i++)
+                {
+                    pthread_join(thread_data[i].thread, NULL);
+                }
+
+                rankCountTuples += tuples;
+            }
+
+            // Finished sector
+            rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
+            fprintf(stderr, "  Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
+
+            PQclear(resPlaces);
+        }
+        if (rankTotalTuples-rankCountTuples < num_threads*20 && iSector < PQntuples(resSectors))
+        {
+            iSector = PQntuples(resSectors) - 1;
+        }
+    }
+    // Finished rank
+    fprintf(stderr, "\r  Done %i in %i @ %f per second - FINISHED\n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
+
+    PQclear(resSectors);
+}
+
+void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile)
+{
+    struct index_thread_data * thread_data;
+
+    PGconn *conn;
+    PGresult * res;
+
+    int rank;
+    
+    int i;
 
     xmlTextWriterPtr writer;
     pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -70,6 +293,16 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
         exit(EXIT_FAILURE);
     }
     PQclear(res);
+    
+    res = PQprepare(conn, "index_sectors_osmline",
+                    "select geometry_sector,count(*) from location_property_osmline where indexed_status > 0 group by geometry_sector order by geometry_sector",
+                    0, NULL);
+    if (PQresultStatus(res) != PGRES_COMMAND_OK)
+    {
+        fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
+        exit(EXIT_FAILURE);
+    }
+    PQclear(res);
 
     pg_prepare_params[0] = PG_OID_INT4;
     res = PQprepare(conn, "index_nosectors",
@@ -104,7 +337,28 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
         exit(EXIT_FAILURE);
     }
     PQclear(res);
-
+    
+    pg_prepare_params[0] = PG_OID_INT4;
+    res = PQprepare(conn, "index_sector_places_osmline",
+                    "select place_id from location_property_osmline where geometry_sector = $1 and indexed_status > 0",
+                    1, pg_prepare_params);
+    if (PQresultStatus(res) != PGRES_COMMAND_OK)
+    {
+        fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
+        exit(EXIT_FAILURE);
+    }
+    PQclear(res);
+    
+    res = PQprepare(conn, "index_nosector_places_osmline",
+                    "select place_id from location_property_osmline where indexed_status > 0 order by geometry_sector",
+                    0, NULL);
+    if (PQresultStatus(res) != PGRES_COMMAND_OK)
+    {
+        fprintf(stderr, "Failed preparing index_nosector_places: %s\n", PQerrorMessage(conn));
+        exit(EXIT_FAILURE);
+    }
+    PQclear(res);
+    
     // Build the data for each thread
     thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
     for (i = 0; i < num_threads; i++)
@@ -126,6 +380,17 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
             exit(EXIT_FAILURE);
         }
         PQclear(res);
+        
+        pg_prepare_params[0] = PG_OID_INT8;
+        res = PQprepare(thread_data[i].conn, "index_osmline",
+                        "update location_property_osmline set indexed_status = 0 where place_id = $1",
+                        1, pg_prepare_params);
+        if (PQresultStatus(res) != PGRES_COMMAND_OK)
+        {
+            fprintf(stderr, "Failed preparing index_osmline: %s\n", PQerrorMessage(conn));
+            exit(EXIT_FAILURE);
+        }
+        PQclear(res);
 
         /*res = PQexec(thread_data[i].conn, "set enable_seqscan = false");
         if (PQresultStatus(res) != PGRES_COMMAND_OK)
@@ -138,183 +403,25 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
         nominatim_exportCreatePreparedQueries(thread_data[i].conn);
     }
 
-    // Create the output file
-    writer = NULL;
-    if (structuredoutputfile)
-    {
-        writer = nominatim_exportXMLStart(structuredoutputfile);
-    }
 
     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++)
     {
-        fprintf(stderr, "Starting rank %d\n", rank);
-        rankCountTuples = 0;
-        rankPerSecond = 0;
-
-        paramRank = PGint32(rank);
-        paramValues[0] = (char *)&paramRank;
-        paramLengths[0] = sizeof(paramRank);
-        paramFormats[0] = 1;
-//        if (rank < 16)
-//            resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
-//        else
-        resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
-
-        if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
-        {
-            fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
-            PQclear(resSectors);
-            exit(EXIT_FAILURE);
-        }
-        if (PQftype(resSectors, 0) != PG_OID_INT4)
-        {
-            fprintf(stderr, "Sector value has unexpected type\n");
-            PQclear(resSectors);
-            exit(EXIT_FAILURE);
-        }
-        if (PQftype(resSectors, 1) != PG_OID_INT8)
+        // OSMLINE: do reindexing (=> reparenting) for interpolation lines at rank 30, but before all other objects of rank 30
+        // reason: houses (rank 30) depend on the updated interpolation line, when reparenting (see placex_update in functions.sql)
+        if (rank == 30)
         {
-            fprintf(stderr, "Sector value has unexpected type\n");
-            PQclear(resSectors);
-            exit(EXIT_FAILURE);
-        }
-
-        rankTotalTuples = 0;
-        for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
-        {
-            rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
+            run_indexing(rank, 1, conn, num_threads, thread_data, structuredoutputfile);
         }
-
-        rankStartTime = time(0);
-        for (iSector = 0; iSector <= PQntuples(resSectors); iSector++)
-        {
-            if (iSector > 0)
-            {
-                resPlaces = PQgetResult(conn);
-                if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
-                {
-                    fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
-                    PQclear(resPlaces);
-                    exit(EXIT_FAILURE);
-                }
-                if (PQftype(resPlaces, 0) != PG_OID_INT8)
-                {
-                    fprintf(stderr, "Place_id value has unexpected type\n");
-                    PQclear(resPlaces);
-                    exit(EXIT_FAILURE);
-                }
-                resNULL = PQgetResult(conn);
-                if (resNULL != NULL)
-                {
-                    fprintf(stderr, "Unexpected non-null response\n");
-                    exit(EXIT_FAILURE);
-                }
-            }
-
-            if (iSector < PQntuples(resSectors))
-            {
-                sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
-//                fprintf(stderr, "\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
-
-                // Get all the place_id's for this sector
-                paramRank = PGint32(rank);
-                paramValues[0] = (char *)&paramRank;
-                paramLengths[0] = sizeof(paramRank);
-                paramFormats[0] = 1;
-                paramSector = PGint32(sector);
-                paramValues[1] = (char *)&paramSector;
-                paramLengths[1] = sizeof(paramSector);
-                paramFormats[1] = 1;
-                if (rankTotalTuples-rankCountTuples < num_threads*1000)
-               {
-                    iResult = PQsendQueryPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
-               }
-                else
-               {
-                    iResult = PQsendQueryPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
-               }
-                if (!iResult)
-                {
-                    fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
-                    PQclear(resPlaces);
-                    exit(EXIT_FAILURE);
-                }
-            }
-
-            if (iSector > 0)
-            {
-                count = 0;
-                rankPerSecond = 0;
-                tuples = PQntuples(resPlaces);
-
-                if (tuples > 0)
-                {
-                    // Spawn threads
-                    for (i = 0; i < num_threads; i++)
-                    {
-                        thread_data[i].res = resPlaces;
-                        thread_data[i].tuples = tuples;
-                        thread_data[i].count = &count;
-                        thread_data[i].count_mutex = &count_mutex;
-                        thread_data[i].writer = writer;
-                        thread_data[i].writer_mutex = &writer_mutex;
-                        pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
-                    }
-
-                    // Monitor threads to give user feedback
-                    sleepcount = 0;
-                    while (count < tuples)
-                    {
-                        usleep(1000);
-
-                        // Aim for one update per second
-                        if (sleepcount++ > 500)
-                        {
-                            rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
-                            fprintf(stderr, "  Done %i in %i @ %f per second - Rank %i ETA (seconds): %f\n", (rankCountTuples + count), (int)(difftime(time(0), rankStartTime)), rankPerSecond, rank, ((float)(rankTotalTuples - (rankCountTuples + count)))/rankPerSecond);
-                            sleepcount = 0;
-                        }
-                    }
-
-                    // Wait for everything to finish
-                    for (i = 0; i < num_threads; i++)
-                    {
-                        pthread_join(thread_data[i].thread, NULL);
-                    }
-
-                    rankCountTuples += tuples;
-                }
-
-                // Finished sector
-                rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
-                fprintf(stderr, "  Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
-
-                PQclear(resPlaces);
-            }
-            if (rankTotalTuples-rankCountTuples < num_threads*20 && iSector < PQntuples(resSectors))
-            {
-                iSector = PQntuples(resSectors) - 1;
-            }
-        }
-        // Finished rank
-        fprintf(stderr, "\r  Done %i in %i @ %f per second - FINISHED                      \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
-
-        PQclear(resSectors);
-    }
-
-    if (writer)
-    {
-        nominatim_exportXMLEnd(writer);
-    }
-
-    // Close all connections
-    for (i = 0; i < num_threads; i++)
-    {
-        PQfinish(thread_data[i].conn);
+        run_indexing(rank, 0, conn, num_threads, thread_data, structuredoutputfile);
     }
-    PQfinish(conn);
+       // Close all connections
+       for (i = 0; i < num_threads; i++)
+       {
+               PQfinish(thread_data[i].conn);
+       }
+       PQfinish(conn);
 }
 
 void *nominatim_indexThread(void * thread_data_in)
@@ -324,12 +431,15 @@ void *nominatim_indexThread(void * thread_data_in)
 
     PGresult   *res;
 
-    const char *paramValues[1];
+    const char  *paramValues[1];
     int         paramLengths[1];
     int         paramFormats[1];
     uint64_t    paramPlaceID;
-    uint64_t   place_id;
-    time_t             updateStartTime;
+    uint64_t    place_id;
+    time_t      updateStartTime;
+    uint        table;
+    
+    table = (uint)(thread_data->table);
 
     while (1)
     {
@@ -348,37 +458,58 @@ void *nominatim_indexThread(void * thread_data_in)
         if (verbose) fprintf(stderr, "  Processing place_id %ld\n", place_id);
 
         updateStartTime = time(0);
-       int done = 0;
+        int done = 0;
 
         if (thread_data->writer)
         {
              nominatim_exportPlaceQueries(place_id, thread_data->conn, &querySet);
         }
 
-       while(!done)
-       {
-               paramPlaceID = PGint64(place_id);
-               paramValues[0] = (char *)&paramPlaceID;
-               paramLengths[0] = sizeof(paramPlaceID);
-               paramFormats[0] = 1;
-               res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
-               if (PQresultStatus(res) == PGRES_COMMAND_OK)
-                       done = 1;
-               else
-               {
-                       if (!strncmp(PQerrorMessage(thread_data->conn), "ERROR:  deadlock detected", 25))
-                       {
-                           fprintf(stderr, "index_placex: UPDATE failed - deadlock, retrying (%ld)\n", place_id);
-                           PQclear(res);
-                            sleep(rand() % 10);
-                       }
-                       else
-                       {
-                           fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
-                           PQclear(res);
-                           exit(EXIT_FAILURE);
-                       }
-               }
+        while(!done)
+        {
+            paramPlaceID = PGint64(place_id);
+            paramValues[0] = (char *)&paramPlaceID;
+            paramLengths[0] = sizeof(paramPlaceID);
+            paramFormats[0] = 1;
+            if (table == 1) // table=1 for placex
+            {
+                res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
+            }
+            else // table=0 for osmline
+            {
+                res = PQexecPrepared(thread_data->conn, "index_osmline", 1, paramValues, paramLengths, paramFormats, 1);
+            }
+            if (PQresultStatus(res) == PGRES_COMMAND_OK)
+                done = 1;
+            else
+            {
+                if (!strncmp(PQerrorMessage(thread_data->conn), "ERROR:  deadlock detected", 25))
+                {
+                    if (table == 1)
+                    {
+                        fprintf(stderr, "index_placex: UPDATE failed - deadlock, retrying (%ld)\n", place_id);
+                    }
+                    else
+                    {
+                        fprintf(stderr, "index_osmline: UPDATE failed - deadlock, retrying (%ld)\n", place_id);
+                    }
+                    PQclear(res);
+                    sleep(rand() % 10);
+                }
+                else
+                {
+                    if (table == 1)
+                    {
+                        fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
+                    }
+                    else
+                    {
+                        fprintf(stderr, "index_osmline: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
+                    }
+                    PQclear(res);
+                    exit(EXIT_FAILURE);
+                }
+            }
         }
         PQclear(res);
         if (difftime(time(0), updateStartTime) > 1) fprintf(stderr, "  Slow place_id %ld\n", place_id);
index 1b698eed749dbe6650d4f190a50381a21c0bb932..46c8bb8a2178f260302ac07d8e9799443a6f50f6 100644 (file)
@@ -14,6 +14,7 @@ struct index_thread_data
     pthread_mutex_t * count_mutex;
     xmlTextWriterPtr writer;
     pthread_mutex_t * writer_mutex;
+    uint table;
 };
 void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile);
 void *nominatim_indexThread(void * thread_data_in);
index 4256490ebcf967a80d82bd16550252acebbfc64e..a5200973af3f1a7d49e597eba606f7298c32d18c 100644 (file)
@@ -579,8 +579,7 @@ $$
 LANGUAGE plpgsql;
 
 
-
--- find the parant road of an interpolation
+-- find the parent road of the cut road parts
 CREATE OR REPLACE FUNCTION get_interpolation_parent(wayid BIGINT, street TEXT, place TEXT,
                                                     partition INTEGER, centroid GEOMETRY, geom GEOMETRY)
 RETURNS BIGINT AS $$
@@ -647,11 +646,12 @@ END;
 $$
 LANGUAGE plpgsql;
 
-CREATE OR REPLACE FUNCTION create_interpolation(wayid BIGINT, interpolationtype TEXT,
-                                                parent_id BIGINT, partition INTEGER,
-                                                country_code TEXT,  geometry_sector INTEGER,
-                                                defpostalcode TEXT, geom GEOMETRY) RETURNS INTEGER
-  AS $$
+
+CREATE OR REPLACE FUNCTION insert_osmline(wayid BIGINT, interpolationtype TEXT,
+                                                street TEXT, addr_place TEXT, 
+                                                defpostalcode TEXT, country_code TEXT,
+                                                geom GEOMETRY)
+RETURNS INTEGER AS $$
 DECLARE
 
   newpoints INTEGER;
@@ -661,26 +661,24 @@ DECLARE
   nextnode RECORD;
   startnumber INTEGER;
   endnumber INTEGER;
-  stepsize INTEGER;
-  orginalstartnumber INTEGER;
-  originalnumberrange INTEGER;
   housenum INTEGER;
   linegeo GEOMETRY;
   splitline GEOMETRY;
   sectiongeo GEOMETRY;
   pointgeo GEOMETRY;
+  place_centroid GEOMETRY;
+  calculated_country_code VARCHAR(2);
+  partition INTEGER;
+  geometry_sector INTEGER;
 
 BEGIN
-  delete from placex where osm_type = 'W' and osm_id = wayid
-                                          and class = 'place' and type = 'address';
-
-  IF interpolationtype = 'odd' OR interpolationtype = 'even' THEN
-    stepsize := 2;
-  ELSEIF interpolationtype = 'all' THEN
-    stepsize := 1;
-  ELSEIF interpolationtype ~ '^\d+$' THEN
-    stepsize := interpolationtype::INTEGER;
-  ELSE
+  place_centroid := ST_PointOnSurface(geom);
+  calculated_country_code := lower(get_country_code(place_centroid));
+  partition := get_partition(calculated_country_code);
+  geometry_sector := geometry_sector(partition, place_centroid);
+
+  IF interpolationtype != 'odd' AND interpolationtype != 'even' AND interpolationtype!='all' THEN
+    -- other interpolation types than odd/even/all (e.g. numeric ones) are not supported
     RETURN 0;
   END IF;
 
@@ -692,7 +690,6 @@ BEGIN
 
   linegeo := geom;
   startnumber := NULL;
-  newpoints := 0;
 
   FOR nodeidpos in 1..array_upper(waynodes, 1) LOOP
 
@@ -703,82 +700,62 @@ BEGIN
     select * from placex where osm_type = 'N' and osm_id = waynodes[nodeidpos]::BIGINT
                                and indexed_status < 100 and housenumber is not NULL
                          order by (type = 'address'),place_id limit 1 INTO nextnode;
+    --RAISE NOTICE 'Nextnode.place_id: %s', nextnode.place_id;
     IF nextnode.place_id IS NOT NULL THEN
+      --RAISE NOTICE 'place_id is not null';
+      IF nodeidpos > 1 and nodeidpos < array_upper(waynodes, 1) THEN
+        -- Make sure that the point is actually on the line. That might
+        -- be a bit paranoid but ensures that the algorithm still works
+        -- should osm2pgsql attempt to repair geometries.
+        splitline := split_line_on_node(linegeo, nextnode.geometry);
+        sectiongeo := ST_GeometryN(splitline, 1);
+        linegeo := ST_GeometryN(splitline, 2);
+      ELSE
+        sectiongeo = linegeo;
+      END IF;
+      endnumber := substring(nextnode.housenumber,'[0-9]+')::integer;
 
-        IF nodeidpos > 1 and nodeidpos < array_upper(waynodes, 1) THEN
-          -- Make sure that the point is actually on the line. That might
-          -- be a bit paranoid but ensures that the algorithm still works
-          -- should osm2pgsql attempt to repair geometries.
-          splitline := split_line_on_node(linegeo, nextnode.geometry);
-          sectiongeo := ST_GeometryN(splitline, 1);
-          linegeo := ST_GeometryN(splitline, 2);
-        ELSE
-          sectiongeo = linegeo;
-        END IF;
-        endnumber := substring(nextnode.housenumber,'[0-9]+')::integer;
-
-        IF startnumber IS NOT NULL AND endnumber IS NOT NULL
-           AND @(startnumber - endnumber) < 1000 AND startnumber != endnumber
-           AND ST_GeometryType(sectiongeo) = 'ST_LineString' THEN
+      IF startnumber IS NOT NULL AND endnumber IS NOT NULL
+         AND @(startnumber - endnumber) < 1000 AND startnumber != endnumber
+         AND ST_GeometryType(sectiongeo) = 'ST_LineString' THEN
 
-          IF (startnumber > endnumber) THEN
-            housenum := endnumber;
-            endnumber := startnumber;
-            startnumber := housenum;
-            sectiongeo := ST_Reverse(sectiongeo);
-          END IF;
-          orginalstartnumber := startnumber;
-          originalnumberrange := endnumber - startnumber;
-
-          startnumber := startnumber + stepsize;
-          -- correct for odd/even
-          IF (interpolationtype = 'odd' AND startnumber%2 = 0)
-             OR (interpolationtype = 'even' AND startnumber%2 = 1) THEN
-            startnumber := startnumber - 1;
-          END IF;
-          endnumber := endnumber - 1;
-
-          -- keep for compatibility with previous versions
-          delete from placex where osm_type = 'N' and osm_id = prevnode.osm_id
-                               and place_id != prevnode.place_id and class = 'place'
-                               and type = 'house';
-          FOR housenum IN startnumber..endnumber BY stepsize LOOP
-            pointgeo := ST_LineInterpolatePoint(sectiongeo, (housenum::float-orginalstartnumber::float)/originalnumberrange::float);
-            insert into placex (place_id, partition, osm_type, osm_id,
-                                class, type, admin_level, housenumber,
-                                postcode,
-                                country_code, parent_place_id, rank_address, rank_search,
-                                indexed_status, indexed_date, geometry_sector,
-                                calculated_country_code, centroid, geometry)
-              values (nextval('seq_place'), partition, 'W', wayid,
-                      'place', 'address', prevnode.admin_level, housenum,
-                      coalesce(prevnode.postcode, defpostalcode),
-                      prevnode.country_code, parent_id, 30, 30,
-                      0, now(), geometry_sector, country_code,
-                      pointgeo, pointgeo);
-            newpoints := newpoints + 1;
---RAISE WARNING 'interpolation number % % ',prevnode.place_id,housenum;
-          END LOOP;
+        IF (startnumber > endnumber) THEN
+          housenum := endnumber;
+          endnumber := startnumber;
+          startnumber := housenum;
+          sectiongeo := ST_Reverse(sectiongeo);
         END IF;
+        
+        -- a correction for odd/even is NOT necessary anymore (e.g. if interpolationtype=even, but start/endnumber != even)
+        -- see PlaceLookup.php
+
+        -- keep for compatibility with previous versions
+        delete from placex where osm_type = 'N' and osm_id = prevnode.osm_id
+                             and place_id != prevnode.place_id and class = 'place'
+                             and type = 'house';
+        insert into location_property_osmline
+          values (sectiongeo, nextval('seq_place'), partition, wayid, NULL, startnumber, endnumber, 
+          interpolationtype, street, coalesce(prevnode.postcode, defpostalcode),
+          calculated_country_code, geometry_sector, 2, now());
+      END IF;
 
-        -- early break if we are out of line string,
-        -- might happen when a line string loops back on itself
-        IF ST_GeometryType(linegeo) != 'ST_LineString' THEN
-            RETURN newpoints;
-        END IF;
+      -- early break if we are out of line string,
+      -- might happen when a line string loops back on itself
+      IF ST_GeometryType(linegeo) != 'ST_LineString' THEN
+          RETURN 0;
+      END IF;
 
-        startnumber := substring(nextnode.housenumber,'[0-9]+')::integer;
-        prevnode := nextnode;
+      startnumber := substring(nextnode.housenumber,'[0-9]+')::integer;
+      prevnode := nextnode;
     END IF;
   END LOOP;
 
---RAISE WARNING 'interpolation points % ',newpoints;
-
-  RETURN newpoints;
+  RETURN 1;
 END;
 $$
 LANGUAGE plpgsql;
 
+
 CREATE OR REPLACE FUNCTION placex_insert() RETURNS TRIGGER
   AS $$
 DECLARE
@@ -789,10 +766,11 @@ DECLARE
   default_language VARCHAR(10);
   diameter FLOAT;
   classtable TEXT;
+  line RECORD;
 BEGIN
   --DEBUG: RAISE WARNING '% %',NEW.osm_type,NEW.osm_id;
 
-  -- ignore interpolated addresses
+  -- ignore interpolated addresses, not necessary anymore, cause interpolated addresses are now in location_property_osmline
   IF NEW.class = 'place' and NEW.type = 'address' THEN
     RETURN NEW;
   END IF;
@@ -1065,6 +1043,8 @@ BEGIN
         IF NEW.rank_search >= 26 THEN
           -- roads may cause reparenting for >27 rank places
           update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter);
+          -- reparenting also for OSM Interpolation Lines (and for Tiger?)
+          update location_property_osmline set indexed_status = 2 where indexed_status = 0 and ST_DWithin(location_property_osmline.linegeo, NEW.geometry, diameter);
         ELSEIF NEW.rank_search >= 16 THEN
           -- up to rank 16, street-less addresses may need reparenting
           update placex set indexed_status = 2 where indexed_status = 0 and rank_search > NEW.rank_search and ST_DWithin(placex.geometry, NEW.geometry, diameter) and (rank_search < 28 or name is not null or addr_place is not null);
@@ -1093,7 +1073,37 @@ END;
 $$
 LANGUAGE plpgsql;
 
-CREATE OR REPLACE FUNCTION placex_update() RETURNS 
+CREATE OR REPLACE FUNCTION osmline_update() RETURNS 
+TRIGGER
+  AS $$
+DECLARE
+  place_centroid GEOMETRY;
+BEGIN
+  -- deferred delete
+  IF OLD.indexed_status = 100 THEN
+    delete from location_property_osmline where place_id = OLD.place_id;
+    RETURN NULL;
+  END IF;
+  
+  IF NEW.indexed_status != 0 OR OLD.indexed_status = 0 THEN
+    RETURN NEW;
+  END IF;
+  
+  IF OLD.indexed_status = 2 and NEW.indexed_status=0 THEN
+    -- do the reparenting: (finally here, because ALL places in placex, that are needed for reparenting, need to be up to date)
+    -- (the osm interpolationline in location_property_osmline was marked for reparenting in placex_insert/placex_delete with index_status = 2 
+    -- => index.c: sets index_status back to 0
+    -- => triggers this function)
+    place_centroid := ST_PointOnSurface(NEW.linegeo);
+    -- marking descendants for reparenting is not needed, because there are actually no descendants for interpolation lines
+    NEW.parent_place_id = get_interpolation_parent(NEW.osm_id, NEW.street, null, NEW.partition, place_centroid, NEW.linegeo); -- addr_place (3rd param) is not necessarily needed
+    return NEW;
+  END IF;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION placex_update() RETURNS
 TRIGGER
   AS $$
 DECLARE
@@ -1137,7 +1147,6 @@ DECLARE
 
   result BOOLEAN;
 BEGIN
-
   -- deferred delete
   IF OLD.indexed_status = 100 THEN
     --DEBUG: RAISE WARNING 'placex_update_delete % %',NEW.osm_type,NEW.osm_id;
@@ -1164,649 +1173,637 @@ BEGIN
     RETURN NEW;
   END IF;
 
-  -- TODO: this test is now redundant?
-  IF OLD.indexed_status != 0 THEN
+  NEW.indexed_date = now();
 
-    NEW.indexed_date = now();
+  result := deleteSearchName(NEW.partition, NEW.place_id);
+  DELETE FROM place_addressline WHERE place_id = NEW.place_id;
+  result := deleteRoad(NEW.partition, NEW.place_id);
+  result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
+  UPDATE placex set linked_place_id = null, indexed_status = 2
+         where linked_place_id = NEW.place_id;
+  -- update not necessary for osmline, cause linked_place_id does not exist
 
-    result := deleteSearchName(NEW.partition, NEW.place_id);
-    DELETE FROM place_addressline WHERE place_id = NEW.place_id;
-    result := deleteRoad(NEW.partition, NEW.place_id);
-    result := deleteLocationArea(NEW.partition, NEW.place_id, NEW.rank_search);
-    UPDATE placex set linked_place_id = null, indexed_status = 2
-           where linked_place_id = NEW.place_id;
+  IF NEW.linked_place_id is not null THEN
+    RETURN NEW;
+  END IF;
 
-    IF NEW.linked_place_id is not null THEN
-      RETURN NEW;
-    END IF;
+  -- Speed up searches - just use the centroid of the feature
+  -- cheaper but less acurate
+  place_centroid := ST_PointOnSurface(NEW.geometry);
+  NEW.centroid := null;
 
-    -- Speed up searches - just use the centroid of the feature
-    -- cheaper but less acurate
-    place_centroid := ST_PointOnSurface(NEW.geometry);
-    NEW.centroid := null;
-
-    -- recalculate country and partition
-    IF NEW.rank_search = 4 THEN
-      -- for countries, believe the mapped country code,
-      -- so that we remain in the right partition if the boundaries
-      -- suddenly expand.
-      NEW.partition := get_partition(lower(NEW.country_code));
-      IF NEW.partition = 0 THEN
-        NEW.calculated_country_code := lower(get_country_code(place_centroid));
-        NEW.partition := get_partition(NEW.calculated_country_code);
-      ELSE
-        NEW.calculated_country_code := lower(NEW.country_code);
-      END IF;
-    ELSE
-      IF NEW.rank_search > 4 THEN
-        --NEW.calculated_country_code := lower(get_country_code(NEW.geometry, NEW.country_code));
-        NEW.calculated_country_code := lower(get_country_code(place_centroid));
-      ELSE
-        NEW.calculated_country_code := NULL;
-      END IF;
+  -- recalculate country and partition
+  IF NEW.rank_search = 4 THEN
+    -- for countries, believe the mapped country code,
+    -- so that we remain in the right partition if the boundaries
+    -- suddenly expand.
+    NEW.partition := get_partition(lower(NEW.country_code));
+    IF NEW.partition = 0 THEN
+      NEW.calculated_country_code := lower(get_country_code(place_centroid));
       NEW.partition := get_partition(NEW.calculated_country_code);
+    ELSE
+      NEW.calculated_country_code := lower(NEW.country_code);
     END IF;
-    NEW.geometry_sector := geometry_sector(NEW.partition, place_centroid);
-
-    -- interpolations
-    IF NEW.class = 'place' AND NEW.type = 'houses'THEN
-      IF NEW.osm_type = 'W' and ST_GeometryType(NEW.geometry) = 'ST_LineString' THEN
-        NEW.parent_place_id := get_interpolation_parent(NEW.osm_id, NEW.street, NEW.addr_place,
-                                                        NEW.partition, place_centroid, NEW.geometry);
-        i := create_interpolation(NEW.osm_id, NEW.housenumber, NEW.parent_place_id,
-                                  NEW.partition, NEW.calculated_country_code,
-                                  NEW.geometry_sector, NEW.postcode, NEW.geometry);
-      END IF;
-      RETURN NEW;
-    END IF;
-
-    -- waterway ways are linked when they are part of a relation and have the same class/type
-    IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
-        FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
-        LOOP
-            FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
-                IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
-                  --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation.members[i];
-                  FOR linked_node_id IN SELECT place_id FROM placex
-                    WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
-                    and class = NEW.class and type = NEW.type
-                    and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
-                  LOOP
-                    UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
-                  END LOOP;
-                END IF;
-            END LOOP;
-        END LOOP;
-    END IF;
+  ELSE
+    IF NEW.rank_search > 4 THEN
+      --NEW.calculated_country_code := lower(get_country_code(NEW.geometry, NEW.country_code));
+      NEW.calculated_country_code := lower(get_country_code(place_centroid));
+    ELSE
+      NEW.calculated_country_code := NULL;
+    END IF;
+    NEW.partition := get_partition(NEW.calculated_country_code);
+  END IF;
+
+  -- waterway ways are linked when they are part of a relation and have the same class/type
+  IF NEW.osm_type = 'R' and NEW.class = 'waterway' THEN
+      FOR relation_members IN select members from planet_osm_rels r where r.id = NEW.osm_id and r.parts != array[]::bigint[]
+      LOOP
+          FOR i IN 1..array_upper(relation_members, 1) BY 2 LOOP
+              IF relation_members[i+1] in ('', 'main_stream', 'side_stream') AND substring(relation_members[i],1,1) = 'w' THEN
+                --DEBUG: RAISE WARNING 'waterway parent %, child %/%', NEW.osm_id, i, relation.members[i];
+                FOR linked_node_id IN SELECT place_id FROM placex
+                  WHERE osm_type = 'W' and osm_id = substring(relation_members[i],2,200)::bigint
+                  and class = NEW.class and type = NEW.type
+                  and ( relation_members[i+1] != 'side_stream' or NEW.name->'name' = name->'name')
+                LOOP
+                  UPDATE placex SET linked_place_id = NEW.place_id WHERE place_id = linked_node_id;
+                END LOOP;
+              END IF;
+          END LOOP;
+      END LOOP;
+  END IF;
 
-    -- Adding ourselves to the list simplifies address calculations later
-    INSERT INTO place_addressline VALUES (NEW.place_id, NEW.place_id, true, true, 0, NEW.rank_address); 
+  -- Adding ourselves to the list simplifies address calculations later
+  INSERT INTO place_addressline VALUES (NEW.place_id, NEW.place_id, true, true, 0, NEW.rank_address); 
 
-    -- What level are we searching from
-    search_maxrank := NEW.rank_search;
+  -- What level are we searching from
+  search_maxrank := NEW.rank_search;
 
-    -- Thought this wasn't needed but when we add new languages to the country_name table
-    -- we need to update the existing names
-    IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
-      default_language := get_country_language_code(NEW.calculated_country_code);
-      IF default_language IS NOT NULL THEN
-        IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
-          NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
-        ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
-          NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
-        END IF;
+  -- Thought this wasn't needed but when we add new languages to the country_name table
+  -- we need to update the existing names
+  IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
+    default_language := get_country_language_code(NEW.calculated_country_code);
+    IF default_language IS NOT NULL THEN
+      IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
+        NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
+      ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
+        NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
       END IF;
     END IF;
+  END IF;
 
-    -- Initialise the name vector using our name
-    name_vector := make_keywords(NEW.name);
-    nameaddress_vector := '{}'::int[];
+  -- Initialise the name vector using our name
+  name_vector := make_keywords(NEW.name);
+  nameaddress_vector := '{}'::int[];
 
-    FOR i IN 1..28 LOOP
-      address_havelevel[i] := false;
-    END LOOP;
+  FOR i IN 1..28 LOOP
+    address_havelevel[i] := false;
+  END LOOP;
 
-    NEW.importance := null;
-    select language||':'||title,importance from get_wikipedia_match(NEW.extratags, NEW.calculated_country_code) INTO NEW.wikipedia,NEW.importance;
-    IF NEW.importance IS NULL THEN
-      select language||':'||title,importance from wikipedia_article where osm_type = NEW.osm_type and osm_id = NEW.osm_id order by importance desc limit 1 INTO NEW.wikipedia,NEW.importance;
-    END IF;
+  NEW.importance := null;
+  select language||':'||title,importance from get_wikipedia_match(NEW.extratags, NEW.calculated_country_code) INTO NEW.wikipedia,NEW.importance;
+  IF NEW.importance IS NULL THEN
+    select language||':'||title,importance from wikipedia_article where osm_type = NEW.osm_type and osm_id = NEW.osm_id order by importance desc limit 1 INTO NEW.wikipedia,NEW.importance;
+  END IF;
 
 --RAISE WARNING 'before low level% %', NEW.place_id, NEW.rank_search;
 
-    -- For low level elements we inherit from our parent road
-    IF (NEW.rank_search > 27 OR (NEW.type = 'postcode' AND NEW.rank_search = 25)) THEN
+  -- For low level elements we inherit from our parent road
+  IF (NEW.rank_search > 27 OR (NEW.type = 'postcode' AND NEW.rank_search = 25)) THEN
 
 --RAISE WARNING 'finding street for %', NEW;
 
-      -- We won't get a better centroid, besides these places are too small to care
-      NEW.centroid := place_centroid;
-
-      NEW.parent_place_id := null;
-
-      -- if we have a POI and there is no address information,
-      -- see if we can get it from a surrounding building
-      IF NEW.osm_type = 'N' AND NEW.street IS NULL AND NEW.addr_place IS NULL
-         AND NEW.housenumber IS NULL THEN
-        FOR location IN select * from placex where ST_Covers(geometry, place_centroid)
-              and (housenumber is not null or street is not null or addr_place is not null)
-              and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
-              limit 1
-        LOOP
-          NEW.housenumber := location.housenumber;
-          NEW.street := location.street;
-          NEW.addr_place := location.addr_place;
-        END LOOP;
-      END IF;
+    -- We won't get a better centroid, besides these places are too small to care
+    NEW.centroid := place_centroid;
+
+    NEW.parent_place_id := null;
+
+    -- if we have a POI and there is no address information,
+    -- see if we can get it from a surrounding building
+    IF NEW.osm_type = 'N' AND NEW.street IS NULL AND NEW.addr_place IS NULL
+       AND NEW.housenumber IS NULL THEN
+      FOR location IN select * from placex where ST_Covers(geometry, place_centroid)
+            and (housenumber is not null or street is not null or addr_place is not null)
+            and rank_search > 28 AND ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon')
+            limit 1
+      LOOP
+        NEW.housenumber := location.housenumber;
+        NEW.street := location.street;
+        NEW.addr_place := location.addr_place;
+      END LOOP;
+    END IF;
 
-      -- We have to find our parent road.
-      -- Copy data from linked items (points on ways, addr:street links, relations)
+    -- We have to find our parent road.
+    -- Copy data from linked items (points on ways, addr:street links, relations)
 
-      -- Is this object part of a relation?
-        FOR relation IN select * from planet_osm_rels where parts @> ARRAY[NEW.osm_id] and members @> ARRAY[lower(NEW.osm_type)||NEW.osm_id]
-        LOOP
-          -- At the moment we only process one type of relation - associatedStreet
-          IF relation.tags @> ARRAY['associatedStreet'] THEN
-            FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
-              IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
+    -- Is this object part of a relation?
+    FOR relation IN select * from planet_osm_rels where parts @> ARRAY[NEW.osm_id] and members @> ARRAY[lower(NEW.osm_type)||NEW.osm_id]
+    LOOP
+      -- At the moment we only process one type of relation - associatedStreet
+      IF relation.tags @> ARRAY['associatedStreet'] THEN
+        FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
+          IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
 --RAISE WARNING 'node in relation %',relation;
-                SELECT place_id from placex where osm_type = 'W'
-                  and osm_id = substring(relation.members[i],2,200)::bigint
-                  and rank_search = 26 and name is not null INTO NEW.parent_place_id;
-              END IF;
-            END LOOP;
+            SELECT place_id from placex where osm_type = 'W'
+              and osm_id = substring(relation.members[i],2,200)::bigint
+              and rank_search = 26 and name is not null INTO NEW.parent_place_id;
           END IF;
         END LOOP;
+      END IF;
+    END LOOP;
 
 
-      -- Note that addr:street links can only be indexed once the street itself is indexed
-       IF NEW.parent_place_id IS NULL AND NEW.street IS NOT NULL THEN
-        address_street_word_ids := get_name_ids(make_standard_name(NEW.street));
-        IF address_street_word_ids IS NOT NULL THEN
-          FOR location IN SELECT * from getNearestNamedRoadFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
-              NEW.parent_place_id := location.place_id;
-          END LOOP;
-        END IF;
+    -- Note that addr:street links can only be indexed once the street itself is indexed
+    IF NEW.parent_place_id IS NULL AND NEW.street IS NOT NULL THEN
+      address_street_word_ids := get_name_ids(make_standard_name(NEW.street));
+      IF address_street_word_ids IS NOT NULL THEN
+        FOR location IN SELECT * from getNearestNamedRoadFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+            NEW.parent_place_id := location.place_id;
+        END LOOP;
       END IF;
+    END IF;
 
-      IF NEW.parent_place_id IS NULL AND NEW.addr_place IS NOT NULL THEN
-        address_street_word_ids := get_name_ids(make_standard_name(NEW.addr_place));
-        IF address_street_word_ids IS NOT NULL THEN
-          FOR location IN SELECT * from getNearestNamedPlaceFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
-            NEW.parent_place_id := location.place_id;
-          END LOOP;
-        END IF;
+    IF NEW.parent_place_id IS NULL AND NEW.addr_place IS NOT NULL THEN
+      address_street_word_ids := get_name_ids(make_standard_name(NEW.addr_place));
+      IF address_street_word_ids IS NOT NULL THEN
+        FOR location IN SELECT * from getNearestNamedPlaceFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+          NEW.parent_place_id := location.place_id;
+        END LOOP;
       END IF;
+    END IF;
 
-      IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
+    IF NEW.parent_place_id IS NULL AND NEW.osm_type = 'N' THEN
 
 --RAISE WARNING 'x1';
-        -- Is this node part of a way?
-        FOR location IN select p.* from placex p, planet_osm_ways w
-           where p.osm_type = 'W' and p.rank_search >= 26
-             and p.geometry && NEW.geometry and p.osm_id = w.id and NEW.osm_id = any(w.nodes)
-        LOOP
+      -- Is this node part of a way? search for the way in placex AND location_property_osmline (for interpolation lines)
+      FOR location IN select p.place_id, p.osm_id, p.parent_place_id, p.class, p.type, p.rank_search, p.street, p.addr_place from placex p, planet_osm_ways w
+         where p.osm_type = 'W' and p.rank_search >= 26 and p.geometry && NEW.geometry and w.id = p.osm_id and NEW.osm_id = any(w.nodes) 
+         UNION 
+         select q.place_id, q.osm_id, q.parent_place_id, 'place' as class, 'houses' as type, 30 as rank_search, null as street, 
+         null as addr_place from location_property_osmline q, planet_osm_ways x
+         where q.linegeo && NEW.geometry and x.id = q.osm_id and NEW.osm_id = any(x.nodes)
+      LOOP
+        
 --RAISE WARNING '%', location;
-          -- Way IS a road then we are on it - that must be our road
-          IF location.rank_search = 26 AND NEW.parent_place_id IS NULL THEN
+        -- Way IS a road then we are on it - that must be our road
+        IF location.rank_search = 26 AND NEW.parent_place_id IS NULL THEN
 --RAISE WARNING 'node in way that is a street %',location;
-            NEW.parent_place_id := location.place_id;
-          END IF;
+          NEW.parent_place_id := location.place_id;
+        END IF;
 
-          -- If this way is a street interpolation line then it is probably as good as we are going to get
-          IF NEW.parent_place_id IS NULL AND location.class = 'place' and location.type='houses' THEN
-            NEW.parent_place_id := location.parent_place_id;
-          END IF;
+        -- If this way is a street interpolation line then it is probably as good as we are going to get
+        IF NEW.parent_place_id IS NULL AND location.class = 'place' and location.type='houses' THEN
+          NEW.parent_place_id := location.parent_place_id;
+        END IF;
 
-          -- Is the WAY part of a relation
-          IF NEW.parent_place_id IS NULL THEN
-              FOR relation IN select * from planet_osm_rels where parts @> ARRAY[location.osm_id] and members @> ARRAY['w'||location.osm_id]
-              LOOP
-                -- At the moment we only process one type of relation - associatedStreet
-                IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
-                  FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
-                    IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
-    --RAISE WARNING 'node in way that is in a relation %',relation;
-                      SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::bigint 
-                        and rank_search = 26 and name is not null INTO NEW.parent_place_id;
-                    END IF;
-                  END LOOP;
-                END IF;
-              END LOOP;
-          END IF;
+        -- Is the WAY part of a relation
+        IF NEW.parent_place_id IS NULL THEN
+            FOR relation IN select * from planet_osm_rels where parts @> ARRAY[location.osm_id] and members @> ARRAY['w'||location.osm_id]
+            LOOP
+              -- At the moment we only process one type of relation - associatedStreet
+              IF relation.tags @> ARRAY['associatedStreet'] AND array_upper(relation.members, 1) IS NOT NULL THEN
+                FOR i IN 1..array_upper(relation.members, 1) BY 2 LOOP
+                  IF NEW.parent_place_id IS NULL AND relation.members[i+1] = 'street' THEN
+  --RAISE WARNING 'node in way that is in a relation %',relation;
+                    SELECT place_id from placex where osm_type='W' and osm_id = substring(relation.members[i],2,200)::bigint 
+                      and rank_search = 26 and name is not null INTO NEW.parent_place_id;
+                  END IF;
+                END LOOP;
+              END IF;
+            END LOOP;
+        END IF;
 
-          -- If the way mentions a street or place address, try that for parenting.
-          IF NEW.parent_place_id IS NULL AND location.street IS NOT NULL THEN
-            address_street_word_ids := get_name_ids(make_standard_name(location.street));
-            IF address_street_word_ids IS NOT NULL THEN
-              FOR linkedplacex IN SELECT place_id from getNearestNamedRoadFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
-                  NEW.parent_place_id := linkedplacex.place_id;
-              END LOOP;
-            END IF;
+        -- If the way mentions a street or place address, try that for parenting.
+        IF NEW.parent_place_id IS NULL AND location.street IS NOT NULL THEN
+          address_street_word_ids := get_name_ids(make_standard_name(location.street));
+          IF address_street_word_ids IS NOT NULL THEN
+            FOR linkedplacex IN SELECT place_id from getNearestNamedRoadFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+                NEW.parent_place_id := linkedplacex.place_id;
+            END LOOP;
           END IF;
+        END IF;
 
-          IF NEW.parent_place_id IS NULL AND location.addr_place IS NOT NULL THEN
-            address_street_word_ids := get_name_ids(make_standard_name(location.addr_place));
-            IF address_street_word_ids IS NOT NULL THEN
-              FOR linkedplacex IN SELECT place_id from getNearestNamedPlaceFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
-                NEW.parent_place_id := linkedplacex.place_id;
-              END LOOP;
-            END IF;
+        IF NEW.parent_place_id IS NULL AND location.addr_place IS NOT NULL THEN
+          address_street_word_ids := get_name_ids(make_standard_name(location.addr_place));
+          IF address_street_word_ids IS NOT NULL THEN
+            FOR linkedplacex IN SELECT place_id from getNearestNamedPlaceFeature(NEW.partition, place_centroid, address_street_word_ids) LOOP
+              NEW.parent_place_id := linkedplacex.place_id;
+            END LOOP;
           END IF;
+        END IF;
 
-        END LOOP;
+      END LOOP;
 
-      END IF;
+    END IF;
 
 --RAISE WARNING 'x4 %',NEW.parent_place_id;
-      -- Still nothing, just use the nearest road
-      IF NEW.parent_place_id IS NULL THEN
-        FOR location IN SELECT place_id FROM getNearestRoadFeature(NEW.partition, place_centroid) LOOP
-          NEW.parent_place_id := location.place_id;
-        END LOOP;
-      END IF;
+    -- Still nothing, just use the nearest road
+    IF NEW.parent_place_id IS NULL THEN
+      FOR location IN SELECT place_id FROM getNearestRoadFeature(NEW.partition, place_centroid) LOOP
+        NEW.parent_place_id := location.place_id;
+      END LOOP;
+    END IF;
 
 --return NEW;
 --RAISE WARNING 'x6 %',NEW.parent_place_id;
 
-      -- If we didn't find any road fallback to standard method
-      IF NEW.parent_place_id IS NOT NULL THEN
+    -- If we didn't find any road fallback to standard method
+    IF NEW.parent_place_id IS NOT NULL THEN
 
-        -- Get the details of the parent road
-        select * from search_name where place_id = NEW.parent_place_id INTO location;
-        NEW.calculated_country_code := location.country_code;
+      -- Get the details of the parent road
+      select * from search_name where place_id = NEW.parent_place_id INTO location;
+      NEW.calculated_country_code := location.country_code;
 
-        -- Merge the postcode into the parent's address if necessary XXXX
-        IF NEW.postcode IS NOT NULL THEN
-          isin_tokens := '{}'::int[];
-          address_street_word_id := getorcreate_word_id(make_standard_name(NEW.postcode));
-          IF address_street_word_id is not null
-             and not ARRAY[address_street_word_id] <@ location.nameaddress_vector THEN
-             isin_tokens := isin_tokens || address_street_word_id;
-          END IF;
-          address_street_word_id := getorcreate_name_id(make_standard_name(NEW.postcode));
-          IF address_street_word_id is not null
-             and not ARRAY[address_street_word_id] <@ location.nameaddress_vector THEN
-             isin_tokens := isin_tokens || address_street_word_id;
-          END IF;
-          IF isin_tokens != '{}'::int[] THEN
-             UPDATE search_name
-                SET nameaddress_vector = search_name.nameaddress_vector || isin_tokens
-              WHERE place_id = NEW.parent_place_id;
-          END IF;
+      -- Merge the postcode into the parent's address if necessary XXXX
+      IF NEW.postcode IS NOT NULL THEN
+        isin_tokens := '{}'::int[];
+        address_street_word_id := getorcreate_word_id(make_standard_name(NEW.postcode));
+        IF address_street_word_id is not null
+           and not ARRAY[address_street_word_id] <@ location.nameaddress_vector THEN
+           isin_tokens := isin_tokens || address_street_word_id;
         END IF;
-
---RAISE WARNING '%', NEW.name;
-        -- If there is no name it isn't searchable, don't bother to create a search record
-        IF NEW.name is NULL THEN
-          return NEW;
+        address_street_word_id := getorcreate_name_id(make_standard_name(NEW.postcode));
+        IF address_street_word_id is not null
+           and not ARRAY[address_street_word_id] <@ location.nameaddress_vector THEN
+           isin_tokens := isin_tokens || address_street_word_id;
         END IF;
+        IF isin_tokens != '{}'::int[] THEN
+           UPDATE search_name
+              SET nameaddress_vector = search_name.nameaddress_vector || isin_tokens
+            WHERE place_id = NEW.parent_place_id;
+        END IF;
+      END IF;
 
-        -- Merge address from parent
-        nameaddress_vector := array_merge(nameaddress_vector, location.nameaddress_vector);
-        nameaddress_vector := array_merge(nameaddress_vector, location.name_vector);
-
-        -- Performance, it would be more acurate to do all the rest of the import
-        -- process but it takes too long
-        -- Just be happy with inheriting from parent road only
+--RAISE WARNING '%', NEW.name;
+      -- If there is no name it isn't searchable, don't bother to create a search record
+      IF NEW.name is NULL THEN
+        return NEW;
+      END IF;
 
-        IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
-          result := add_location(NEW.place_id, NEW.calculated_country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
-        END IF;
+      -- Merge address from parent
+      nameaddress_vector := array_merge(nameaddress_vector, location.nameaddress_vector);
+      nameaddress_vector := array_merge(nameaddress_vector, location.name_vector);
 
-        result := insertSearchName(NEW.partition, NEW.place_id, NEW.calculated_country_code, name_vector, nameaddress_vector, NEW.rank_search, NEW.rank_address, NEW.importance, place_centroid, NEW.geometry);
+      -- Performance, it would be more acurate to do all the rest of the import
+      -- process but it takes too long
+      -- Just be happy with inheriting from parent road only
 
-        return NEW;
+      IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
+        result := add_location(NEW.place_id, NEW.calculated_country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
       END IF;
 
+      result := insertSearchName(NEW.partition, NEW.place_id, NEW.calculated_country_code, name_vector, nameaddress_vector, NEW.rank_search, NEW.rank_address, NEW.importance, place_centroid, NEW.geometry);
+
+      return NEW;
     END IF;
 
+  END IF;
+
 -- RAISE WARNING '  INDEXING Started:';
 -- RAISE WARNING '  INDEXING: %',NEW;
 
-    IF NEW.osm_type = 'R' AND NEW.rank_search < 26 THEN
+  IF NEW.osm_type = 'R' AND NEW.rank_search < 26 THEN
 
-      -- see if we have any special relation members
-      select members from planet_osm_rels where id = NEW.osm_id INTO relation_members;
+    -- see if we have any special relation members
+    select members from planet_osm_rels where id = NEW.osm_id INTO relation_members;
 
 -- RAISE WARNING 'get_osm_rel_members, label';
-      IF relation_members IS NOT NULL THEN
-        FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['label']) as member LOOP
+    IF relation_members IS NOT NULL THEN
+      FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['label']) as member LOOP
 
-          FOR linkedPlacex IN select * from placex where osm_type = upper(substring(relMember.member,1,1))::char(1) 
-            and osm_id = substring(relMember.member,2,10000)::bigint order by rank_search desc limit 1 LOOP
+        FOR linkedPlacex IN select * from placex where osm_type = upper(substring(relMember.member,1,1))::char(1) 
+          and osm_id = substring(relMember.member,2,10000)::bigint order by rank_search desc limit 1 LOOP
 
-            -- If we don't already have one use this as the centre point of the geometry
-            IF NEW.centroid IS NULL THEN
-              NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
-            END IF;
-
-            -- merge in the label name, re-init word vector
-            IF NOT linkedPlacex.name IS NULL THEN
-              NEW.name := linkedPlacex.name || NEW.name;
-              name_vector := array_merge(name_vector, make_keywords(linkedPlacex.name));
-            END IF;
+          -- If we don't already have one use this as the centre point of the geometry
+          IF NEW.centroid IS NULL THEN
+            NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
+          END IF;
 
-            -- merge in extra tags
-            NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
+          -- merge in the label name, re-init word vector
+          IF NOT linkedPlacex.name IS NULL THEN
+            NEW.name := linkedPlacex.name || NEW.name;
+            name_vector := array_merge(name_vector, make_keywords(linkedPlacex.name));
+          END IF;
 
-            -- mark the linked place (excludes from search results)
-            UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
+          -- merge in extra tags
+          NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
 
-            -- keep a note of the node id in case we need it for wikipedia in a bit
-            linked_node_id := linkedPlacex.osm_id;
-          END LOOP;
+          -- mark the linked place (excludes from search results)
+          UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
 
+          -- keep a note of the node id in case we need it for wikipedia in a bit
+          linked_node_id := linkedPlacex.osm_id;
         END LOOP;
 
-        IF NEW.centroid IS NULL THEN
+      END LOOP;
 
-          FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['admin_center','admin_centre']) as member LOOP
+      IF NEW.centroid IS NULL THEN
 
-            FOR linkedPlacex IN select * from placex where osm_type = upper(substring(relMember.member,1,1))::char(1) 
-              and osm_id = substring(relMember.member,2,10000)::bigint order by rank_search desc limit 1 LOOP
+        FOR relMember IN select get_osm_rel_members(relation_members,ARRAY['admin_center','admin_centre']) as member LOOP
 
-              -- For an admin centre we also want a name match - still not perfect, for example 'new york, new york'
-              -- But that can be fixed by explicitly setting the label in the data
-              IF make_standard_name(NEW.name->'name') = make_standard_name(linkedPlacex.name->'name') 
-                AND NEW.rank_address = linkedPlacex.rank_address THEN
+          FOR linkedPlacex IN select * from placex where osm_type = upper(substring(relMember.member,1,1))::char(1) 
+            and osm_id = substring(relMember.member,2,10000)::bigint order by rank_search desc limit 1 LOOP
 
-                -- If we don't already have one use this as the centre point of the geometry
-                IF NEW.centroid IS NULL THEN
-                  NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
-                END IF;
+            -- For an admin centre we also want a name match - still not perfect, for example 'new york, new york'
+            -- But that can be fixed by explicitly setting the label in the data
+            IF make_standard_name(NEW.name->'name') = make_standard_name(linkedPlacex.name->'name') 
+              AND NEW.rank_address = linkedPlacex.rank_address THEN
 
-                -- merge in the name, re-init word vector
-                IF NOT linkedPlacex.name IS NULL THEN
-                  NEW.name := linkedPlacex.name || NEW.name;
-                  name_vector := make_keywords(NEW.name);
-                END IF;
+              -- If we don't already have one use this as the centre point of the geometry
+              IF NEW.centroid IS NULL THEN
+                NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
+              END IF;
 
-                -- merge in extra tags
-                NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
+              -- merge in the name, re-init word vector
+              IF NOT linkedPlacex.name IS NULL THEN
+                NEW.name := linkedPlacex.name || NEW.name;
+                name_vector := make_keywords(NEW.name);
+              END IF;
 
-                -- mark the linked place (excludes from search results)
-                UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
+              -- merge in extra tags
+              NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
 
-                -- keep a note of the node id in case we need it for wikipedia in a bit
-                linked_node_id := linkedPlacex.osm_id;
-              END IF;
+              -- mark the linked place (excludes from search results)
+              UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
 
-            END LOOP;
+              -- keep a note of the node id in case we need it for wikipedia in a bit
+              linked_node_id := linkedPlacex.osm_id;
+            END IF;
 
           END LOOP;
 
-        END IF;
-      END IF;
+        END LOOP;
 
+      END IF;
     END IF;
 
-    -- Name searches can be done for ways as well as relations
-    IF NEW.osm_type in ('W','R') AND NEW.rank_search < 26 AND NEW.rank_address > 0 THEN
+  END IF;
 
-      -- not found one yet? how about doing a name search
-      IF NEW.centroid IS NULL AND (NEW.name->'name') is not null and make_standard_name(NEW.name->'name') != '' THEN
+  -- Name searches can be done for ways as well as relations
+  IF NEW.osm_type in ('W','R') AND NEW.rank_search < 26 AND NEW.rank_address > 0 THEN
 
-        FOR linkedPlacex IN select placex.* from placex WHERE
-          make_standard_name(name->'name') = make_standard_name(NEW.name->'name')
-          AND placex.rank_address = NEW.rank_address
-          AND placex.place_id != NEW.place_id
-          AND placex.osm_type = 'N'::char(1) AND placex.rank_search < 26
-          AND st_covers(NEW.geometry, placex.geometry)
-        LOOP
+    -- not found one yet? how about doing a name search
+    IF NEW.centroid IS NULL AND (NEW.name->'name') is not null and make_standard_name(NEW.name->'name') != '' THEN
 
-          -- If we don't already have one use this as the centre point of the geometry
-          IF NEW.centroid IS NULL THEN
-            NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
-          END IF;
+      FOR linkedPlacex IN select placex.* from placex WHERE
+        make_standard_name(name->'name') = make_standard_name(NEW.name->'name')
+        AND placex.rank_address = NEW.rank_address
+        AND placex.place_id != NEW.place_id
+        AND placex.osm_type = 'N'::char(1) AND placex.rank_search < 26
+        AND st_covers(NEW.geometry, placex.geometry)
+      LOOP
 
-          -- merge in the name, re-init word vector
-          NEW.name := linkedPlacex.name || NEW.name;
-          name_vector := make_keywords(NEW.name);
+        -- If we don't already have one use this as the centre point of the geometry
+        IF NEW.centroid IS NULL THEN
+          NEW.centroid := coalesce(linkedPlacex.centroid,st_centroid(linkedPlacex.geometry));
+        END IF;
 
-          -- merge in extra tags
-          NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
+        -- merge in the name, re-init word vector
+        NEW.name := linkedPlacex.name || NEW.name;
+        name_vector := make_keywords(NEW.name);
 
-          -- mark the linked place (excludes from search results)
-          UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
+        -- merge in extra tags
+        NEW.extratags := hstore(linkedPlacex.class, linkedPlacex.type) || coalesce(linkedPlacex.extratags, ''::hstore) || coalesce(NEW.extratags, ''::hstore);
 
-          -- keep a note of the node id in case we need it for wikipedia in a bit
-          linked_node_id := linkedPlacex.osm_id;
-        END LOOP;
-      END IF;
+        -- mark the linked place (excludes from search results)
+        UPDATE placex set linked_place_id = NEW.place_id where place_id = linkedPlacex.place_id;
 
-      IF NEW.centroid IS NOT NULL THEN
-        place_centroid := NEW.centroid;
-        -- Place might have had only a name tag before but has now received translations
-        -- from the linked place. Make sure a name tag for the default language exists in
-        -- this case. 
-        IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
-          default_language := get_country_language_code(NEW.calculated_country_code);
-          IF default_language IS NOT NULL THEN
-            IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
-              NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
-            ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
-              NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
-            END IF;
+        -- keep a note of the node id in case we need it for wikipedia in a bit
+        linked_node_id := linkedPlacex.osm_id;
+      END LOOP;
+    END IF;
+
+    IF NEW.centroid IS NOT NULL THEN
+      place_centroid := NEW.centroid;
+      -- Place might have had only a name tag before but has now received translations
+      -- from the linked place. Make sure a name tag for the default language exists in
+      -- this case. 
+      IF NEW.name is not null AND array_upper(akeys(NEW.name),1) > 1 THEN
+        default_language := get_country_language_code(NEW.calculated_country_code);
+        IF default_language IS NOT NULL THEN
+          IF NEW.name ? 'name' AND NOT NEW.name ? ('name:'||default_language) THEN
+            NEW.name := NEW.name || hstore(('name:'||default_language), (NEW.name -> 'name'));
+          ELSEIF NEW.name ? ('name:'||default_language) AND NOT NEW.name ? 'name' THEN
+            NEW.name := NEW.name || hstore('name', (NEW.name -> ('name:'||default_language)));
           END IF;
         END IF;
       END IF;
-
-      -- Did we gain a wikipedia tag in the process? then we need to recalculate our importance
-      IF NEW.importance is null THEN
-        select language||':'||title,importance from get_wikipedia_match(NEW.extratags, NEW.calculated_country_code) INTO NEW.wikipedia,NEW.importance;
-      END IF;
-      -- Still null? how about looking it up by the node id
-      IF NEW.importance IS NULL THEN
-        select language||':'||title,importance from wikipedia_article where osm_type = 'N'::char(1) and osm_id = linked_node_id order by importance desc limit 1 INTO NEW.wikipedia,NEW.importance;
-      END IF;
-
     END IF;
 
-    -- make sure all names are in the word table
-    IF NEW.admin_level = 2 AND NEW.class = 'boundary' AND NEW.type = 'administrative' AND NEW.country_code IS NOT NULL THEN
-      perform create_country(NEW.name, lower(NEW.country_code));
+    -- Did we gain a wikipedia tag in the process? then we need to recalculate our importance
+    IF NEW.importance is null THEN
+      select language||':'||title,importance from get_wikipedia_match(NEW.extratags, NEW.calculated_country_code) INTO NEW.wikipedia,NEW.importance;
+    END IF;
+    -- Still null? how about looking it up by the node id
+    IF NEW.importance IS NULL THEN
+      select language||':'||title,importance from wikipedia_article where osm_type = 'N'::char(1) and osm_id = linked_node_id order by importance desc limit 1 INTO NEW.wikipedia,NEW.importance;
     END IF;
 
-    NEW.parent_place_id = 0;
-    parent_place_id_rank = 0;
+  END IF;
 
-    -- convert isin to array of tokenids
-    isin_tokens := '{}'::int[];
-    IF NEW.isin IS NOT NULL THEN
-      isin := regexp_split_to_array(NEW.isin, E'[;,]');
-      IF array_upper(isin, 1) IS NOT NULL THEN
-        FOR i IN 1..array_upper(isin, 1) LOOP
-          address_street_word_id := get_name_id(make_standard_name(isin[i]));
-          IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
-            nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
-            isin_tokens := isin_tokens || address_street_word_id;
-          END IF;
+  -- make sure all names are in the word table
+  IF NEW.admin_level = 2 AND NEW.class = 'boundary' AND NEW.type = 'administrative' AND NEW.country_code IS NOT NULL THEN
+    perform create_country(NEW.name, lower(NEW.country_code));
+  END IF;
 
-          -- merge word into address vector
-          address_street_word_id := get_word_id(make_standard_name(isin[i]));
-          IF address_street_word_id IS NOT NULL THEN
-            nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
-          END IF;
-        END LOOP;
-      END IF;
-    END IF;
-    IF NEW.postcode IS NOT NULL THEN
-      isin := regexp_split_to_array(NEW.postcode, E'[;,]');
-      IF array_upper(isin, 1) IS NOT NULL THEN
-        FOR i IN 1..array_upper(isin, 1) LOOP
-          address_street_word_id := get_name_id(make_standard_name(isin[i]));
-          IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
-            nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
-            isin_tokens := isin_tokens || address_street_word_id;
-          END IF;
+  NEW.parent_place_id = 0;
+  parent_place_id_rank = 0;
 
-          -- merge into address vector
-          address_street_word_id := get_word_id(make_standard_name(isin[i]));
-          IF address_street_word_id IS NOT NULL THEN
-            nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
-          END IF;
-        END LOOP;
-      END IF;
-    END IF;
 
-    -- %NOTIGERDATA% IF 0 THEN
-    -- for the USA we have an additional address table.  Merge in zip codes from there too
-    IF NEW.rank_search = 26 AND NEW.calculated_country_code = 'us' THEN
-      FOR location IN SELECT distinct postcode from location_property_tiger where parent_place_id = NEW.place_id LOOP
-        address_street_word_id := get_name_id(make_standard_name(location.postcode));
-        nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
-        isin_tokens := isin_tokens || address_street_word_id;
+  -- convert isin to array of tokenids
+  isin_tokens := '{}'::int[];
+  IF NEW.isin IS NOT NULL THEN
+    isin := regexp_split_to_array(NEW.isin, E'[;,]');
+    IF array_upper(isin, 1) IS NOT NULL THEN
+      FOR i IN 1..array_upper(isin, 1) LOOP
+        address_street_word_id := get_name_id(make_standard_name(isin[i]));
+        IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
+          nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
+          isin_tokens := isin_tokens || address_street_word_id;
+        END IF;
 
-        -- also merge in the single word version
-        address_street_word_id := get_word_id(make_standard_name(location.postcode));
-        nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
+        -- merge word into address vector
+        address_street_word_id := get_word_id(make_standard_name(isin[i]));
+        IF address_street_word_id IS NOT NULL THEN
+          nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
+        END IF;
       END LOOP;
     END IF;
-    -- %NOTIGERDATA% END IF;
+  END IF;
+  IF NEW.postcode IS NOT NULL THEN
+    isin := regexp_split_to_array(NEW.postcode, E'[;,]');
+    IF array_upper(isin, 1) IS NOT NULL THEN
+      FOR i IN 1..array_upper(isin, 1) LOOP
+        address_street_word_id := get_name_id(make_standard_name(isin[i]));
+        IF address_street_word_id IS NOT NULL AND NOT(ARRAY[address_street_word_id] <@ isin_tokens) THEN
+          nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
+          isin_tokens := isin_tokens || address_street_word_id;
+        END IF;
+
+        -- merge into address vector
+        address_street_word_id := get_word_id(make_standard_name(isin[i]));
+        IF address_street_word_id IS NOT NULL THEN
+          nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
+        END IF;
+      END LOOP;
+    END IF;
+  END IF;
+
+  -- %NOTIGERDATA% IF 0 THEN
+  -- for the USA we have an additional address table.  Merge in zip codes from there too
+  IF NEW.rank_search = 26 AND NEW.calculated_country_code = 'us' THEN
+    FOR location IN SELECT distinct postcode from location_property_tiger where parent_place_id = NEW.place_id LOOP
+      address_street_word_id := get_name_id(make_standard_name(location.postcode));
+      nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
+      isin_tokens := isin_tokens || address_street_word_id;
+
+      -- also merge in the single word version
+      address_street_word_id := get_word_id(make_standard_name(location.postcode));
+      nameaddress_vector := array_merge(nameaddress_vector, ARRAY[address_street_word_id]);
+    END LOOP;
+  END IF;
+  -- %NOTIGERDATA% END IF;
 
 -- RAISE WARNING 'ISIN: %', isin_tokens;
 
-    -- Process area matches
-    location_rank_search := 0;
-    location_distance := 0;
-    location_parent := NULL;
-    -- added ourself as address already
-    address_havelevel[NEW.rank_address] := true;
-    -- RAISE WARNING '  getNearFeatures(%,''%'',%,''%'')',NEW.partition, place_centroid, search_maxrank, isin_tokens;
-    FOR location IN SELECT * from getNearFeatures(NEW.partition, place_centroid, search_maxrank, isin_tokens) LOOP
+  -- Process area matches
+  location_rank_search := 0;
+  location_distance := 0;
+  location_parent := NULL;
+  -- added ourself as address already
+  address_havelevel[NEW.rank_address] := true;
+  -- RAISE WARNING '  getNearFeatures(%,''%'',%,''%'')',NEW.partition, place_centroid, search_maxrank, isin_tokens;
+  FOR location IN SELECT * from getNearFeatures(NEW.partition, place_centroid, search_maxrank, isin_tokens) LOOP
 
 --RAISE WARNING '  AREA: %',location;
 
-      IF location.rank_address != location_rank_search THEN
-        location_rank_search := location.rank_address;
-        IF location.isguess THEN
-          location_distance := location.distance * 1.5;
+    IF location.rank_address != location_rank_search THEN
+      location_rank_search := location.rank_address;
+      IF location.isguess THEN
+        location_distance := location.distance * 1.5;
+      ELSE
+        IF location.rank_address <= 12 THEN
+          -- for county and above, if we have an area consider that exact
+          -- (It would be nice to relax the constraint for places close to
+          --  the boundary but we'd need the exact geometry for that. Too
+          --  expensive.)
+          location_distance = 0;
         ELSE
-          IF location.rank_address <= 12 THEN
-            -- for county and above, if we have an area consider that exact
-            -- (It would be nice to relax the constraint for places close to
-            --  the boundary but we'd need the exact geometry for that. Too
-            --  expensive.)
-            location_distance = 0;
-          ELSE
-            -- Below county level remain slightly fuzzy.
-            location_distance := location.distance * 0.5;
-          END IF;
+          -- Below county level remain slightly fuzzy.
+          location_distance := location.distance * 0.5;
         END IF;
-      ELSE
-        CONTINUE WHEN location.keywords <@ location_keywords;
       END IF;
+    ELSE
+      CONTINUE WHEN location.keywords <@ location_keywords;
+    END IF;
 
-      IF location.distance < location_distance OR NOT location.isguess THEN
-        location_keywords := location.keywords;
-
-        location_isaddress := NOT address_havelevel[location.rank_address];
-        IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
-            location_isaddress := ST_Contains(location_parent,location.centroid);
-        END IF;
+    IF location.distance < location_distance OR NOT location.isguess THEN
+      location_keywords := location.keywords;
 
-        -- RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
-        -- Add it to the list of search terms
-        IF location.rank_search > 4 THEN
-            nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
-        END IF;
-        INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, true, location_isaddress, location.distance, location.rank_address);
+      location_isaddress := NOT address_havelevel[location.rank_address];
+      IF location_isaddress AND location.isguess AND location_parent IS NOT NULL THEN
+          location_isaddress := ST_Contains(location_parent,location.centroid);
+      END IF;
 
-        IF location_isaddress THEN
+      -- RAISE WARNING '% isaddress: %', location.place_id, location_isaddress;
+      -- Add it to the list of search terms
+      IF location.rank_search > 4 THEN
+          nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
+      END IF;
+      INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, true, location_isaddress, location.distance, location.rank_address);
 
-          address_havelevel[location.rank_address] := true;
-          IF NOT location.isguess THEN
-            SELECT geometry FROM placex WHERE place_id = location.place_id INTO location_parent;
-          END IF;
-
-          IF location.rank_address > parent_place_id_rank THEN
-            NEW.parent_place_id = location.place_id;
-            parent_place_id_rank = location.rank_address;
-          END IF;
+      IF location_isaddress THEN
 
+        address_havelevel[location.rank_address] := true;
+        IF NOT location.isguess THEN
+          SELECT geometry FROM placex WHERE place_id = location.place_id INTO location_parent;
         END IF;
 
---RAISE WARNING '  Terms: (%) %',location, nameaddress_vector;
+        IF location.rank_address > parent_place_id_rank THEN
+          NEW.parent_place_id = location.place_id;
+          parent_place_id_rank = location.rank_address;
+        END IF;
 
       END IF;
 
-    END LOOP;
-
-    -- try using the isin value to find parent places
-    IF array_upper(isin_tokens, 1) IS NOT NULL THEN
-      FOR i IN 1..array_upper(isin_tokens, 1) LOOP
---RAISE WARNING '  getNearestNamedFeature: % % % %',NEW.partition, place_centroid, search_maxrank, isin_tokens[i];
-        IF NOT ARRAY[isin_tokens[i]] <@ nameaddress_vector THEN
-
-          FOR location IN SELECT * from getNearestNamedFeature(NEW.partition, place_centroid, search_maxrank, isin_tokens[i]) LOOP
+--RAISE WARNING '  Terms: (%) %',location, nameaddress_vector;
 
-  --RAISE WARNING '  ISIN: %',location;
+    END IF;
 
-            IF location.rank_search > 4 THEN
-                nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
-                INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, false, NOT address_havelevel[location.rank_address], location.distance, location.rank_address);
-                address_havelevel[location.rank_address] := true;
+  END LOOP;
 
-                IF location.rank_address > parent_place_id_rank THEN
-                  NEW.parent_place_id = location.place_id;
-                  parent_place_id_rank = location.rank_address;
-                END IF;
-            END IF;
-          END LOOP;
+  -- try using the isin value to find parent places
+  IF array_upper(isin_tokens, 1) IS NOT NULL THEN
+    FOR i IN 1..array_upper(isin_tokens, 1) LOOP
+--RAISE WARNING '  getNearestNamedFeature: % % % %',NEW.partition, place_centroid, search_maxrank, isin_tokens[i];
+      IF NOT ARRAY[isin_tokens[i]] <@ nameaddress_vector THEN
 
-        END IF;
+        FOR location IN SELECT * from getNearestNamedFeature(NEW.partition, place_centroid, search_maxrank, isin_tokens[i]) LOOP
 
-      END LOOP;
-    END IF;
+--RAISE WARNING '  ISIN: %',location;
 
-    -- for long ways we should add search terms for the entire length
-    IF st_length(NEW.geometry) > 0.05 THEN
+          IF location.rank_search > 4 THEN
+              nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
+              INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, false, NOT address_havelevel[location.rank_address], location.distance, location.rank_address);
+              address_havelevel[location.rank_address] := true;
 
-      location_rank_search := 0;
-      location_distance := 0;
+              IF location.rank_address > parent_place_id_rank THEN
+                NEW.parent_place_id = location.place_id;
+                parent_place_id_rank = location.rank_address;
+              END IF;
+          END IF;
+        END LOOP;
 
-      FOR location IN SELECT * from getNearFeatures(NEW.partition, NEW.geometry, search_maxrank, isin_tokens) LOOP
+      END IF;
 
-        IF location.rank_address != location_rank_search THEN
-          location_rank_search := location.rank_address;
-          location_distance := location.distance * 1.5;
-        END IF;
+    END LOOP;
+  END IF;
 
-        IF location.rank_search > 4 AND location.distance < location_distance THEN
+  -- for long ways we should add search terms for the entire length
+  IF st_length(NEW.geometry) > 0.05 THEN
 
-          -- Add it to the list of search terms
-          nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
-          INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, true, false, location.distance, location.rank_address); 
+    location_rank_search := 0;
+    location_distance := 0;
 
-        END IF;
+    FOR location IN SELECT * from getNearFeatures(NEW.partition, NEW.geometry, search_maxrank, isin_tokens) LOOP
 
-      END LOOP;
+      IF location.rank_address != location_rank_search THEN
+        location_rank_search := location.rank_address;
+        location_distance := location.distance * 1.5;
+      END IF;
 
-    END IF;
+      IF location.rank_search > 4 AND location.distance < location_distance THEN
 
-    -- if we have a name add this to the name search table
-    IF NEW.name IS NOT NULL THEN
+        -- Add it to the list of search terms
+        nameaddress_vector := array_merge(nameaddress_vector, location.keywords::integer[]);
+        INSERT INTO place_addressline VALUES (NEW.place_id, location.place_id, true, false, location.distance, location.rank_address); 
 
-      IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
-        result := add_location(NEW.place_id, NEW.calculated_country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
       END IF;
 
-      IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
-        result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.calculated_country_code, NEW.geometry);
-      END IF;
+    END LOOP;
 
-      result := insertSearchName(NEW.partition, NEW.place_id, NEW.calculated_country_code, name_vector, nameaddress_vector, NEW.rank_search, NEW.rank_address, NEW.importance, place_centroid, NEW.geometry);
+  END IF;
+
+  -- if we have a name add this to the name search table
+  IF NEW.name IS NOT NULL THEN
 
+    IF NEW.rank_search <= 25 and NEW.rank_address > 0 THEN
+      result := add_location(NEW.place_id, NEW.calculated_country_code, NEW.partition, name_vector, NEW.rank_search, NEW.rank_address, NEW.geometry);
     END IF;
 
-    -- If we've not managed to pick up a better one - default centroid
-    IF NEW.centroid IS NULL THEN
-      NEW.centroid := place_centroid;
+    IF NEW.rank_search between 26 and 27 and NEW.class = 'highway' THEN
+      result := insertLocationRoad(NEW.partition, NEW.place_id, NEW.calculated_country_code, NEW.geometry);
     END IF;
 
+    result := insertSearchName(NEW.partition, NEW.place_id, NEW.calculated_country_code, name_vector, nameaddress_vector, NEW.rank_search, NEW.rank_address, NEW.importance, place_centroid, NEW.geometry);
+
   END IF;
 
+  -- If we've not managed to pick up a better one - default centroid
+  IF NEW.centroid IS NULL THEN
+    NEW.centroid := place_centroid;
+  END IF;
+  
   RETURN NEW;
 END;
 $$
@@ -1841,6 +1838,8 @@ BEGIN
     --DEBUG: RAISE WARNING 'placex_delete:06 % %',OLD.osm_type,OLD.osm_id;
     update placex set indexed_status = 2 where parent_place_id = OLD.place_id and indexed_status = 0;
     --DEBUG: RAISE WARNING 'placex_delete:07 % %',OLD.osm_type,OLD.osm_id;
+    -- reparenting also for OSM Interpolation Lines (and for Tiger?)
+    update location_property_osmline set indexed_status = 2 where indexed_status = 0 and parent_place_id = OLD.place_id;
 
   END IF;
 
@@ -1898,8 +1897,8 @@ BEGIN
   UPDATE placex set indexed_status = 100 where osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = OLD.class and type = OLD.type;
 
   -- interpolations are special
-  IF OLD.class = 'place' and OLD.type = 'houses' THEN
-    UPDATE placex set indexed_status = 100 where osm_type = OLD.osm_type and osm_id = OLD.osm_id and class = 'place' and type = 'address';
+  IF OLD.osm_type='W' and OLD.class = 'place' and OLD.type = 'houses' THEN
+    UPDATE location_property_osmline set indexed_status = 100 where osm_id = OLD.osm_id; -- osm_id = wayid (=old.osm_id)
   END IF;
 
   RETURN OLD;
@@ -1914,6 +1913,7 @@ DECLARE
   i INTEGER;
   existing RECORD;
   existingplacex RECORD;
+  existingline RECORD;
   existinggeometry GEOMETRY;
   existingplace_id BIGINT;
   result BOOLEAN;
@@ -1922,215 +1922,279 @@ BEGIN
 
   --DEBUG: RAISE WARNING '-----------------------------------------------------------------------------------';
   --DEBUG: RAISE WARNING 'place_insert: % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,st_area(NEW.geometry);
-
-  IF FALSE and NEW.osm_type = 'R' THEN
-    select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
-    --DEBUG: RAISE WARNING '%', existingplacex;
-  END IF;
-
+  -- filter wrong tupels
   IF ST_IsEmpty(NEW.geometry) OR NOT ST_IsValid(NEW.geometry) OR ST_X(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') OR ST_Y(ST_Centroid(NEW.geometry))::text in ('NaN','Infinity','-Infinity') THEN  
     INSERT INTO import_polygon_error values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.country_code, 
       now(), ST_IsValidReason(NEW.geometry), null, NEW.geometry);
 --    RAISE WARNING 'Invalid Geometry: % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type;
     RETURN null;
   END IF;
-
-  -- Patch in additional country names
-  IF NEW.admin_level = 2 AND NEW.type = 'administrative' AND NEW.country_code is not null THEN
-    select coalesce(country_name.name || NEW.name,NEW.name) from country_name where country_name.country_code = lower(NEW.country_code) INTO NEW.name;
-  END IF;
+  
+  -- decide, whether its an osm interpolation line => insert_osmline, or else just insert into placex
+  IF NEW.class='place' and NEW.type='houses' and NEW.osm_type='W' and ST_GeometryType(NEW.geometry) = 'ST_LineString' THEN    
+    -- Have we already done this place?
+    select * from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existing;
     
-  -- Have we already done this place?
-  select * from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existing;
-
-  -- Get the existing place_id
-  select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
-
-  -- Handle a place changing type by removing the old data
-  -- My generated 'place' types are causing havok because they overlap with real keys
-  -- TODO: move them to their own special purpose key/class to avoid collisions
-  IF existing.osm_type IS NULL THEN
-    DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class;
-  END IF;
-
-  --DEBUG: RAISE WARNING 'Existing: %',existing.osm_id;
-  --DEBUG: RAISE WARNING 'Existing PlaceX: %',existingplacex.place_id;
-
-  -- Log and discard 
-  IF existing.geometry is not null AND st_isvalid(existing.geometry) 
-    AND st_area(existing.geometry) > 0.02
-    AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
-    AND st_area(NEW.geometry) < st_area(existing.geometry)*0.5
-    THEN
-    INSERT INTO import_polygon_error values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.country_code, now(), 
-      'Area reduced from '||st_area(existing.geometry)||' to '||st_area(NEW.geometry), existing.geometry, NEW.geometry);
-    RETURN null;
-  END IF;
-
-  DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
-  DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
-
-  -- To paraphrase, if there isn't an existing item, OR if the admin level has changed
-  IF existingplacex.osm_type IS NULL OR
-    (coalesce(existingplacex.admin_level, 15) != coalesce(NEW.admin_level, 15) AND existingplacex.class = 'boundary' AND existingplacex.type = 'administrative')
-  THEN
-
-    IF existingplacex.osm_type IS NOT NULL THEN
-      -- sanity check: ignore admin_level changes on places with too many active children
-      -- or we end up reindexing entire countries because somebody accidentally deleted admin_level
-      --LIMIT INDEXING: SELECT count(*) FROM (SELECT 'a' FROM placex , place_addressline where address_place_id = existingplacex.place_id and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress LIMIT 100001) sub INTO i;
-      --LIMIT INDEXING: IF i > 100000 THEN
-      --LIMIT INDEXING:  RETURN null;
-      --LIMIT INDEXING: END IF;
-    END IF;
-
-    IF existing.osm_type IS NOT NULL THEN
-      -- pathological case caused by the triggerless copy into place during initial import
-      -- force delete even for large areas, it will be reinserted later
-      UPDATE place set geometry = ST_SetSRID(ST_Point(0,0), 4326) where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
-      DELETE from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
-    END IF;
-
-    -- No - process it as a new insertion (hopefully of low rank or it will be slow)
-    insert into placex (osm_type, osm_id, class, type, name, admin_level, housenumber, 
-      street, addr_place, isin, postcode, country_code, extratags, geometry)
-      values (NEW.osm_type
-        ,NEW.osm_id
-        ,NEW.class
-        ,NEW.type
-        ,NEW.name
-        ,NEW.admin_level
-        ,NEW.housenumber
-        ,NEW.street
-        ,NEW.addr_place
-        ,NEW.isin
-        ,NEW.postcode
-        ,NEW.country_code
-        ,NEW.extratags
-        ,NEW.geometry
-        );
-
-    --DEBUG: RAISE WARNING 'insert done % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,NEW.name;
-
-    RETURN NEW;
-  END IF;
-
-  -- Various ways to do the update
-
-  -- Debug, what's changed?
-  IF FALSE THEN
-    IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '') THEN
-      RAISE WARNING 'update details, name: % % % %',NEW.osm_type,NEW.osm_id,existing.name::text,NEW.name::text;
+    -- Get the existing place_id
+    select * from location_property_osmline where osm_id = NEW.osm_id INTO existingline;
+    
+    -- Handle a place changing type by removing the old data (this trigger is executed BEFORE INSERT of the NEW tupel)
+    -- My generated 'place' types are causing havok because they overlap with real keys
+    -- TODO: move them to their own special purpose key/class to avoid collisions
+    IF existing.osm_type IS NULL THEN
+      DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class;
     END IF;
-    IF coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '') THEN
-      RAISE WARNING 'update details, housenumber: % % % %',NEW.osm_type,NEW.osm_id,existing.housenumber,NEW.housenumber;
+    
+    DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
+    DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
+    
+    -- To paraphrase, if there isn't an existing item
+    IF existingline.osm_id IS NULL THEN
+      -- insert new line into location_property_osmline, use function insert_osmline
+      i = insert_osmline(NEW.osm_id, NEW.housenumber, NEW.street, NEW.addr_place, NEW.postcode, NEW.country_code, NEW.geometry);
+      RETURN NEW;
     END IF;
-    IF coalesce(existing.street, '') != coalesce(NEW.street, '') THEN
-      RAISE WARNING 'update details, street: % % % %',NEW.osm_type,NEW.osm_id,existing.street,NEW.street;
+    IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
+       OR coalesce(existing.extratags::text, '') != coalesce(NEW.extratags::text, '')
+       OR coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '')
+       OR coalesce(existing.street, '') != coalesce(NEW.street, '')
+       OR coalesce(existing.addr_place, '') != coalesce(NEW.addr_place, '')
+       OR coalesce(existing.isin, '') != coalesce(NEW.isin, '')
+       OR coalesce(existing.postcode, '') != coalesce(NEW.postcode, '')
+       OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '')
+       OR coalesce(existing.admin_level, 15) != coalesce(NEW.admin_level, 15)
+       OR existing.geometry::text != NEW.geometry::text
+       THEN
+
+      update place set 
+        name = NEW.name,
+        housenumber  = NEW.housenumber,
+        street = NEW.street,
+        addr_place = NEW.addr_place,
+        isin = NEW.isin,
+        postcode = NEW.postcode,
+        country_code = NEW.country_code,
+        extratags = NEW.extratags,
+        admin_level = NEW.admin_level,
+        geometry = NEW.geometry
+        where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
+
+      -- update method for interpolation lines: delete all old interpolation lines with same osm_id (update on place) and insert the new one(s) (they can be split up, if they have > 2 nodes)
+      delete from location_property_osmline where osm_id = NEW.osm_id;
+      i = insert_osmline(NEW.osm_id, NEW.housenumber, NEW.street, NEW.addr_place, NEW.postcode, NEW.country_code, NEW.geometry);
     END IF;
-    IF coalesce(existing.addr_place, '') != coalesce(NEW.addr_place, '') THEN
-      RAISE WARNING 'update details, street: % % % %',NEW.osm_type,NEW.osm_id,existing.addr_place,NEW.addr_place;
+    
+    -- for interpolations invalidate all nodes on the line
+    update placex p set indexed_status = 2 from planet_osm_ways w where w.id = NEW.osm_id and p.osm_type = 'N' and p.osm_id = any(w.nodes);
+    RETURN NULL;
+  
+  ELSE -- insert to placex
+  
+    IF FALSE and NEW.osm_type = 'R' THEN
+      select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
+      --DEBUG: RAISE WARNING '%', existingplacex;
     END IF;
-    IF coalesce(existing.isin, '') != coalesce(NEW.isin, '') THEN
-      RAISE WARNING 'update details, isin: % % % %',NEW.osm_type,NEW.osm_id,existing.isin,NEW.isin;
+    -- Patch in additional country names
+    IF NEW.admin_level = 2 AND NEW.type = 'administrative' AND NEW.country_code is not null THEN
+      select coalesce(country_name.name || NEW.name,NEW.name) from country_name where country_name.country_code = lower(NEW.country_code) INTO NEW.name;
     END IF;
-    IF coalesce(existing.postcode, '') != coalesce(NEW.postcode, '') THEN
-      RAISE WARNING 'update details, postcode: % % % %',NEW.osm_type,NEW.osm_id,existing.postcode,NEW.postcode;
+      
+    -- Have we already done this place?
+    select * from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existing;
+
+    -- Get the existing place_id
+    select * from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type INTO existingplacex;
+
+    -- Handle a place changing type by removing the old data
+    -- My generated 'place' types are causing havok because they overlap with real keys
+    -- TODO: move them to their own special purpose key/class to avoid collisions
+    IF existing.osm_type IS NULL THEN
+      DELETE FROM place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class;
     END IF;
-    IF coalesce(existing.country_code, '') != coalesce(NEW.country_code, '') THEN
-      RAISE WARNING 'update details, country_code: % % % %',NEW.osm_type,NEW.osm_id,existing.country_code,NEW.country_code;
+
+    --DEBUG: RAISE WARNING 'Existing: %',existing.osm_id;
+    --DEBUG: RAISE WARNING 'Existing PlaceX: %',existingplacex.place_id;
+
+    -- Log and discard 
+    IF existing.geometry is not null AND st_isvalid(existing.geometry) 
+      AND st_area(existing.geometry) > 0.02
+      AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon')
+      AND st_area(NEW.geometry) < st_area(existing.geometry)*0.5
+      THEN
+      INSERT INTO import_polygon_error values (NEW.osm_type, NEW.osm_id, NEW.class, NEW.type, NEW.name, NEW.country_code, now(), 
+        'Area reduced from '||st_area(existing.geometry)||' to '||st_area(NEW.geometry), existing.geometry, NEW.geometry);
+      RETURN null;
     END IF;
-  END IF;
 
-  -- Special case for polygon shape changes because they tend to be large and we can be a bit clever about how we handle them
-  IF existing.geometry::text != NEW.geometry::text 
-     AND ST_GeometryType(existing.geometry) in ('ST_Polygon','ST_MultiPolygon')
-     AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') 
-     THEN 
+    DELETE from import_polygon_error where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
+    DELETE from import_polygon_delete where osm_type = NEW.osm_type and osm_id = NEW.osm_id;
 
-    -- Get the version of the geometry actually used (in placex table)
-    select geometry from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type into existinggeometry;
+    -- To paraphrase, if there isn't an existing item, OR if the admin level has changed
+    IF existingplacex.osm_type IS NULL OR
+      (coalesce(existingplacex.admin_level, 15) != coalesce(NEW.admin_level, 15) AND existingplacex.class = 'boundary' AND existingplacex.type = 'administrative')
+    THEN
 
-    -- Performance limit
-    IF st_area(NEW.geometry) < 0.000000001 AND st_area(existinggeometry) < 1 THEN
+      IF existingplacex.osm_type IS NOT NULL THEN
+        -- sanity check: ignore admin_level changes on places with too many active children
+        -- or we end up reindexing entire countries because somebody accidentally deleted admin_level
+        --LIMIT INDEXING: SELECT count(*) FROM (SELECT 'a' FROM placex , place_addressline where address_place_id = existingplacex.place_id and placex.place_id = place_addressline.place_id and indexed_status = 0 and place_addressline.isaddress LIMIT 100001) sub INTO i;
+        --LIMIT INDEXING: IF i > 100000 THEN
+        --LIMIT INDEXING:  RETURN null;
+        --LIMIT INDEXING: END IF;
+      END IF;
 
-      -- re-index points that have moved in / out of the polygon, could be done as a single query but postgres gets the index usage wrong
-      update placex set indexed_status = 2 where indexed_status = 0 and 
-          (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
-          AND NOT (st_covers(existinggeometry, placex.geometry) OR ST_Intersects(existinggeometry, placex.geometry))
-          AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
+      IF existing.osm_type IS NOT NULL THEN
+        -- pathological case caused by the triggerless copy into place during initial import
+        -- force delete even for large areas, it will be reinserted later
+        UPDATE place set geometry = ST_SetSRID(ST_Point(0,0), 4326) where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
+        DELETE from place where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
+      END IF;
 
-      update placex set indexed_status = 2 where indexed_status = 0 and 
-          (st_covers(existinggeometry, placex.geometry) OR ST_Intersects(existinggeometry, placex.geometry))
-          AND NOT (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
-          AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
+      -- No - process it as a new insertion (hopefully of low rank or it will be slow)
+      insert into placex (osm_type, osm_id, class, type, name, admin_level, housenumber, 
+        street, addr_place, isin, postcode, country_code, extratags, geometry)
+        values (NEW.osm_type
+          ,NEW.osm_id
+          ,NEW.class
+          ,NEW.type
+          ,NEW.name
+          ,NEW.admin_level
+          ,NEW.housenumber
+          ,NEW.street
+          ,NEW.addr_place
+          ,NEW.isin
+          ,NEW.postcode
+          ,NEW.country_code
+          ,NEW.extratags
+          ,NEW.geometry
+          );
+
+      --DEBUG: RAISE WARNING 'insert done % % % % %',NEW.osm_type,NEW.osm_id,NEW.class,NEW.type,NEW.name;
 
+      RETURN NEW;
     END IF;
 
-  END IF;
+    -- Various ways to do the update
 
+    -- Debug, what's changed?
+    IF FALSE THEN
+      IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '') THEN
+        RAISE WARNING 'update details, name: % % % %',NEW.osm_type,NEW.osm_id,existing.name::text,NEW.name::text;
+      END IF;
+      IF coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '') THEN
+        RAISE WARNING 'update details, housenumber: % % % %',NEW.osm_type,NEW.osm_id,existing.housenumber,NEW.housenumber;
+      END IF;
+      IF coalesce(existing.street, '') != coalesce(NEW.street, '') THEN
+        RAISE WARNING 'update details, street: % % % %',NEW.osm_type,NEW.osm_id,existing.street,NEW.street;
+      END IF;
+      IF coalesce(existing.addr_place, '') != coalesce(NEW.addr_place, '') THEN
+        RAISE WARNING 'update details, street: % % % %',NEW.osm_type,NEW.osm_id,existing.addr_place,NEW.addr_place;
+      END IF;
+      IF coalesce(existing.isin, '') != coalesce(NEW.isin, '') THEN
+        RAISE WARNING 'update details, isin: % % % %',NEW.osm_type,NEW.osm_id,existing.isin,NEW.isin;
+      END IF;
+      IF coalesce(existing.postcode, '') != coalesce(NEW.postcode, '') THEN
+        RAISE WARNING 'update details, postcode: % % % %',NEW.osm_type,NEW.osm_id,existing.postcode,NEW.postcode;
+      END IF;
+      IF coalesce(existing.country_code, '') != coalesce(NEW.country_code, '') THEN
+        RAISE WARNING 'update details, country_code: % % % %',NEW.osm_type,NEW.osm_id,existing.country_code,NEW.country_code;
+      END IF;
+    END IF;
 
-  IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
-     OR coalesce(existing.extratags::text, '') != coalesce(NEW.extratags::text, '')
-     OR coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '')
-     OR coalesce(existing.street, '') != coalesce(NEW.street, '')
-     OR coalesce(existing.addr_place, '') != coalesce(NEW.addr_place, '')
-     OR coalesce(existing.isin, '') != coalesce(NEW.isin, '')
-     OR coalesce(existing.postcode, '') != coalesce(NEW.postcode, '')
-     OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '')
-     OR coalesce(existing.admin_level, 15) != coalesce(NEW.admin_level, 15)
-     OR existing.geometry::text != NEW.geometry::text
-     THEN
+    -- Special case for polygon shape changes because they tend to be large and we can be a bit clever about how we handle them
+    IF existing.geometry::text != NEW.geometry::text 
+       AND ST_GeometryType(existing.geometry) in ('ST_Polygon','ST_MultiPolygon')
+       AND ST_GeometryType(NEW.geometry) in ('ST_Polygon','ST_MultiPolygon') 
+       THEN 
 
-    update place set 
-      name = NEW.name,
-      housenumber  = NEW.housenumber,
-      street = NEW.street,
-      addr_place = NEW.addr_place,
-      isin = NEW.isin,
-      postcode = NEW.postcode,
-      country_code = NEW.country_code,
-      extratags = NEW.extratags,
-      admin_level = NEW.admin_level,
-      geometry = NEW.geometry
-      where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
+      -- Get the version of the geometry actually used (in placex table)
+      select geometry from placex where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type into existinggeometry;
 
-    IF NEW.class in ('place','boundary') AND NEW.type in ('postcode','postal_code') THEN
-        IF NEW.postcode IS NULL THEN
-            -- postcode was deleted, no longer retain in placex
-            DELETE FROM placex where place_id = existingplacex.place_id;
-            RETURN NULL;
-        END IF;
+      -- Performance limit
+      IF st_area(NEW.geometry) < 0.000000001 AND st_area(existinggeometry) < 1 THEN
+
+        -- re-index points that have moved in / out of the polygon, could be done as a single query but postgres gets the index usage wrong
+        update placex set indexed_status = 2 where indexed_status = 0 and 
+            (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
+            AND NOT (st_covers(existinggeometry, placex.geometry) OR ST_Intersects(existinggeometry, placex.geometry))
+            AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
+
+        update placex set indexed_status = 2 where indexed_status = 0 and 
+            (st_covers(existinggeometry, placex.geometry) OR ST_Intersects(existinggeometry, placex.geometry))
+            AND NOT (st_covers(NEW.geometry, placex.geometry) OR ST_Intersects(NEW.geometry, placex.geometry))
+            AND rank_search > existingplacex.rank_search AND (rank_search < 28 or name is not null);
+
+      END IF;
 
-        NEW.name := hstore('ref', NEW.postcode);
     END IF;
 
-    update placex set 
-      name = NEW.name,
-      housenumber = NEW.housenumber,
-      street = NEW.street,
-      addr_place = NEW.addr_place,
-      isin = NEW.isin,
-      postcode = NEW.postcode,
-      country_code = NEW.country_code,
-      parent_place_id = null,
-      extratags = NEW.extratags,
-      admin_level = CASE WHEN NEW.admin_level > 15 THEN 15 ELSE NEW.admin_level END,
-      indexed_status = 2,    
-      geometry = NEW.geometry
-      where place_id = existingplacex.place_id;
 
-  END IF;
+    IF coalesce(existing.name::text, '') != coalesce(NEW.name::text, '')
+       OR coalesce(existing.extratags::text, '') != coalesce(NEW.extratags::text, '')
+       OR coalesce(existing.housenumber, '') != coalesce(NEW.housenumber, '')
+       OR coalesce(existing.street, '') != coalesce(NEW.street, '')
+       OR coalesce(existing.addr_place, '') != coalesce(NEW.addr_place, '')
+       OR coalesce(existing.isin, '') != coalesce(NEW.isin, '')
+       OR coalesce(existing.postcode, '') != coalesce(NEW.postcode, '')
+       OR coalesce(existing.country_code, '') != coalesce(NEW.country_code, '')
+       OR coalesce(existing.admin_level, 15) != coalesce(NEW.admin_level, 15)
+       OR existing.geometry::text != NEW.geometry::text
+       THEN
+
+      update place set 
+        name = NEW.name,
+        housenumber  = NEW.housenumber,
+        street = NEW.street,
+        addr_place = NEW.addr_place,
+        isin = NEW.isin,
+        postcode = NEW.postcode,
+        country_code = NEW.country_code,
+        extratags = NEW.extratags,
+        admin_level = NEW.admin_level,
+        geometry = NEW.geometry
+        where osm_type = NEW.osm_type and osm_id = NEW.osm_id and class = NEW.class and type = NEW.type;
+        
+
+      IF NEW.class in ('place','boundary') AND NEW.type in ('postcode','postal_code') THEN
+          IF NEW.postcode IS NULL THEN
+              -- postcode was deleted, no longer retain in placex
+              DELETE FROM placex where place_id = existingplacex.place_id;
+              RETURN NULL;
+          END IF;
 
-  -- for interpolations invalidate all nodes on the line
-  IF NEW.class = 'place' and NEW.type = 'houses' and NEW.osm_type = 'W' THEN
-    update placex p set indexed_status = 2 from planet_osm_ways w where w.id = NEW.osm_id and p.osm_type = 'N' and p.osm_id = any(w.nodes);
-  END IF;
+          NEW.name := hstore('ref', NEW.postcode);
+      END IF;
+      
+      update placex set 
+        name = NEW.name,
+        housenumber = NEW.housenumber,
+        street = NEW.street,
+        addr_place = NEW.addr_place,
+        isin = NEW.isin,
+        postcode = NEW.postcode,
+        country_code = NEW.country_code,
+        parent_place_id = null,
+        extratags = NEW.extratags,
+        admin_level = CASE WHEN NEW.admin_level > 15 THEN 15 ELSE NEW.admin_level END,
+        indexed_status = 2,    
+        geometry = NEW.geometry
+        where place_id = existingplacex.place_id;
+        
+      -- if a node(=>house), which is part of a interpolation line, changes (e.g. the street attribute) => mark this line for reparenting 
+      -- (already here, because interpolation lines are reindexed before nodes, so in the second call it would be too late)
+      -- needed for test case features/db/import: Scenario: addr:street added to housenumbers
+      IF NEW.osm_type='N' and NEW.class='place' and NEW.type='house' THEN
+          -- Is this node part of an interpolation line? search for it in location_property_osmline and mark the interpolation line for reparenting
+          update location_property_osmline p set indexed_status = 2 from planet_osm_ways w where p.linegeo && NEW.geometry and p.osm_id = w.id and NEW.osm_id = any(w.nodes);
+      END IF;
 
-  -- Abort the add (we modified the existing place instead)
-  RETURN NULL;
+    END IF;
+
+    -- Abort the add (we modified the existing place instead)
+    RETURN NULL;
+  END IF;
 
-END; 
+END;
 $$ LANGUAGE plpgsql;
 
 
@@ -2242,7 +2306,7 @@ create type addressline as (
 CREATE OR REPLACE FUNCTION get_addressdata(in_place_id BIGINT, in_housenumber INTEGER) RETURNS setof addressline 
   AS $$
 DECLARE
-  for_place_id BIGINT;--parent_place_id
+  for_place_id BIGINT;
   result TEXT[];
   search TEXT[];
   found INTEGER;
@@ -2258,14 +2322,24 @@ DECLARE
   countryname HSTORE;
   hadcountry BOOLEAN;
 BEGIN
-    --first query tiger data
-  -- %NOTIGERDATA% IF 0 THEN
-  select parent_place_id,'us', 30, postcode, null, 'place', 'house' from location_property_tiger 
+  -- first query osmline (interpolation lines)
+  select parent_place_id, calculated_country_code, 30, postcode, null, 'place', 'house' from location_property_osmline 
     WHERE place_id = in_place_id AND in_housenumber>=startnumber AND in_housenumber <= endnumber
     INTO for_place_id,searchcountrycode, searchrankaddress, searchpostcode, searchhousename, searchclass, searchtype;
   IF for_place_id IS NOT NULL THEN
     searchhousenumber = in_housenumber::text;
   END IF;
+
+  --then query tiger data
+  -- %NOTIGERDATA% IF 0 THEN
+  IF for_place_id IS NULL THEN
+    select parent_place_id,'us', 30, postcode, null, 'place', 'house' from location_property_tiger 
+      WHERE place_id = in_place_id AND in_housenumber>=startnumber AND in_housenumber <= endnumber
+      INTO for_place_id,searchcountrycode, searchrankaddress, searchpostcode, searchhousename, searchclass, searchtype;
+    IF for_place_id IS NOT NULL THEN
+      searchhousenumber = in_housenumber::text;
+    END IF;
+  END IF;
   -- %NOTIGERDATA% END IF;
 
   -- %NOAUXDATA% IF 0 THEN
index 5ee621df94c1a2b1f87fc57d2a9713279e97eed6..bf6093e0020c7068be6956544a2a8c49ca48cb21 100644 (file)
@@ -17,6 +17,11 @@ CREATE INDEX idx_placex_parent_place_id ON placex USING BTREE (parent_place_id)
 CREATE INDEX idx_placex_reverse_geometry ON placex USING gist (geometry) {ts:search-index} where rank_search != 28 and (name is not null or housenumber is not null) and class not in ('waterway','railway','tunnel','bridge','man_made');
 CREATE INDEX idx_location_area_country_place_id ON location_area_country USING BTREE (place_id) {ts:address-index};
 
+CREATE UNIQUE INDEX idx_osmline_place_id ON location_property_osmline USING BTREE (place_id) {ts:search-index};
+CREATE INDEX idx_osmline_parent_place_id ON location_property_osmline USING BTREE (parent_place_id) {ts:search-index};
+CREATE INDEX idx_osmline_geometry_sector ON location_property_osmline USING BTREE (geometry_sector) {ts:address-index};
+CREATE INDEX idx_osmline_linegeo ON location_property_osmline USING GIST (linegeo) {ts:search-index};
+
 CREATE INDEX idx_search_name_country_centroid ON search_name_country USING GIST (centroid) {ts:address-index};
 
 DROP INDEX IF EXISTS place_id_idx;
index bcc3eec1b854aa398406dd0a4738ff09addd2ebd..2578aa91cc0bd9a798221218a3a179c25ddf0127 100644 (file)
@@ -75,6 +75,37 @@ CREATE TABLE location_property (
   );
 SELECT AddGeometryColumn('location_property', 'centroid', 4326, 'POINT', 2);
 
+CREATE TABLE location_property_aux () INHERITS (location_property);
+CREATE INDEX idx_location_property_aux_place_id ON location_property_aux USING BTREE (place_id);
+CREATE INDEX idx_location_property_aux_parent_place_id ON location_property_aux USING BTREE (parent_place_id);
+CREATE INDEX idx_location_property_aux_housenumber_parent_place_id ON location_property_aux USING BTREE (parent_place_id, housenumber);
+GRANT SELECT ON location_property_aux TO "{www-user}";
+
+CREATE TABLE location_property_tiger (linegeo GEOMETRY, place_id BIGINT, partition INTEGER, parent_place_id BIGINT, startnumber INTEGER, endnumber INTEGER, interpolationtype TEXT, postcode TEXT);
+GRANT SELECT ON location_property_tiger TO "{www-user}";
+
+drop table if exists location_property_osmline;
+CREATE TABLE location_property_osmline (
+    linegeo GEOMETRY,
+    place_id BIGINT NOT NULL,
+    partition INTEGER,
+    osm_id BIGINT,
+    parent_place_id BIGINT,
+    startnumber INTEGER,
+    endnumber INTEGER,
+    interpolationtype TEXT,
+    street TEXT,
+    postcode TEXT,
+    calculated_country_code VARCHAR(2),
+    geometry_sector INTEGER,
+    indexed_status INTEGER,
+    indexed_date TIMESTAMP){ts:search-data};
+CREATE UNIQUE INDEX idx_osmline_place_id ON location_property_osmline USING BTREE (place_id) {ts:search-index};
+CREATE INDEX idx_osmline_parent_place_id ON location_property_osmline USING BTREE (parent_place_id) {ts:search-index};
+CREATE INDEX idx_osmline_geometry_sector ON location_property_osmline USING BTREE (geometry_sector) {ts:address-index};
+CREATE INDEX idx_osmline_linegeo ON location_property_osmline USING GIST (linegeo) {ts:search-index};
+GRANT SELECT ON location_property_osmline TO "{www-user}";
+
 drop table IF EXISTS search_name;
 CREATE TABLE search_name (
   place_id BIGINT,
@@ -133,13 +164,15 @@ GRANT SELECT ON planet_osm_ways to "{www-user}" ;
 GRANT SELECT ON planet_osm_rels to "{www-user}" ;
 GRANT SELECT on location_area to "{www-user}" ;
 
--- insert creates the location tagbles, creates location indexes if indexed == true
+-- insert creates the location tables, creates location indexes if indexed == true
 CREATE TRIGGER placex_before_insert BEFORE INSERT ON placex
     FOR EACH ROW EXECUTE PROCEDURE placex_insert();
 
 -- update insert creates the location tables
 CREATE TRIGGER placex_before_update BEFORE UPDATE ON placex
     FOR EACH ROW EXECUTE PROCEDURE placex_update();
+CREATE TRIGGER osmline_before_update BEFORE UPDATE ON location_property_osmline
+    FOR EACH ROW EXECUTE PROCEDURE osmline_update();
 
 -- diff update triggers
 CREATE TRIGGER placex_before_delete AFTER DELETE ON placex
index 0dcb12bdbb978bc18a4dc71edda737f416e1810e..c282d73a27ac84652116da2ecc8cbc762adcbc81 100644 (file)
@@ -18,7 +18,10 @@ Feature: Reverse geocoding
           | xml    | 4
         When looking up coordinates 53.9788769,13.0830313
         And results contain valid boundingboxes
-
+    
+    Scenario: Reverse geocoding for odd interpolated housenumber
+    
+    Scenario: Reverse geocoding for even interpolated housenumber
 
     @Tiger
     Scenario: TIGER house number
index 195cbfdf27cff359654e5ac88776d468ccdcf8ec..356c7e16156cc9e9b4bb92482850717ec955555f 100644 (file)
@@ -2,41 +2,23 @@
 Feature: Import of address interpolations
     Tests that interpolated addresses are added correctly
 
-    Scenario: Simple even two point interpolation
+    Scenario: Simple even interpolation line with two points
         Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 2           | 1 1
-          | 2      | place | house | 6           | 1 1.001
-        And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | even        | 1 1, 1 1.001
-        And the ways
-          | id | nodes
-          | 1  | 1,2
-        When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.0005
-
-    Scenario: Simple even two point interpolation with zero beginning
-        Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 0           | 1 1
-          | 2      | place | house | 8           | 1 1.001
+          | osm_id | osm_type | class | type  | housenumber | geometry
+          | 1      | N        | place | house | 2           | 1 1
+          | 2      | N        | place | house | 6           | 1 1.001
         And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | even        | 1 1, 1 1.001
+          | osm_id | osm_type | class | type   | housenumber | geometry
+          | 1      | W        | place | houses | even        | 1 1, 1 1.001
         And the ways
           | id | nodes
           | 1  | 1,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 2           | 1,1.00025
-          | 4           | 1,1.0005
-          | 6           | 1,1.00075
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 6         | 1 1, 1 1.001
 
-    Scenario: Backwards even two point interpolation
+    Scenario: Backwards even two point interpolation line
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 1 1
@@ -48,77 +30,9 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 2,1
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.0005
-
-    Scenario: Even two point interpolation with odd beginning
-        Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 11          | 1 1
-          | 2      | place | house | 16          | 1 1.001
-        And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | even        | 1 1, 1 1.001
-        And the ways
-          | id | nodes
-          | 1  | 1,2
-        When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 12          | 1,1.0002
-          | 14          | 1,1.0006
-
-    Scenario: Even two point interpolation with odd end
-        Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 10          | 1 1
-          | 2      | place | house | 15          | 1 1.001
-        And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | even        | 1 1, 1 1.001
-        And the ways
-          | id | nodes
-          | 1  | 1,2
-        When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 12          | 1,1.0004
-          | 14          | 1,1.0008
-
-    Scenario: Reverse even two point interpolation with odd beginning
-        Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 11          | 1 1
-          | 2      | place | house | 16          | 1 1.001
-        And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | even        | 1 1.001, 1 1
-        And the ways
-          | id | nodes
-          | 1  | 2,1
-        When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 12          | 1,1.0002
-          | 14          | 1,1.0006
-
-    Scenario: Reverse even two point interpolation with odd end
-        Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 10          | 1 1
-          | 2      | place | house | 15          | 1 1.001
-        And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | even        | 1 1.001, 1 1
-        And the ways
-          | id | nodes
-          | 1  | 2,1
-        When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 12          | 1,1.0004
-          | 14          | 1,1.0008
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 6         | 1 1, 1 1.001
 
       Scenario: Simple odd two point interpolation
         Given the place nodes
@@ -132,29 +46,9 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 3           | 1,1.0002
-          | 5           | 1,1.0004
-          | 7           | 1,1.0006
-          | 9           | 1,1.0008
-
-      Scenario: Odd two point interpolation with even beginning
-        Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 2           | 1 1
-          | 2      | place | house | 7           | 1 1.001
-        And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | odd         | 1 1, 1 1.001
-        And the ways
-          | id | nodes
-          | 1  | 1,2
-        When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 3           | 1,1.0002
-          | 5           | 1,1.0006
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 1           | 11        | 1 1, 1 1.001
 
      Scenario: Simple all two point interpolation
         Given the place nodes
@@ -168,27 +62,11 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 2           | 1,1.0005
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 1           | 3         | 1 1, 1 1.001
 
-    Scenario: Simple numbered two point interpolation
-        Given the place nodes
-          | osm_id | class | type  | housenumber | geometry
-          | 1      | place | house | 3           | 1 1
-          | 2      | place | house | 9           | 1 1.001
-        And the place ways
-          | osm_id | class | type   | housenumber | geometry
-          | 1      | place | houses | 3           | 1 1, 1 1.001
-        And the ways
-          | id | nodes
-          | 1  | 1,2
-        When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 6           | 1,1.0005
-
-    Scenario: Even two point interpolation with intermediate empty node
+    Scenario: Even two point interpolation line with intermediate empty node
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 1 1
@@ -200,14 +78,11 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,3,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.0005
-          | 6           | 1,1.001
-          | 8           | 1.0005,1.001
-
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 10         | 1 1, 1 1.001, 1.001 1.001
 
-    Scenario: Even two point interpolation with intermediate duplicated empty node
+    Scenario: Even two point interpolation line with intermediate duplicated empty node
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 1 1
@@ -219,13 +94,11 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,3,3,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.0005
-          | 6           | 1,1.001
-          | 8           | 1.0005,1.001
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 10         | 1 1, 1 1.001, 1.001 1.001
 
-    Scenario: Simple even three point interpolation
+    Scenario: Simple even three point interpolation line
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 1 1
@@ -238,14 +111,12 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,3,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.00025
-          | 6           | 1,1.0005
-          | 8           | 1,1.00075
-          | 12          | 1.0005,1.001
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 10        | 1 1, 1 1.001
+          | 10          | 14        | 1 1.001, 1.001 1.001
 
-     Scenario: Simple even four point interpolation
+     Scenario: Simple even four point interpolation line
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 1 1
@@ -259,15 +130,13 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,3,2,4
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.00025
-          | 6           | 1,1.0005
-          | 8           | 1,1.00075
-          | 12          | 1.0005,1.001
-          | 16          | 1.001,1.0015
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 10        | 1 1, 1 1.001
+          | 10          | 14        | 1 1.001, 1.001 1.001
+          | 14          | 18        | 1.001 1.001, 1.001 1.002
 
-    Scenario: Reverse simple even three point interpolation
+    Scenario: Reverse simple even three point interpolation line
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 1 1
@@ -280,14 +149,12 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 2,3,1
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.00025
-          | 6           | 1,1.0005
-          | 8           | 1,1.00075
-          | 12          | 1.0005,1.001
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 10        | 1 1, 1 1.001
+          | 10          | 14        | 1 1.001, 1.001 1.001
 
-    Scenario: Even three point interpolation with odd center point
+    Scenario: Even three point interpolation line with odd center point
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 1 1
@@ -300,12 +167,12 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,3,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 1,1.0004
-          | 6           | 1,1.0008
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 7         | 1 1, 1 1.001
+          | 7           | 8         | 1 1.001, 1.001 1.001
 
-    Scenario: Interpolation on self-intersecting way
+    Scenario: Interpolation line with self-intersecting way
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 0 0
@@ -318,12 +185,12 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,2,3,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 0,0.0005
-          | 8           | 0,0.0015
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 6         | 0 0, 0 0.001
+          | 6           | 10        | 0 0.001, 0 0.002
 
-    Scenario: Interpolation on self-intersecting way II
+    Scenario: Interpolation line with self-intersecting way II
         Given the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | 0 0
@@ -335,24 +202,24 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,2,3,2
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 4           | 0,0.0005
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 6         | 0 0, 0 0.001
 
 
-     Scenario: addr:street on interpolation way
-       Given the scene parallel-road
-       And the place nodes
+    Scenario: addr:street on interpolation way
+        Given the scene parallel-road
+        And the place nodes
           | osm_id | class | type  | housenumber | geometry
           | 1      | place | house | 2           | :n-middle-w
           | 2      | place | house | 6           | :n-middle-e
           | 3      | place | house | 12          | :n-middle-w
           | 4      | place | house | 16          | :n-middle-e
-       And the place ways
+        And the place ways
           | osm_id | class   | type    | housenumber | street       | geometry
           | 10     | place   | houses  | even        |              | :w-middle
           | 11     | place   | houses  | even        | Cloud Street | :w-middle
-       And the place ways
+        And the place ways
           | osm_id | class   | type     | name                    | geometry
           | 2      | highway | tertiary | 'name' : 'Sun Way'      | :w-north
           | 3      | highway | tertiary | 'name' : 'Cloud Street' | :w-south
@@ -360,49 +227,49 @@ Feature: Import of address interpolations
           | id | nodes
           | 10  | 1,100,101,102,2
           | 11  | 3,200,201,202,4
-       When importing
-       Then table placex contains
+        When importing
+        Then table placex contains
           | object | parent_place_id
           | N1     | W2
           | N2     | W2
           | N3     | W3
           | N4     | W3
-          | W10    | W2
-          | W11    | W3
-       And way 10 expands exactly to housenumbers 4
-       And way 11 expands exactly to housenumbers 14
+        Then table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W2              | 2           | 6
+          | W11    | W3              | 12          | 16
 
-     Scenario: addr:street on housenumber way
-       Given the scene parallel-road
-       And the place nodes
+    Scenario: addr:street on housenumber way
+        Given the scene parallel-road
+        And the place nodes
           | osm_id | class | type  | housenumber | street       | geometry
           | 1      | place | house | 2           |              | :n-middle-w
           | 2      | place | house | 6           |              | :n-middle-e
           | 3      | place | house | 12          | Cloud Street | :n-middle-w
           | 4      | place | house | 16          | Cloud Street | :n-middle-e
-       And the place ways
+        And the place ways
           | osm_id | class   | type    | housenumber | geometry
           | 10     | place   | houses  | even        | :w-middle
           | 11     | place   | houses  | even        | :w-middle
-       And the place ways
+        And the place ways
           | osm_id | class   | type     | name                    | geometry
           | 2      | highway | tertiary | 'name' : 'Sun Way'      | :w-north
           | 3      | highway | tertiary | 'name' : 'Cloud Street' | :w-south
         And the ways
-          | id | nodes
+          | id  | nodes
           | 10  | 1,100,101,102,2
           | 11  | 3,200,201,202,4
-       When importing
-       Then table placex contains
+        When importing
+        Then table placex contains
           | object | parent_place_id
           | N1     | W2
           | N2     | W2
           | N3     | W3
           | N4     | W3
-          | W10    | W2
-          | W11    | W3
-       And way 10 expands exactly to housenumbers 4
-       And way 11 expands exactly to housenumbers 14
+        And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W2              | 2           | 6
+          | W11    | W3              | 12          | 16
 
     Scenario: Geometry of points and way don't match (github #253)
         Given the place nodes
@@ -417,10 +284,10 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,2,3
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber  | centroid
-          | 4            | 144.963016723312,-37.7629464422819+-0.000005
-          | 8            | 144.9631440856,-37.762223694978+-0.000005
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 2           | 6         | 144.9629794 -37.7630755, 144.9630541 -37.7628174
+          | 6           | 10        | 144.9630541 -37.7628174, 144.9632341 -37.76163
 
     Scenario: Place with missing address information
         Given the place nodes
@@ -435,7 +302,6 @@ Feature: Import of address interpolations
           | id | nodes
           | 1  | 1,2,3
         When importing
-        Then way 1 expands to housenumbers
-          | housenumber | centroid
-          | 25          | 0.0001,0.0002
-          | 27          | 0.0001,0.0003
+        Then way 1 expands to lines
+          | startnumber | endnumber | geometry
+          | 23          | 29        | 0.0001 0.0001, 0.0001 0.0002, 0.0001 0.0004
index bfc93b12962f3cd6117358d42f767a846449b92c..ac72f852c9b009869b1fd13ca9085b38323d69c6 100644 (file)
@@ -23,19 +23,20 @@ Feature: Update of address interpolations
           | object | parent_place_id
           | N1     | W2
           | N2     | W2
-          | W10    | W2
-      And way 10 expands exactly to housenumbers 4
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W2              | 2           | 6
       When updating place ways
           | osm_id | class   | type    | housenumber | street       | geometry
           | 10     | place   | houses  | even        | Cloud Street | :w-middle
-      Then way 10 expands exactly to housenumbers 4
-      And table placex contains
+      Then table placex contains
           | object | parent_place_id
           | N1     | W3
           | N2     | W3
-          | W10    | W3
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W3              | 2           | 6
 
-    @Fail
     Scenario: addr:street added to housenumbers
       Given the scene parallel-road
       And the place nodes
@@ -57,18 +58,20 @@ Feature: Update of address interpolations
           | object | parent_place_id
           | N1     | W2
           | N2     | W2
-          | W10    | W2
-      And way 10 expands exactly to housenumbers 4
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W2              | 2           | 6
       When updating place nodes
           | osm_id | class | type  | street      | housenumber | geometry
           | 1      | place | house | Cloud Street| 2           | :n-middle-w
           | 2      | place | house | Cloud Street| 6           | :n-middle-e
-      Then way 10 expands exactly to housenumbers 4
-      And table placex contains
+      Then table placex contains
           | object | parent_place_id
           | N1     | W3
           | N2     | W3
-          | W10    | W3
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W3              | 2           | 6
 
 
     Scenario: interpolation tag removed
@@ -92,10 +95,11 @@ Feature: Update of address interpolations
           | object | parent_place_id
           | N1     | W2
           | N2     | W2
-          | W10    | W2
-      And way 10 expands exactly to housenumbers 4
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W2              | 2           | 6
       When marking for delete W10
-      Then way 10 expands to no housenumbers
+      Then table location_property_osmline has no entry for W10
       And table placex contains
           | object | parent_place_id
           | N1     | W2
@@ -122,17 +126,19 @@ Feature: Update of address interpolations
           | object | parent_place_id
           | N1     | W2
           | N2     | W2
-          | W10    | W2
-      And way 10 expands exactly to housenumbers 4
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W2              | 2           | 6
       When updating place ways
           | osm_id | class   | type         | name                    | geometry
           | 3      | highway | unclassified | 'name' : 'Cloud Street' | :w-south
-      Then way 10 expands exactly to housenumbers 4
-      And table placex contains
+      Then table placex contains
           | object | parent_place_id
           | N1     | W3
           | N2     | W3
-          | W10    | W3
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W3              | 2           | 6
 
 
     Scenario: referenced road deleted
@@ -156,12 +162,14 @@ Feature: Update of address interpolations
           | object | parent_place_id
           | N1     | W3
           | N2     | W3
-          | W10    | W3
-      And way 10 expands exactly to housenumbers 4
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W3              | 2           | 6
       When marking for delete W3
-      Then way 10 expands exactly to housenumbers 4
-      And table placex contains
+      Then table placex contains
           | object | parent_place_id
           | N1     | W2
           | N2     | W2
-          | W10    | W2
+      And table location_property_osmline contains
+          | object | parent_place_id | startnumber | endnumber
+          | W10    | W2              | 2           | 6
index 71a30927b3314076ff7427a59b7ed71ce3b7f5c8..7fe4da30557b0657448ba020c4e859ab744483eb 100644 (file)
@@ -35,6 +35,7 @@ def check_placex_names(step, osmtyp, osmid):
 
 
 
+
 @step(u'table ([a-z_]+) contains$')
 def check_placex_content(step, tablename):
     """ check that the given lines are in the given table
@@ -49,13 +50,21 @@ def check_placex_content(step, tablename):
             q = 'SELECT *'
             if tablename == 'placex':
                 q = q + ", ST_X(centroid) as clat, ST_Y(centroid) as clon"
-            q = q + ", ST_GeometryType(geometry) as geometrytype"
-            q = q + ' FROM %s where osm_type = %%s and osm_id = %%s' % (tablename,)
+            if tablename == 'location_property_osmline':
+                q = q + ' FROM %s where osm_id = %%s' % (tablename,)
+            else:
+                q = q + ' FROM %s where osm_type = %%s and osm_id = %%s' % (tablename,)
             if cls is None:
-                params = (osmtype, osmid)
+                if tablename == 'location_property_osmline':
+                    params = (osmid,)
+                else:
+                    params = (osmtype, osmid)
             else:
                 q = q + ' and class = %s'
-                params = (osmtype, osmid, cls)
+                if tablename == 'location_property_osmline':
+                    params = (osmid, cls)
+                else:
+                    params = (osmtype, osmid, cls)
             cur.execute(q, params)
             assert(cur.rowcount > 0)
             for res in cur:
@@ -92,6 +101,18 @@ def check_placex_missing(step, tablename, osmtyp, osmid, placeclass):
         cur.close()
         world.conn.commit()
 
+@step(u'table location_property_osmline has no entry for W(\d+)?')
+def check_osmline_missing(step, osmid):
+    cur = world.conn.cursor()
+    try:
+        q = 'SELECT count(*) FROM location_property_osmline where osm_id = %s' % (osmid, )
+        cur.execute(q)
+        numres = cur.fetchone()[0]
+        assert_equals (numres, 0)
+    finally:
+        cur.close()
+        world.conn.commit()
+
 @step(u'search_name table contains$')
 def check_search_name_content(step):
     cur = world.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
@@ -134,12 +155,35 @@ def check_interpolated_housenumbers(step, nodeid):
                    FROM placex WHERE osm_type = 'W' and osm_id = %s
                                  and class = 'place' and type = 'address'""",
                    (int(nodeid),))
+                   
     assert_equals(len(numbers), cur.rowcount)
     for r in cur:
         assert_in(r["housenumber"], numbers)
         world.match_geometry((r['clat'], r['clon']), numbers[r["housenumber"]])
         del numbers[r["housenumber"]]
 
+@step(u'way (\d+) expands to lines')
+def check_interpolation_lines(step, wayid):
+    """Check that the correct interpolation line has been entered in
+       location_property_osmline for the given source line/nodes. Expected are three columns:
+       startnumber, endnumber and linegeo
+    """
+    lines = {}
+    for line in step.hashes:
+        assert line["startnumber"] not in lines
+        lines[line["startnumber"]] = {'endnumber': line["endnumber"], 'geometry': line["geometry"]}
+    cur = world.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
+    cur.execute("""SELECT startnumber::text, endnumber::text, st_astext(linegeo) as geometry
+                   FROM location_property_osmline WHERE osm_id = %s""",
+                   (int(wayid),))
+    assert_equals(len(lines), cur.rowcount)
+    for r in cur:
+        assert_in(r["startnumber"], lines)
+        assert_equals(r["endnumber"], lines[r["startnumber"]]["endnumber"])
+        linegeo = str(str(r["geometry"].split('(')[1]).split(')')[0]).replace(',', ', ')
+        assert_equals(linegeo, lines[r["startnumber"]]["geometry"])
+        del lines[r["startnumber"]]
+
 @step(u'way (\d+) expands exactly to housenumbers ([0-9,]*)')
 def check_interpolated_housenumber_list(step, nodeid, numberlist):
     """ Checks that the interpolated house numbers corresponds
@@ -149,7 +193,7 @@ def check_interpolated_housenumber_list(step, nodeid, numberlist):
     cur = world.conn.cursor()
     cur.execute("""SELECT housenumber FROM placex
                    WHERE osm_type = 'W' and osm_id = %s
-                     and class = 'place' and type = 'address'""", (int(nodeid),))
+                   and class = 'place' and type = 'address'""", (int(nodeid),))
     for r in cur:
         assert_in(r[0], expected, "Unexpected house number %s for node %s." % (r[0], nodeid))
         expected.remove(r[0])
index 23d526f87847c756268c5ffcdf2f4baf42b161b2..727e6105bb0d916a219c3f163252efbf30b59190 100644 (file)
@@ -23,6 +23,7 @@ import os
 import subprocess
 import random
 import base64
+import sys
 
 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
 
@@ -143,8 +144,8 @@ def import_set_scene(step, scene):
 
 @step(u'the (named )?place (node|way|area)s')
 def import_place_table_nodes(step, named, osmtype):
-    """Insert a list of nodes into the placex table.
-       Expects a table where columns are named in the same way as placex.
+    """Insert a list of nodes into the place table.
+       Expects a table where columns are named in the same way as place.
     """
     cur = world.conn.cursor()
     cur.execute('ALTER TABLE place DISABLE TRIGGER place_before_insert')
@@ -214,18 +215,19 @@ def import_database(step):
     """ Runs the actual indexing. """
     world.run_nominatim_script('setup', 'create-functions', 'create-partition-functions')
     cur = world.conn.cursor()
+    #world.db_dump_table('place')
     cur.execute("""insert into placex (osm_type, osm_id, class, type, name, admin_level,
                    housenumber, street, addr_place, isin, postcode, country_code, extratags,
-                   geometry) select * from place""")
+                   geometry) select * from place where not (class='place' and type='houses' and osm_type='W')""")
+    cur.execute("""select insert_osmline (osm_id, housenumber, street, addr_place, postcode, country_code, geometry) from place where class='place' and type='houses' and osm_type='W'""")
     world.conn.commit()
     world.run_nominatim_script('setup', 'index', 'index-noanalyse')
     #world.db_dump_table('placex')
-
+    #world.db_dump_table('location_property_osmline')
 
 @step(u'updating place (node|way|area)s')
 def update_place_table_nodes(step, osmtype):
-    """ Replace a geometry in place by reinsertion and reindex database.
-    """
+    """ Replace a geometry in place by reinsertion and reindex database."""
     world.run_nominatim_script('setup', 'create-functions', 'create-partition-functions', 'enable-diff-updates')
     if osmtype == 'node':
         _insert_place_table_nodes(step.hashes, False)
index 0a4495e01217b8ef3c36cb0275d04dcc9d294849..80beebd5a49c8bb67b97268bd19ab7c4c7b6585f 100644 (file)
@@ -123,16 +123,19 @@ def match_geometry(coord, matchstring):
     logger.debug("Distances expected: %f, got: %f" % (expdist, dist))
     assert dist <= expdist, "Geometry too far away, expected: %f, got: %f" % (expdist, dist)
 
+@world.absorb
+def print_statement(element):
+    print '\n\n\n'+str(element)+'\n\n\n'
 
 
 @world.absorb
 def db_dump_table(table):
     cur = world.conn.cursor()
     cur.execute('SELECT * FROM %s' % table)
-    print '<<<<<<< BEGIN OF TABLE DUMP %s' % table
+    print '\n\n\n<<<<<<< BEGIN OF TABLE DUMP %s' % table
     for res in cur:
             print res
-    print '<<<<<<< END OF TABLE DUMP %s' % table
+    print '<<<<<<< END OF TABLE DUMP %s\n\n\n' % table
 
 @world.absorb
 def db_drop_database(name):
index 01402842e8bda9aa560a0709016ae07646c4f352..b9045e7050f7b3afef5ca22b508de10d50340132 100755 (executable)
                echo '.';
                if (!pg_query($oDB->connection, 'TRUNCATE placex')) fail(pg_last_error($oDB->connection));
                echo '.';
+               if (!pg_query($oDB->connection, 'TRUNCATE location_property_osmline')) fail(pg_last_error($oDB->connection));
+               echo '.';
                if (!pg_query($oDB->connection, 'TRUNCATE place_addressline')) fail(pg_last_error($oDB->connection));
                echo '.';
                if (!pg_query($oDB->connection, 'TRUNCATE place_boundingbox')) fail(pg_last_error($oDB->connection));
                for($i = 0; $i < $iInstances; $i++)
                {
                        $aDBInstances[$i] =& getDB(true);
-                       $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
-                       $sSQL .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
-                       $sSQL .= 'geometry) select * from place where osm_id % '.$iInstances.' = '.$i;
+                       if( $i < $iInstances-1 )
+                       {
+                               $sSQL = 'insert into placex (osm_type, osm_id, class, type, name, admin_level, ';
+                               $sSQL .= 'housenumber, street, addr_place, isin, postcode, country_code, extratags, ';
+                               $sSQL .= 'geometry) select * from place where osm_id % '.$iInstances-1.' = '.$i;
+                       }
+                       else
+                       {
+                               // last thread for interpolation lines
+                               $sSQL = 'select insert_osmline (osm_id, housenumber, street, addr_place, postcode, country_code, ';
+                               $sSQL .= 'geometry) from place where ';
+                               $sSQL .= 'class=\'place\' and type=\'houses\' and osm_type=\'W\' and ST_GeometryType(geometry) = \'ST_LineString\'';
+                       }
                        if ($aCMDResult['verbose']) echo "$sSQL\n";
                        if (!pg_send_query($aDBInstances[$i]->connection, $sSQL)) fail(pg_last_error($oDB->connection));
                }