[gdal-dev] Setting the spatial reference of an existing shapefile with OGR

Jason Roberts jason.roberts at duke.edu
Thu Nov 12 19:53:53 EST 2009


> Yes. Or... you could cheat a bit. In the case of a shapefile, the spatial
> reference is written in a .prj file. So technically you can create a fake
empty
> shapefile with the right spatial reference, close it, rename the .prj to
have
> the same basename as your shapefile of interes and re-open it.

Ah, excellent. Thanks for this suggestion!

It looks like the .prj file simply contains the WKT. Would it be sufficient
to just call OGRSpatialReference::exportToWKT and write the string to the
.prj file directly? In my situation that would be slightly cleaner. If this
is a good approach, should I also call morphToESRI first, if I know the
coordinate system has not already been morphed to ESRI?

Thanks,
Jason

-----Original Message-----
From: Even Rouault [mailto:even.rouault at mines-paris.org] 
Sent: Thursday, November 12, 2009 7:43 PM
To: Jason Roberts
Cc: 'gdal-dev'
Subject: Re: [gdal-dev] Setting the spatial reference of an existing
shapefile with OGR

Selon Jason Roberts <jason.roberts at duke.edu>:

> Greetings OGR experts,
>
>
>
> I have a shapefile for which no spatial reference was defined when the
> shapefile was created, but I know the spatial reference and would like to
> set it on this existing shapefile. Is this possible with the OGR Python
API?
>

You're correct. There's no direct (Python or C) API for this.

>
> It looks like the only place that takes an OGRSpatialReference instance is
> OGRDataSource::CreateLayer. This suggests that the only way to do it is to
> call CreateLayer with the appropriate OGRSpatialReference instance and
then
> write all of the code to create the fields and copy all of the features
from
> the existing layer to the new one. Is this correct?
>

Yes. Or... you could cheat a bit. In the case of a shapefile, the spatial
reference is written in a .prj file. So technically you can create a fake
empty
shapefile with the right spatial reference, close it, rename the .prj to
have
the same basename as your shapefile of interes and re-open it.

>
>
> Thanks very much for any suggestions you can provide,
>
>
>
> Jason
>
>
>
>





More information about the gdal-dev mailing list