Beispiel #1
0
char *msProjectionObj2OGCWKT( projectionObj *projection )

{

#if !defined(USE_GDAL) && !defined(USE_OGR)
  msSetError(MS_OGRERR,
             "Not implemented since neither OGR nor GDAL is enabled.",
             "msProjectionObj2OGCWKT()");
  return NULL;

#else /* defined USE_GDAL or USE_OGR */

  OGRSpatialReferenceH hSRS;
  char *pszWKT=NULL, *pszProj4;
  int  nLength = 0, i;
  OGRErr eErr;

  if( projection->proj == NULL )
    return NULL;

  /* -------------------------------------------------------------------- */
  /*      Form arguments into a full Proj.4 definition string.            */
  /* -------------------------------------------------------------------- */
  for( i = 0; i < projection->numargs; i++ )
    nLength += strlen(projection->args[i]) + 2;

  pszProj4 = (char *) CPLMalloc(nLength+2);
  pszProj4[0] = '\0';

  for( i = 0; i < projection->numargs; i++ ) {
    strcat( pszProj4, "+" );
    strcat( pszProj4, projection->args[i] );
    strcat( pszProj4, " " );
  }

  /* -------------------------------------------------------------------- */
  /*      Ingest the string into OGRSpatialReference.                     */
  /* -------------------------------------------------------------------- */
  hSRS = OSRNewSpatialReference( NULL );
  eErr =  OSRImportFromProj4( hSRS, pszProj4 );
  CPLFree( pszProj4 );

  /* -------------------------------------------------------------------- */
  /*      Export as a WKT string.                                         */
  /* -------------------------------------------------------------------- */
  if( eErr == OGRERR_NONE )
    eErr = OSRExportToWkt( hSRS, &pszWKT );

  OSRDestroySpatialReference( hSRS );

  if( pszWKT ) {
    char *pszWKT2 = msStrdup(pszWKT);
    CPLFree( pszWKT );

    return pszWKT2;
  } else
    return NULL;
#endif /* defined USE_GDAL or USE_OGR */
}
void QgsCoordinateReferenceSystem::setProj4String( QString theProj4String )
{
  const char *oldlocale = setlocale( LC_NUMERIC, NULL );

  setlocale( LC_NUMERIC, "C" );
  OSRDestroySpatialReference( mCRS );
  mCRS = OSRNewSpatialReference( NULL );
  mIsValidFlag = OSRImportFromProj4( mCRS, theProj4String.toLatin1().constData() ) == OGRERR_NONE;
  setMapUnits();

#if defined(QGISDEBUG) && QGISDEBUG>=3
  debugPrint();
#endif

  setlocale( LC_NUMERIC, oldlocale );
}
    void object::test<2>()
    {
        ensure("SRS handle is NULL", NULL != srs_);

        std::string wkt("+proj=lcc +x_0=0.6096012192024384e+06 +y_0=0"
            "+lon_0=90dw +lat_0=42dn +lat_1=44d4'n +lat_2=42d44'n"
            "+a=6378206.400000 +rf=294.978698 +nadgrids=conus,ntv1_can.dat");

        err_ = OSRImportFromProj4(srs_, wkt.c_str());
        ensure_equals("OSRImportFromProj4)( failed", err_, OGRERR_NONE);

        const double maxError = 0.0005;
        double val = 0;

        val = OSRGetProjParm(srs_, SRS_PP_FALSE_EASTING, -1111, &err_);
        ensure_equals("OSRGetProjParm() failed", err_, OGRERR_NONE);
        ensure("Parsing exponents not supported",
               std::fabs(val - 609601.219) <= maxError);
    }
Beispiel #4
0
    void object::test<4>()
    {
        ensure("SRS handle is NULL", nullptr != srs_);

        err_ = OSRSetGS(srs_, -117.0, 100000.0, 100000);
        ensure_equals("OSRSetGS failed", err_, OGRERR_NONE);

        err_ = OSRSetGeogCS(srs_, "Test GCS", "Test Datum", "WGS84",
            SRS_WGS84_SEMIMAJOR, SRS_WGS84_INVFLATTENING,
            nullptr, 0, nullptr, 0);
        ensure_equals("OSRSetGeogCS failed", err_, OGRERR_NONE);

        err_ = OSRSetTOWGS84(srs_, 1, 2, 3, 0, 0, 0, 0);
        ensure_equals("OSRSetTOWGS84 failed", err_, OGRERR_NONE);

        const int coeffSize = 7;
        double coeff[coeffSize] = { 0 };
        const double expect[coeffSize] = { 1, 2, 3, 0, 0, 0, 0 };

        err_ = OSRGetTOWGS84(srs_, coeff, 7);
        ensure_equals("OSRSetTOWGS84 failed", err_, OGRERR_NONE);
        ensure("GetTOWGS84 result is wrong",
            std::equal(coeff, coeff + coeffSize, expect));
        OSRSetLinearUnits(srs_, "Metre", 1);

        char* proj4 = nullptr;
        err_ = OSRExportToProj4(srs_, &proj4);
        ensure_equals("OSRExportToProj4 failed", err_, OGRERR_NONE);

        OGRSpatialReferenceH srs2 = nullptr;
        srs2 = OSRNewSpatialReference(nullptr);

        err_ = OSRImportFromProj4(srs2, proj4);
        ensure_equals("OSRImportFromProj4 failed", err_, OGRERR_NONE);

        err_ = OSRGetTOWGS84(srs2, coeff, 7);
        ensure_equals("OSRSetTOWGS84 failed", err_, OGRERR_NONE);
        ensure("GetTOWGS84 result is wrong",
            std::equal(coeff, coeff + coeffSize, expect));

        OSRDestroySpatialReference(srs2);
        CPLFree(proj4);
    }
    void object::test<1>()
    {
        ensure("SRS handle is NULL", NULL != srs_);

        std::string wkt("+proj=tmerc +lat_0=53.5000000000 +lon_0=-8.0000000000"
              "+k_0=1.0000350000 +x_0=200000.0000000000 +y_0=250000.0000000000"
              "+a=6377340.189000 +rf=299.324965 +towgs84=482.530,"
              "-130.596,564.557,-1.042,-0.214,-0.631,8.15");

        err_ = OSRImportFromProj4(srs_, wkt.c_str());
        ensure_equals("OSRImportFromProj4)( failed", err_, OGRERR_NONE);

        // TODO: Check max error value
        const double maxError = 0.00005; // 0.0000005
        double val = 0;

        val = OSRGetProjParm(srs_, SRS_PP_SCALE_FACTOR, -1111, &err_);
        ensure_equals("OSRGetProjParm() failed", err_, OGRERR_NONE);

        ensure("+k_0 not supported on import from PROJ.4",
               std::fabs(val - 1.000035) <= maxError);
    }
Beispiel #6
0
OgrFileImport::OgrFileImport(QIODevice* stream, Map* map, MapView* view, bool drawing_from_projected)
 : Importer(stream, map, view)
 , map_srs{ OSRNewSpatialReference(nullptr) }
 , manager{ OGR_SM_Create(nullptr) }
 , drawing_from_projected{ drawing_from_projected }
{
	GdalManager().configure();
	
	setOption(QLatin1String{ "Separate layers" }, QVariant{ false });
	
	auto spec = QByteArray::fromRawData("WGS84", 6);
	auto error = OSRSetWellKnownGeogCS(map_srs.get(), spec);
	if (!map->getGeoreferencing().isLocal() && !error)
	{
		spec = map->getGeoreferencing().getProjectedCRSSpec().toLatin1();
		error = OSRImportFromProj4(map_srs.get(), spec);
	}
	
	if (error)
	{
		addWarning(tr("Unable to setup \"%1\" SRS for GDAL: %2")
		           .arg(QString::fromLatin1(spec), QString::number(error)));
	}
	
	// Reasonable default?
	
	// OGR feature style defaults
	default_pen_color = new MapColor("Black", 0); 
	default_pen_color->setRgb({0.0, 0.0, 0.0});
	default_pen_color->setCmykFromRgb();
	map->addColor(default_pen_color, 0);
	
	auto default_brush_color = new MapColor("Black 50%", 0);
	default_brush_color->setRgb({0.5, 0.5, 0.5});
	default_brush_color->setCmykFromRgb();
	map->addColor(default_brush_color, 1);
	
	default_point_symbol = new PointSymbol();
	default_point_symbol->setName(tr("Point"));
	default_point_symbol->setNumberComponent(0, 1);
	default_point_symbol->setInnerColor(default_pen_color);
	map->addSymbol(default_point_symbol, 0);
	
	default_line_symbol = new LineSymbol();
	default_line_symbol->setName(tr("Line"));
	default_line_symbol->setNumberComponent(0, 2);
	default_line_symbol->setColor(default_pen_color);
	default_line_symbol->setLineWidth(0.1); // (0.1 mm, nearly cosmetic)
	default_line_symbol->setCapStyle(LineSymbol::FlatCap);
	default_line_symbol->setJoinStyle(LineSymbol::MiterJoin);
	map->addSymbol(default_line_symbol, 1);
	
	default_area_symbol = new AreaSymbol();
	default_area_symbol->setName(tr("Area"));
	default_area_symbol->setNumberComponent(0, 3);
	default_area_symbol->setColor(default_brush_color);
	map->addSymbol(default_area_symbol, 2);
	
	default_text_symbol = new TextSymbol();
	default_text_symbol->setName(tr("Text"));
	default_text_symbol->setNumberComponent(0, 4);
	default_text_symbol->setColor(default_pen_color);
	map->addSymbol(default_text_symbol, 3);
}
Beispiel #7
0
void QgsGrassNewMapset::setGrassProjection()
{
    QgsDebugMsg( "entered." );
    setError( mProjErrorLabel, "" );

    QString proj4 = mProjectionSelector->selectedProj4String();

    // Not defined
    if ( mNoProjRadioButton->isChecked() )
    {
        mCellHead.proj = PROJECTION_XY;
        mCellHead.zone = 0;
        mProjInfo = 0;
        mProjUnits = 0;

        button( QWizard::NextButton )->setEnabled( true );
        return;
    }

    // Define projection
    if ( !proj4.isEmpty() )
    {
        QgsDebugMsg( QString( "proj4 = %1" ).arg( proj4.toLocal8Bit().constData() ) );

        OGRSpatialReferenceH hCRS = NULL;
        hCRS = OSRNewSpatialReference( NULL );
        int errcode;
        const char *oldlocale = setlocale( LC_NUMERIC, NULL );
        setlocale( LC_NUMERIC, "C" );
        errcode = OSRImportFromProj4( hCRS, proj4.toUtf8() );
        setlocale( LC_NUMERIC, oldlocale );
        if ( errcode != OGRERR_NONE )
        {
            QgsDebugMsg( QString( "OGR can't parse PROJ.4-style parameter string:\n%1\nOGR Error code was %2" ).arg( proj4 ).arg( errcode ) );

            mCellHead.proj = PROJECTION_XY;
            mCellHead.zone = 0;
            mProjInfo = 0;
            mProjUnits = 0;
        }
        else
        {
            char *wkt = NULL;

            QgsDebugMsg( QString( "OSRIsGeographic = %1" ).arg( OSRIsGeographic( hCRS ) ) );
            QgsDebugMsg( QString( "OSRIsProjected = %1" ).arg( OSRIsProjected( hCRS ) ) );

            if (( errcode = OSRExportToWkt( hCRS, &wkt ) ) != OGRERR_NONE )
            {
                QgsDebugMsg( QString( "OGR can't get Wkt-style parameter string\nOGR Error code was %1" ).arg( errcode ) );
            }
            else
            {
                QgsDebugMsg( QString( "wkt = %1" ).arg( wkt ) );
            }

            // Note: GPJ_osr_to_grass() defaults in PROJECTION_XY if projection
            //       cannot be set

            // There was a bug in GRASS, it is present in 6.0.x line
            int ret = GPJ_wkt_to_grass( &mCellHead, &mProjInfo, &mProjUnits, wkt, 0 );

            // Note: It seems that GPJ_osr_to_grass()returns always 1,
            //   -> test if mProjInfo was set

            Q_UNUSED( ret );
            QgsDebugMsg( QString( "ret = %1" ).arg( ret ) );
            QgsDebugMsg( QString( "mProjInfo = %1" ).arg( QString::number(( qulonglong )mProjInfo, 16 ).toLocal8Bit().constData() ) );

            OGRFree( wkt );
        }

        if ( !mProjInfo || !mProjUnits )
        {
            setError( mProjErrorLabel, tr( "Selected projection is not supported by GRASS!" ) );
        }
    }
    else // Nothing selected
    {
        mCellHead.proj = PROJECTION_XY;
        mCellHead.zone = 0;
        mProjInfo = 0;
        mProjUnits = 0;
    }
    button( QWizard::NextButton )->setEnabled( mProjInfo && mProjUnits );
}
Beispiel #8
0
OGRSpatialReferenceH GPJ_grass_to_osr(struct Key_Value * proj_info,
				      struct Key_Value * proj_units)
{
    struct pj_info pjinfo;
    char *proj4, *proj4mod, *wkt, *modwkt, *startmod, *lastpart;
    OGRSpatialReferenceH hSRS, hSRS2;
    OGRErr errcode;
    struct gpj_datum dstruct;
    struct gpj_ellps estruct;
    size_t len;
    const char *ellpskv, *unit, *unfact;
    char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname,
	*start, *end;
    const char *sysname, *osrunit, *osrunfact;
    double a, es, rf;
    int haveparams = 0;

    if ((proj_info == NULL) || (proj_units == NULL))
	return NULL;

    hSRS = OSRNewSpatialReference(NULL);

    if (pj_get_kv(&pjinfo, proj_info, proj_units) < 0) {
	G_warning(_("Unable parse GRASS PROJ_INFO file"));
	return NULL;
    }

    if ((proj4 = pj_get_def(pjinfo.pj, 0)) == NULL) {
	G_warning(_("Unable get PROJ.4-style parameter string"));
	return NULL;
    }
    pj_free(pjinfo.pj);

    unit = G_find_key_value("unit", proj_units);
    unfact = G_find_key_value("meters", proj_units);
    if (unfact != NULL && (strcmp(pjinfo.proj, "ll") != 0))
	G_asprintf(&proj4mod, "%s +to_meter=%s", proj4, unfact);
    else
	proj4mod = G_store(proj4);
    pj_dalloc(proj4);

    if ((errcode = OSRImportFromProj4(hSRS, proj4mod)) != OGRERR_NONE) {
	G_warning(_("OGR can't parse PROJ.4-style parameter string: "
		    "%s (OGR Error code was %d)"), proj4mod, errcode);
	return NULL;
    }
    G_free(proj4mod);

    if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) {
	G_warning(_("OGR can't get WKT-style parameter string "
		    "(OGR Error code was %d)"), errcode);
	return NULL;
    }

    ellpskv = G_find_key_value("ellps", proj_info);
    GPJ__get_ellipsoid_params(proj_info, &a, &es, &rf);
    haveparams = GPJ__get_datum_params(proj_info, &datum, &params);

    if(ellpskv != NULL)
	ellps = G_store(ellpskv);
    else
	ellps = NULL;

    if ((datum == NULL) || (GPJ_get_datum_by_name(datum, &dstruct) < 0)) {
	datumlongname = G_store("unknown");
	if (ellps == NULL)
	    ellps = G_store("unnamed");
    }
    else {
	datumlongname = G_store(dstruct.longname);
	if (ellps == NULL)
	    ellps = G_store(dstruct.ellps);
	GPJ_free_datum(&dstruct);
    }
    G_free(datum);
    if (GPJ_get_ellipsoid_by_name(ellps, &estruct) > 0) {
	ellpslong = G_store(estruct.longname);
	DatumNameMassage(&ellpslong);
	GPJ_free_ellps(&estruct);
    }
    else
	ellpslong = G_store(ellps);

    startmod = strstr(wkt, "GEOGCS");
    lastpart = strstr(wkt, "PRIMEM");
    len = strlen(wkt) - strlen(startmod);
    wkt[len] = '\0';
    if (haveparams == 2) {
	/* Only put datum params into the WKT if they were specifically
	 * specified in PROJ_INFO */
	char *paramkey, *paramvalue;

	paramkey = strtok(params, "=");
	paramvalue = params + strlen(paramkey) + 1;
	if (G_strcasecmp(paramkey, "towgs84") == 0)
	    G_asprintf(&towgs84, ",TOWGS84[%s]", paramvalue);
	else
	    towgs84 = G_store("");
	G_free(params);
    }
    else
	towgs84 = G_store("");

    sysname = OSRGetAttrValue(hSRS, "PROJCS", 0);
    if (sysname == NULL) {
	/* Not a projected co-ordinate system */
	start = G_store("");
	end = G_store("");
    }
    else {
	if ((strcmp(sysname, "unnamed") == 0) &&
	    (G_find_key_value("name", proj_info) != NULL))
	    G_asprintf(&start, "PROJCS[\"%s\",",
		       G_find_key_value("name", proj_info));
	else
	    start = G_store(wkt);

	osrunit = OSRGetAttrValue(hSRS, "UNIT", 0);
	osrunfact = OSRGetAttrValue(hSRS, "UNIT", 1);

	if ((unfact == NULL) || (G_strcasecmp(osrunit, "unknown") != 0))
	    end = G_store("");
	else {
	    char *buff;
	    double unfactf = atof(unfact);

	    G_asprintf(&buff, ",UNIT[\"%s\",", osrunit);

	    startmod = strstr(lastpart, buff);
	    len = strlen(lastpart) - strlen(startmod);
	    lastpart[len] = '\0';
	    G_free(buff);

	    if (unit == NULL)
		unit = "unknown";
	    G_asprintf(&end, ",UNIT[\"%s\",%.16g]]", unit, unfactf);
	}

    }
    OSRDestroySpatialReference(hSRS);
    G_asprintf(&modwkt,
	       "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
	       start, ellps, datumlongname, ellpslong, a, rf, towgs84,
	       lastpart, end);
    hSRS2 = OSRNewSpatialReference(modwkt);
    G_free(modwkt);

    CPLFree(wkt);
    G_free(start);
    G_free(ellps);
    G_free(datumlongname);
    G_free(ellpslong);
    G_free(towgs84);
    G_free(end);

    return hSRS2;
}