Changeset 11992

Show
Ignore:
Timestamp:
08/29/07 10:07:30 (1 year ago)
Author:
pka
Message:

Fix for area polygonisation with crossing lines (curves) as described in #1613

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/ogr/ogrsf_frmts/ili/ili1reader.cpp

    r11344 r11992  
    8686    if( fpItf == NULL ) 
    8787    { 
    88           CPLError( CE_Failure, CPLE_OpenFailed,  
    89                     "Failed to open ILI file `%s'.",  
     88          CPLError( CE_Failure, CPLE_OpenFailed, 
     89                    "Failed to open ILI file `%s'.", 
    9090                    pszFilename ); 
    9191 
     
    181181  iom_init(); 
    182182 
    183   // set error listener to a iom provided one, that just  
     183  // set error listener to a iom provided one, that just 
    184184  // dumps all errors to stderr 
    185185  iom_seterrlistener(iom_stderrlistener); 
     
    270270    const char *topic = NULL; 
    271271    int ret = TRUE; 
    272      
     272 
    273273    while (ret && (tokens = ReadParseLine())) 
    274274    { 
     
    277277      { 
    278278        //read description 
    279         do  
     279        do 
    280280        { 
    281281          pszLine = CPLReadLine( fpItf ); 
     
    287287      { 
    288288        //read model 
    289         do  
     289        do 
    290290        { 
    291291          pszLine = CPLReadLine( fpItf ); 
     
    338338        CPLDebug( "OGR_ILI", "Unexpected token: %s", firsttok ); 
    339339      } 
    340        
     340 
    341341      CSLDestroy(tokens); 
    342342    } 
     
    366366} 
    367367 
    368 OGRMultiPolygon* ILI1Reader::Polygonize( OGRGeometryCollection* poLines
     368OGRMultiPolygon* ILI1Reader::Polygonize( OGRGeometryCollection* poLines, bool fix_crossing_lines
    369369{ 
    370370    OGRMultiPolygon *poPolygon = new OGRMultiPolygon(); 
     371 
     372    if (poLines->getNumGeometries() == 0) return poPolygon; 
    371373 
    372374#if defined(POLYGONIZE_AREAS) 
    373375    GEOSGeom *ahInGeoms = NULL; 
    374376    int       i = 0; 
    375      
    376     ahInGeoms = (GEOSGeom *) CPLCalloc(sizeof(void*),poLines->getNumGeometries()); 
    377     for( i = 0; i < poLines->getNumGeometries(); i++ ) 
    378         ahInGeoms[i] = poLines->getGeometryRef(i)->exportToGEOS(); 
    379  
    380      
    381     GEOSGeom hResultGeom = GEOSPolygonize( ahInGeoms,  
    382                                            poLines->getNumGeometries() ); 
    383  
    384     for( i = 0; i < poLines->getNumGeometries(); i++ ) 
     377    OGRGeometryCollection *poNoncrossingLines = poLines; 
     378    GEOSGeom hResultGeom = NULL; 
     379    OGRGeometry *poMP = NULL; 
     380 
     381    if (fix_crossing_lines) 
     382    { 
     383        CPLDebug( "OGR_ILI", "Fixing crossing lines"); 
     384        poNoncrossingLines = (OGRGeometryCollection*)poLines->Union(poLines->getGeometryRef(0)); 
     385        CPLDebug( "OGR_ILI", "Fixed lines: %d", poNoncrossingLines->getNumGeometries()-poLines->getNumGeometries()); 
     386    } 
     387 
     388    ahInGeoms = (GEOSGeom *) CPLCalloc(sizeof(void*),poNoncrossingLines->getNumGeometries()); 
     389    for( i = 0; i < poNoncrossingLines->getNumGeometries(); i++ ) 
     390          ahInGeoms[i] = poNoncrossingLines->getGeometryRef(i)->exportToGEOS(); 
     391 
     392    hResultGeom = GEOSPolygonize( ahInGeoms, 
     393                                  poNoncrossingLines->getNumGeometries() ); 
     394 
     395    for( i = 0; i < poNoncrossingLines->getNumGeometries(); i++ ) 
    385396        GEOSGeom_destroy( ahInGeoms[i] ); 
    386397    CPLFree( ahInGeoms ); 
     398    if (poNoncrossingLines != poLines) delete poNoncrossingLines; 
    387399 
    388400    if( hResultGeom == NULL ) 
    389401        return NULL; 
    390402 
    391     OGRGeometry *poMP = OGRGeometryFactory::createFromGEOS( hResultGeom ); 
    392      
     403    poMP = OGRGeometryFactory::createFromGEOS( hResultGeom ); 
     404 
    393405    GEOSGeom_destroy( hResultGeom ); 
    394406 
     
    417429      CPLDebug( "OGR_ILI", "Polygonizing layer %s with %d multilines", poAreaLayer->GetLayerDefn()->GetName(), gc->getNumGeometries()); 
    418430      OGRMultiPolygon* polys = Polygonize( gc ); 
     431      if (polys->getNumGeometries() != poAreaLayer->GetFeatureCount()) 
     432      { 
     433          delete polys; 
     434          polys = Polygonize( gc, true ); //try again with crossing line fix 
     435      } 
     436      delete gc; 
    419437 
    420438      //associate polygon feature with data row according to centroid 
     
    481499 
    482500int ILI1Reader::ReadTable() { 
    483      
     501 
    484502    char **tokens = NULL; 
    485503    const char *firsttok = NULL; 
     
    531549            } 
    532550          } 
    533    
     551 
    534552          int fieldno = 0; 
    535553          for (fIndex=1; fIndex<CSLCount(tokens) && fieldno < featureDef->GetFieldCount(); fIndex++, fieldno++) 
     
    592610        CPLDebug( "OGR_ILI", "Unexpected token: %s", firsttok ); 
    593611      } 
    594        
     612 
    595613      CSLDestroy(tokens); 
    596614      fpos = VSIFTell(fpItf); 
    597615    } 
    598      
     616 
    599617    return ret; 
    600618} 
    601619 
    602620void ILI1Reader::ReadGeom(char **stgeom, OGRwkbGeometryType eType, OGRFeature *feature) { 
    603      
     621 
    604622    char **tokens = NULL; 
    605623    const char *firsttok = NULL; 
     
    697715        CPLDebug( "OGR_ILI", "Unexpected token: %s", firsttok ); 
    698716      } 
    699      
     717 
    700718      CSLDestroy(tokens); 
    701719    } 
     
    711729    papoLayers = (OGRILI1Layer **) 
    712730        CPLRealloc( papoLayers, sizeof(void*) * ++nLayers ); 
    713      
     731 
    714732    papoLayers[nLayers-1] = poNewLayer; 
    715733} 
     
    728746    papoAreaLineLayers = (OGRILI1Layer **) 
    729747        CPLRealloc( papoAreaLineLayers, sizeof(void*) * nAreaLayers ); 
    730      
     748 
    731749    papoAreaLayers[nAreaLayers-1] = poAreaLayer; 
    732750    papoAreaLineLayers[nAreaLayers-1] = poLineLayer; 
     
    746764    papoSurfacePolyLayers = (OGRILI1Layer **) 
    747765        CPLRealloc( papoSurfacePolyLayers, sizeof(void*) * nSurfaceLayers ); 
    748      
     766 
    749767    papoSurfaceLayers[nSurfaceLayers-1] = poDataLayer; 
    750768    papoSurfacePolyLayers[nSurfaceLayers-1] = poPolyLayer; 
     
    801819    if( fpItf == NULL ) 
    802820        return( NULL ); 
    803      
     821 
    804822    pszLine = CPLReadLine( fpItf ); 
    805823    if( pszLine == NULL ) 
    806824        return( NULL ); 
    807      
     825 
    808826    if (strlen(pszLine) == 0) return NULL; 
    809        
     827 
    810828    tokens = CSLTokenizeString2( pszLine, " ", CSLT_PRESERVEESCAPES ); 
    811829    token = tokens[CSLCount(tokens)-1]; 
    812      
     830 
    813831    //Append CONT lines 
    814832    while (strlen(pszLine) && EQUALN(token, "\\", 2)) 
     
    817835      CPLFree(tokens[CSLCount(tokens)-1]); 
    818836      tokens[CSLCount(tokens)-1] = NULL; 
    819        
     837 
    820838      pszLine = CPLReadLine( fpItf ); 
    821839      conttok = CSLTokenizeString2( pszLine, " ", CSLT_PRESERVEESCAPES ); 
    822840      if (!conttok || !EQUAL(conttok[0], "CONT")) break; 
    823        
     841 
    824842      //append 
    825843      tokens = CSLInsertStrings(tokens, -1, &conttok[1]); 
    826844      token = tokens[CSLCount(tokens)-1]; 
    827        
     845 
    828846      CSLDestroy(conttok); 
    829847    } 
  • trunk/gdal/ogr/ogrsf_frmts/ili/ili1readerp.h

    r11343 r11992  
    1515 * and/or sell copies of the Software, and to permit persons to whom the 
    1616 * Software is furnished to do so, subject to the following conditions: 
    17  *  
     17 * 
    1818 * The above copyright notice and this permission notice shall be included 
    1919 * in all copies or substantial portions of the Software. 
    20  *  
     20 * 
    2121 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    2222 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
     
    2424 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    2525 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
    26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER  
     26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
    2727 * DEALINGS IN THE SOFTWARE. 
    2828 ****************************************************************************/ 
     
    4444 
    4545class ILI1Reader : public IILI1Reader 
    46 {     
     46{ 
    4747private: 
    4848    FILE         *fpItf; 
     
    7474    void         AddSurfaceLayer( OGRILI1Layer * poDataLayer,  OGRILI1Layer * poPolyLayer ); 
    7575    int          AddIliGeom(OGRFeature *feature, int iField, long fpos); 
    76     OGRMultiPolygon* Polygonize( OGRGeometryCollection* poLines ); 
     76    OGRMultiPolygon* Polygonize( OGRGeometryCollection* poLines, bool fix_crossing_lines = false ); 
    7777    void         PolygonizeAreaLayers(); 
    7878    void         JoinSurfaceLayers();