Skip to content

Commit

Permalink
Add support for a -E|--proj option which allows users to use any epsg
Browse files Browse the repository at this point in the history
projection that be used by proj4's +init=epsg:<num> format.

Should help all those people wanting to do maps in different projections.


git-svn-id: http://svn.openstreetmap.org/applications/utils/export/osm2pgsql@7141 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
  • Loading branch information
martinvoosterhout committed Mar 22, 2008
1 parent 2c820b8 commit 0f9ae55
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.txt
Expand Up @@ -114,6 +114,12 @@ step, but don't forget to change the Mapnik config to match.
Combining the -v and -h switches will tell about the exact definitions of
the projections.

In case you want to use some completely different projection there is the -E
option. It will initialise the projection as +init=epsg:<num>. This allows
you to use any projection recognised by proj4, which is useful if you want
to make a map in a different projection. These projections are usually
defined in /usr/share/proj/epsg.

Database Access Examples
========================
If you wish to access the data from the database then the
Expand Down
25 changes: 13 additions & 12 deletions osm2pgsql.c
Expand Up @@ -330,6 +330,7 @@ static void usage(const char *arg0)
fprintf(stderr, " \tto (default: gis).\n");
fprintf(stderr, " -l|--latlong\t\tStore data in degrees of latitude & longitude.\n");
fprintf(stderr, " -m|--merc\t\tStore data in proper spherical mercator, not OSM merc\n");
fprintf(stderr, " -E|--proj num\tUse projection EPSG:num\n");
fprintf(stderr, " -u|--utf8-sanitize\tRepair bad UTF8 input data (present in planet\n");
fprintf(stderr, " \tdumps prior to August 2007). Adds about 10%% overhead.\n");
fprintf(stderr, " -p|--prefix\t\tPrefix for table names (default planet_osm)\n");
Expand All @@ -345,7 +346,10 @@ static void usage(const char *arg0)
fprintf(stderr, " -v|--verbose\t\tVerbose output.\n");
fprintf(stderr, "\n");
if(!verbose)
fprintf(stderr, "Add -v to display supported projections.\n" );
{
fprintf(stderr, "Add -v to display supported projections.\n");
fprintf(stderr, "Use -E to access any espg projections (usually in /usr/share/proj/epsg)\n" );
}
else
{
fprintf(stderr, "Supported projections:\n" );
Expand Down Expand Up @@ -397,7 +401,7 @@ int main(int argc, char *argv[])
int slim=0;
int sanitize=0;
int pass_prompt=0;
int latlong = 0, sphere_merc = 0;
int projection = PROJ_MERC;
const char *db = "gis";
const char *username=NULL;
const char *host=NULL;
Expand All @@ -422,6 +426,7 @@ int main(int argc, char *argv[])
{"slim", 0, 0, 's'},
#endif
{"prefix", 1, 0, 'p'},
{"proj", 1, 0, 'E'},
{"merc", 0, 0, 'm'},
{"utf8-sanitize", 0, 0, 'u'},
{"username", 1, 0, 'U'},
Expand All @@ -432,7 +437,7 @@ int main(int argc, char *argv[])
{0, 0, 0, 0}
};

c = getopt_long (argc, argv, "ab:cd:hlmp:suvU:WH:P:", long_options, &option_index);
c = getopt_long (argc, argv, "ab:cd:hlmp:suvU:WH:P:E:", long_options, &option_index);
if (c == -1)
break;

Expand All @@ -445,8 +450,9 @@ int main(int argc, char *argv[])
case 's': slim=1; break;
#endif
case 'u': sanitize=1; break;
case 'l': latlong=1; break;
case 'm': sphere_merc=1; break;
case 'l': projection=PROJ_LATLONG; break;
case 'm': projection=PROJ_SPHERE_MERC; break;
case 'E': projection=-atoi(optarg); break;
case 'p': prefix=optarg; break;
case 'd': db=optarg; break;
case 'U': username=optarg; break;
Expand Down Expand Up @@ -494,12 +500,7 @@ int main(int argc, char *argv[])

LIBXML_TEST_VERSION

if( latlong && sphere_merc )
{
fprintf(stderr, "Error: --latlong and --merc are mutually exclusive\n" );
exit(EXIT_FAILURE);
}
project_init(latlong ? PROJ_LATLONG : sphere_merc ? PROJ_SPHERE_MERC : PROJ_MERC );
project_init(projection);
fprintf(stderr, "Using projection SRS %d (%s)\n",
project_getprojinfo()->srs, project_getprojinfo()->descr );

Expand All @@ -513,7 +514,7 @@ int main(int argc, char *argv[])

while (optind < argc) {
fprintf(stderr, "\nReading in file: %s\n", argv[optind]);
mid->start(conninfo, latlong);
mid->start(conninfo, projection==PROJ_LATLONG);
if (streamFile(argv[optind], sanitize) != 0)
exit_nicely();
mid->end();
Expand Down
36 changes: 32 additions & 4 deletions reprojection.c
Expand Up @@ -6,13 +6,14 @@
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <proj_api.h>

#include "reprojection.h"

static projPJ pj_ll, pj_merc;
static enum Projection Proj;
static int Proj;
const struct Projection_Info Projection_Info[] = {
[PROJ_LATLONG] = {
descr: "Latlong",
Expand All @@ -30,21 +31,45 @@ const struct Projection_Info Projection_Info[] = {
srs:900913,
option: "-m" }
};
static struct Projection_Info custom_projection;

void project_init(enum Projection proj)
// Positive numbers refer the to the table above, negative numbers are
// assumed to refer to EPSG codes and it uses the proj4 to find those.
void project_init(int proj)
{
char buffer[16];
Proj = proj;

if( proj == PROJ_LATLONG )
return;

pj_ll = pj_init_plus("+proj=latlong +ellps=GRS80 +no_defs");
pj_merc = pj_init_plus( Projection_Info[proj].proj4text );
if( proj >= 0 && proj < PROJ_COUNT )
pj_merc = pj_init_plus( Projection_Info[proj].proj4text );
else if( proj < 0 )
{
sprintf( buffer, "+init=epsg:%d", -proj );
pj_merc = pj_init_plus( buffer );
if( !pj_merc )
{
fprintf( stderr, "Couldn't read EPSG definition (do you have /usr/share/proj/epsg?)\n" );
exit(1);
}
}

if (!pj_ll || !pj_merc) {
fprintf(stderr, "Projection code failed to initialise\n");
exit(1);
}

if( proj >= 0 )
return;
custom_projection.srs = -proj;
custom_projection.proj4text = pj_get_def( pj_merc, 0 );
sprintf( buffer, "EPSG:%d", -proj );
custom_projection.descr = strdup(buffer);
custom_projection.option = "-E";
return;
}

void project_exit(void)
Expand All @@ -60,7 +85,10 @@ void project_exit(void)

struct Projection_Info const *project_getprojinfo(void)
{
return &Projection_Info[Proj];
if( Proj >= 0 )
return &Projection_Info[Proj];
else
return &custom_projection;
}

void reproject(double *lat, double *lon)
Expand Down
2 changes: 1 addition & 1 deletion reprojection.h
Expand Up @@ -16,7 +16,7 @@ struct Projection_Info {
};

enum Projection { PROJ_LATLONG = 0, PROJ_MERC, PROJ_SPHERE_MERC, PROJ_COUNT };
void project_init(enum Projection);
void project_init(int);
void project_exit(void);
struct Projection_Info const* project_getprojinfo(void);
void reproject(double *lat, double *lon);
Expand Down

0 comments on commit 0f9ae55

Please sign in to comment.