banniere

Le portail francophone de la géomatique


Toujours pas inscrit ? Mot de passe oublié ?
Nom d'utilisateur    Mot de passe              Toujours pas inscrit ?   Mot de passe oublié ?

Annonce

GEODATA DAYS 2024

#1 Mon 24 August 2009 15:07

nicolas.degarne
Participant occasionnel
Date d'inscription: 18 Apr 2008
Messages: 14

Ouverture fichier shape file "*.shp" avec OGR

Bonjour,

Je voudrais récupérer les données contenues dans  Shapefile avec OGR.

J'ai essayé en utilisant  le tutoriel fourni par le site internet de GDAL/OGR mais il affiche une erreur

Code:

ERROR 10: Pointer 'hLayer' is NULL in 'OGR_L_ResetReading'.

ERROR 10: Pointer 'hLayer' is NULL in 'OGR_L_GetNextFeature'.

Sachant que le code que j'utilise est :

Code:

#include "ogr_api.h"

int main()
{
    OGRRegisterAll();

    OGRDataSourceH hDS;
    OGRLayerH hLayer;
    OGRFeatureH hFeature;

    hDS = OGROpen( "001.shp", FALSE, NULL );
    if( hDS == NULL )
    {
        printf( "Open failed.\n" );
        exit( 1 );
    }

    hLayer = OGR_DS_GetLayerByName( hDS, "point" );

    OGR_L_ResetReading(hLayer);
    while( (hFeature = OGR_L_GetNextFeature(hLayer)) != NULL )
    {
        OGRFeatureDefnH hFDefn;
        int iField;
        OGRGeometryH hGeometry;

        hFDefn = OGR_L_GetLayerDefn(hLayer);

        for( iField = 0; iField < OGR_FD_GetFieldCount(hFDefn); iField++ )
        {
            OGRFieldDefnH hFieldDefn = OGR_FD_GetFieldDefn( hFDefn, iField );

            if( OGR_Fld_GetType(hFieldDefn) == OFTInteger )
                printf( "%d,", OGR_F_GetFieldAsInteger( hFeature, iField ) );
            else if( OGR_Fld_GetType(hFieldDefn) == OFTReal )
                printf( "%.3f,", OGR_F_GetFieldAsDouble( hFeature, iField) );
            else if( OGR_Fld_GetType(hFieldDefn) == OFTString )
                printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) );
            else
                printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) );
        }

        hGeometry = OGR_F_GetGeometryRef(hFeature);
        if( hGeometry != NULL
            && wkbFlatten(OGR_G_GetGeometryType(hGeometry)) == wkbPoint )
        {
            printf( "%.3f,%3.f\n", OGR_G_GetX(hGeometry, 0), OGR_G_GetY(hGeometry, 0) );
        }
        else
        {
            printf( "no point geometry\n" );
        }

        OGR_F_Destroy( hFeature );
    }

    OGR_DS_Destroy( hDS );
    return 0;
}

Si quelqu'un a une idée sa serait sympa sachant que mes fichiers .shp ont été crées par OGR.

Bonne journée et merci d'avance

niko

Hors ligne

 

#2 Tue 25 August 2009 00:13

rouault
Participant assidu
Date d'inscription: 26 Apr 2009
Messages: 166

Re: Ouverture fichier shape file "*.shp" avec OGR

Eheh, c'est pas bien de recopier les tutoriaux sans les adapter à son besoin wink

Le problème est la ligne :

Code:

hLayer = OGR_DS_GetLayerByName( hDS, "point" );

qui vu les messages d'erreur que tu as retourne un pointeur NULL.

La raison ? Quand tu as un shapefile qui s'appelle foo.shp, il contient un unique layer appelé "foo". Donc dans ton cas, il faut que tu demandes "001".

Autre solution qui marcherait avec n'importe quel shapefile, demander le layer par son index (0 pour le 1er layer, 1 pour le 2ème, etc...) et non pas par son nom.

Code:

hLayer = OGR_DS_GetLayer( hDS, 0 );

Ensuite il faudra évidemment que tu adaptes la boucle de récupération des features si la géométrie de ton shapefile n'est pas des points comme dans l'exemple du tutoriel

Hors ligne

 

#3 Tue 25 August 2009 15:06

nicolas.degarne
Participant occasionnel
Date d'inscription: 18 Apr 2008
Messages: 14

Re: Ouverture fichier shape file "*.shp" avec OGR

Oui, je sait que c'est pas bien  sad

Je m'en était rendu compte j'ai utilisé la solution de remplacer

Code:

hLayer = OGR_DS_GetLayerByName( hDS, "point" );

par:

Code:

hLayer = OGR_DS_GetLayerByName( hDS, "001" );

et sa marche très bien!

Dans mon raisonnement "point" représentait le types de données et pas le nom du fichier d'où mon problème de compréhension.

En tout cas merci pour ta réponse.

Bonne journée

Hors ligne

 

Pied de page des forums

Powered by FluxBB