Exemplo n.º 1
0
void projection::init_proj4() const
{
#ifdef MAPNIK_USE_PROJ4
    if (!proj_)
    {
#if PJ_VERSION >= 480
        proj_ctx_ = pj_ctx_alloc();
        proj_ = pj_init_plus_ctx(proj_ctx_, params_.c_str());
        if (!proj_)
        {
            if (proj_ctx_) {
                pj_ctx_free(proj_ctx_);
                proj_ctx_ = 0;
            }
            throw proj_init_error(params_);
        }
#else
        #if defined(MAPNIK_THREADSAFE)
        mapnik::scoped_lock lock(mutex_);
        #endif
        proj_ = pj_init_plus(params_.c_str());
        if (!proj_) throw proj_init_error(params_);
#endif
        is_geographic_ = pj_is_latlong(proj_) ? true : false;
    }
#endif
}
Exemplo n.º 2
0
PJ_CONTEXT *proj_context_destroy (PJ_CONTEXT *ctx) {
    if (0==ctx)
        return 0;

    /* Trying to free the default context is a no-op (since it is statically allocated) */
    if (pj_get_default_ctx ()==ctx)
        return 0;

    pj_ctx_free (ctx);
    return 0;
}
Exemplo n.º 3
0
projection::~projection()
{
#ifdef MAPNIK_USE_PROJ4
    #if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
        mapnik::scoped_lock lock(mutex_);
    #endif
        if (proj_) pj_free(proj_);
    #if PJ_VERSION >= 480
        if (proj_ctx_) pj_ctx_free(proj_ctx_);
    #endif
#endif
}
Exemplo n.º 4
0
projection::~projection()
{
#ifdef MAPNIK_USE_PROJ4
 #if defined(MAPNIK_THREADSAFE) && PJ_VERSION < 480
    std::lock_guard<std::mutex> lock(mutex_);
 #endif
    if (proj_)
    {
        pj_free(proj_);
        proj_ = nullptr;
    }
 #if PJ_VERSION >= 480
    if (proj_ctx_)
    {
        pj_ctx_free(proj_ctx_);
        proj_ctx_ = nullptr;
    }
 #endif
#endif
}
QgsProjContextStore::~QgsProjContextStore()
{
  pj_ctx_free( context );
}
Exemplo n.º 6
0
static void TestThread()

{
    int i, test_count = sizeof(test_list) / sizeof(TestItem);
    int repeat_count = num_iterations;
    int i_iter;

/* -------------------------------------------------------------------- */
/*      Initialize coordinate system definitions.                       */
/* -------------------------------------------------------------------- */
    projPJ *src_pj_list, *dst_pj_list;
    projCtx ctx = pj_ctx_alloc();

    src_pj_list = (projPJ *) calloc(test_count,sizeof(projPJ));
    dst_pj_list = (projPJ *) calloc(test_count,sizeof(projPJ));

    if(!reinit_every_iteration)
    {
        for( i = 0; i < test_count; i++ )
        {
            TestItem *test = test_list + i;

            src_pj_list[i] = custom_pj_init_plus_ctx( ctx, test->src_def );
            dst_pj_list[i] = custom_pj_init_plus_ctx( ctx, test->dst_def );
        }
    }

/* -------------------------------------------------------------------- */
/*      Perform tests - over and over.                                  */
/* -------------------------------------------------------------------- */

    for( i_iter = 0; i_iter < repeat_count; i_iter++ )
    {
        for( i = 0; i < test_count; i++ )
        {
            TestItem *test = test_list + i;
            double x, y, z;
            int error;

            x = test->src_x;
            y = test->src_y;
            z = test->src_z;

            if( reinit_every_iteration )
            {
                src_pj_list[i] = custom_pj_init_plus_ctx( ctx, test->src_def );
                dst_pj_list[i] = custom_pj_init_plus_ctx( ctx, test->dst_def );

                {
                    int skipTest = (src_pj_list[i] == NULL || dst_pj_list[i] == NULL);

                    if ( skipTest != test->skip )
                        fprintf( stderr, "Threaded projection initialization does not match unthreaded initialization\n" );

                    if (skipTest)
                    {
                        pj_free( src_pj_list[i] );
                        pj_free( dst_pj_list[i] );
                        continue;
                    }
                }
            }

            if ( test->skip )
                continue;

            error = pj_transform( src_pj_list[i], dst_pj_list[i], 1, 0,
                                  &x, &y, &z );


            if( error != test->dst_error )
            {
                fprintf( stderr, "Got error %d, expected %d\n",
                         error, test->dst_error );
            }

            if( x != test->dst_x || y != test->dst_y || z != test->dst_z )
            {
                fprintf( stderr,
                         "Got %.15g,%.15g,%.15g\n"
                         "Expected %.15g,%.15g,%.15g\n"
                         "Diff %.15g,%.15g,%.15g\n",
                         x, y, z,
                         test->dst_x, test->dst_y, test->dst_z,
                         x-test->dst_x, y-test->dst_y, z-test->dst_z);
            }

            if( reinit_every_iteration )
            {
                pj_free( src_pj_list[i] );
                pj_free( dst_pj_list[i] );
            }
        }
    }

/* -------------------------------------------------------------------- */
/*      Cleanup                                                         */
/* -------------------------------------------------------------------- */
    if( !reinit_every_iteration )
    {
        for( i = 0; i < test_count; i++ )
        {
            pj_free( src_pj_list[i] );
            pj_free( dst_pj_list[i] );
        }
    }

    free( src_pj_list );
    free( dst_pj_list );

    pj_ctx_free( ctx );

    printf( "%d iterations of the %d tests complete in thread X\n",
            repeat_count, test_count );

    active_thread_count--;
}
Exemplo n.º 7
0
void QgsCustomProjectionDialog::pbnCalculate_clicked()
{
  // We must check the prj def is valid!
  projCtx pContext = pj_ctx_alloc();
  projPJ proj = pj_init_plus_ctx( pContext, teParameters->toPlainText().toLocal8Bit().data() );

  QgsDebugMsg( QString( "Proj: %1" ).arg( teParameters->toPlainText() ) );

  if ( !proj )
  {
    QMessageBox::information( this, tr( "QGIS Custom Projection" ),
                              tr( "This proj4 projection definition is not valid." ) );
    projectedX->clear();
    projectedY->clear();
    pj_free( proj );
    pj_ctx_free( pContext );
    return;

  }
  // Get the WGS84 coordinates
  bool okN, okE;
  double northing = northWGS84->text().toDouble( &okN ) * DEG_TO_RAD;
  double easting = eastWGS84->text().toDouble( &okE )  * DEG_TO_RAD;

  if ( !okN || !okE )
  {
    QMessageBox::information( this, tr( "QGIS Custom Projection" ),
                              tr( "Northing and Easthing must be in decimal form." ) );
    projectedX->clear();
    projectedY->clear();
    pj_free( proj );
    pj_ctx_free( pContext );
    return;
  }

  projPJ wgs84Proj = pj_init_plus_ctx( pContext, GEOPROJ4.toLocal8Bit().data() ); //defined in qgis.h

  if ( !wgs84Proj )
  {
    QMessageBox::information( this, tr( "QGIS Custom Projection" ),
                              tr( "Internal Error (source projection invalid?)" ) );
    projectedX->clear();
    projectedY->clear();
    pj_free( wgs84Proj );
    pj_ctx_free( pContext );
    return;
  }

  double z = 0.0;

  int projResult = pj_transform( wgs84Proj, proj, 1, 0, &easting, &northing, &z );
  if ( projResult != 0 )
  {
    projectedX->setText( tr( "Error" ) );
    projectedY->setText( tr( "Error" ) );
    QgsDebugMsg( pj_strerrno( projResult ) );
  }
  else
  {
    QString tmp;

    int precision = 4;

    if ( pj_is_latlong( proj ) )
    {
      northing *= RAD_TO_DEG;
      easting *= RAD_TO_DEG;
      precision = 7;
    }

    tmp = QLocale::system().toString( northing, 'f', precision );
    projectedX->setText( tmp );
    tmp = QLocale::system().toString( easting, 'f', precision );
    projectedY->setText( tmp );
  }

  pj_free( proj );
  pj_free( wgs84Proj );
  pj_ctx_free( pContext );
}
Exemplo n.º 8
0
			void run(const TProjection& p,
				const ForwardIterableInputRange& x,
				const ForwardIterableInputRange& y,
				ForwardIterableOutputRange& out_x,
				ForwardIterableOutputRange& out_y) const {
				size_t sx = boost::size(x);

				// data sanity
				assert(sx == boost::size(y));
				assert(boost::size(y) == boost::size(out_x));
				assert(boost::size(out_x) == boost::size(out_y));

				std::string from = projection_to_string(p.from);
				std::string to = projection_to_string(p.to);

				auto compute = [&p, &from, &to](double *x, double *y, double *z,
						size_t stride, size_t point_count) {
					projCtx ctx = pj_ctx_alloc();

					projPJ pj_in = pj_init_plus_ctx(ctx, from.c_str()),
						   pj_out = pj_init_plus_ctx(ctx, to.c_str());

					assert(pj_in != NULL);
					assert(pj_out != NULL);

					detail::pre_process(p.from, x, y, point_count);


					// fasten your seatbelts
					pj_transform(pj_in, pj_out,
							static_cast<long>(point_count),
							static_cast<int>(stride),
							x, y, NULL);

					pj_free(pj_in);
					pj_free(pj_out);

					pj_ctx_free(ctx);
					detail::post_process(p.to, x, y, point_count);
				};

				typedef typename boost::range_iterator<ForwardIterableOutputRange>::type iterator;
				typedef typename boost::range_const_iterator<ForwardIterableInputRange>::type const_iterator;

				// projcl does in-place transforms, so move all input values to output
				const_iterator bx = boost::begin(x),
							   by = boost::begin(y);

				iterator ox = boost::begin(out_x),
						 oy = boost::begin(out_y);

				std::copy(bx, boost::end(x), ox);
				std::copy(by, boost::end(y), oy);

				utility::scheduler<MaxConcurrency> c;
				unsigned max_threads = c.concurrency();
				size_t per_batch = sx / max_threads;

				size_t offset = 0;
				for (unsigned i = 0 ; i < max_threads ; i ++) {
					double *x = &(*(ox + offset));
					double *y = &(*(oy + offset));
					double *z = NULL;

					c.queue(compute, x, y, z, 1, per_batch);
					offset += per_batch;
				}

				c.wait();
			}
static void *TestThread( void *pData )

{
    int i, test_count = sizeof(test_list) / sizeof(TestItem); 

/* -------------------------------------------------------------------- */
/*      Initialize coordinate system definitions.                       */
/* -------------------------------------------------------------------- */
    projPJ *src_pj_list, *dst_pj_list;
    projCtx ctx = pj_ctx_alloc();
//    projCtx ctx = pj_get_default_ctx();
    
    src_pj_list = (projPJ *) calloc(test_count,sizeof(projPJ));
    dst_pj_list = (projPJ *) calloc(test_count,sizeof(projPJ));
                                
#if reinit_every_iteration == 0
    for( i = 0; i < test_count; i++ )
    {
        TestItem *test = test_list + i;

        src_pj_list[i] = pj_init_plus_ctx( ctx, test->src_def );
        dst_pj_list[i] = pj_init_plus_ctx( ctx, test->dst_def );
    }
#endif
    
/* -------------------------------------------------------------------- */
/*      Perform tests - over and over.                                  */
/* -------------------------------------------------------------------- */
    int repeat_count = num_iterations, i_iter;
    
    for( i_iter = 0; i_iter < repeat_count; i_iter++ )
    {
        for( i = 0; i < test_count; i++ )
        {
            TestItem *test = test_list + i;
            double x, y, z;
            int error;

            if( test->skip )
                continue;
            
            x = test->src_x;
            y = test->src_y;
            z = test->src_z;

#if reinit_every_iteration == 1
            src_pj_list[i] = pj_init_plus_ctx( ctx, test->src_def );
            dst_pj_list[i] = pj_init_plus_ctx( ctx, test->dst_def );
#endif

            error = pj_transform( src_pj_list[i], dst_pj_list[i], 1, 0, 
                                  &x, &y, &z );
            

            if( error != test->dst_error )
            {
                fprintf( stderr, "Got error %d, expected %d\n", 
                         error, test->dst_error );
            }

            if( x != test->dst_x || y != test->dst_y || z != test->dst_z )
            {
                fprintf( stderr, 
                         "Got %.15g,%.15g,%.15g\n"
                         "Expected %.15g,%.15g,%.15g\n",
                         x, y, z, 
                         test->dst_x, test->dst_y, test->dst_z );
            }

#if reinit_every_iteration == 1
            pj_free( src_pj_list[i] );
            pj_free( dst_pj_list[i] );
#endif
        }
    }

/* -------------------------------------------------------------------- */
/*      Cleanup                                                         */
/* -------------------------------------------------------------------- */
#if reinit_every_iteration == 0
    for( i = 0; i < test_count; i++ )
    {
        TestItem *test = test_list + i;
        pj_free( src_pj_list[i] );
        pj_free( dst_pj_list[i] );
    }
#endif
    
    free( src_pj_list );
    free( dst_pj_list );

    pj_ctx_free( ctx );

    printf( "%d iterations of the %d tests complete in thread X\n", 
            repeat_count, test_count );

    active_thread_count--;
}
Exemplo n.º 10
0
SPATIALITE_PRIVATE void
free_internal_cache (struct splite_internal_cache *cache)
{
/* freeing an internal cache */
    struct splite_geos_cache_item *p;
#ifndef OMIT_GEOS
    GEOSContextHandle_t handle = NULL;
#endif

#ifdef ENABLE_LIBXML2
    int i;
    struct splite_xmlSchema_cache_item *p_xmlSchema;
#endif

    if (cache == NULL)
	return;
    if (cache->magic1 != SPATIALITE_CACHE_MAGIC1
	|| cache->magic2 != SPATIALITE_CACHE_MAGIC2)
	return;

#ifndef OMIT_GEOS
    handle = cache->GEOS_handle;
    if (handle != NULL)
	finishGEOS_r (handle);
    cache->GEOS_handle = NULL;
    gaiaResetGeosMsg_r (cache);
#endif

#ifndef OMIT_PROJ
    if (cache->PROJ_handle != NULL)
	pj_ctx_free (cache->PROJ_handle);
    cache->PROJ_handle = NULL;
#endif

/* freeing GEOS error buffers */
    if (cache->gaia_geos_error_msg)
	free (cache->gaia_geos_error_msg);
    if (cache->gaia_geos_warning_msg)
	free (cache->gaia_geos_warning_msg);
    if (cache->gaia_geosaux_error_msg)
	free (cache->gaia_geosaux_error_msg);

/* freeing the XML error buffers */
    gaiaOutBufferReset (cache->xmlParsingErrors);
    gaiaOutBufferReset (cache->xmlSchemaValidationErrors);
    gaiaOutBufferReset (cache->xmlXPathErrors);
    free (cache->xmlParsingErrors);
    free (cache->xmlSchemaValidationErrors);
    free (cache->xmlXPathErrors);

/* freeing the GEOS cache */
    p = &(cache->cacheItem1);
    splite_free_geos_cache_item_r (cache, p);
    p = &(cache->cacheItem2);
    splite_free_geos_cache_item_r (cache, p);
#ifdef ENABLE_LIBXML2
    for (i = 0; i < MAX_XMLSCHEMA_CACHE; i++)
      {
	  /* freeing the XmlSchema cache */
	  p_xmlSchema = &(cache->xmlSchemaCache[i]);
	  splite_free_xml_schema_cache_item (p_xmlSchema);
      }
#endif

    if (cache->cutterMessage != NULL)
	sqlite3_free (cache->cutterMessage);
    cache->cutterMessage = NULL;

    spatialite_finalize_topologies (cache);

#ifndef GEOS_REENTRANT		/* only partially thread-safe mode */
/* releasing the connection pool object */
    invalidate (cache->pool_index);
#endif /* end GEOS_REENTRANT */

/* freeing the cache itself */
    free (cache);
}
Exemplo n.º 11
0
static CPLString GetProj4Filename(const char* pszFilename)
{
    CPLString osFilename;

    /* or fixed path: /name, ./name or ../name */
    if ( !CPLIsFilenameRelative(pszFilename) || *pszFilename == '.' )
    {
        return pszFilename;
    }

#if defined(PROJ_STATIC) && PROJ_VERSION >= 5
    PJ_GRID_INFO info = proj_grid_info(pszFilename);
    if( info.filename[0] )
    {
        osFilename = info.filename;
    }
#elif defined(PROJ_STATIC) && PJ_VERSION > 493
    osFilename.resize(2048);
    projCtx ctx = pj_ctx_alloc();
    if( pj_find_file(ctx, pszFilename, &osFilename[0], osFilename.size()) )
    {
        osFilename.resize( strlen(osFilename) );
    }
    else
    {
        osFilename.clear();
    }
    pj_ctx_free(ctx);
#else
    // Transpose some of the proj.4 pj_open_lib() logic...

    /* check if ~/name */
    char* pszSysname;
    if (*pszFilename == '~' &&
        (pszFilename[1] == '/' || pszFilename[1] == '\\') )
    {
        if ((pszSysname = getenv("HOME")) != nullptr)
        {
            osFilename = CPLFormFilename(pszSysname, pszFilename + 1, nullptr);
        }
        return osFilename;
    }

    /* or is environment PROJ_LIB defined */
    else if ((pszSysname = getenv("PROJ_LIB")) != nullptr)
    {
        osFilename = CPLFormFilename(pszSysname, pszFilename, nullptr);
        VSIStatBufL sStat;
        if( VSIStatL(osFilename, &sStat) == 0 )
            return osFilename;
        osFilename.clear();
    }


#if defined(PROJ_STATIC) && PJ_VERSION >= 490
    // Super messy. proj.4 up to 4.9.3 had no public API to return the full
    // path to a resource file, so we rely on the fact that it emits a log
    // message with it...
    // Basically this is needed in the case where the file is in the
    // resource installation directory of proj.4, which we have no way to
    // know otherwise.
    CPLString osMsg;
    projCtx ctx = pj_ctx_alloc();
    pj_ctx_set_app_data(ctx, &osMsg);
    pj_ctx_set_debug(ctx, PJ_LOG_DEBUG_MAJOR);
    pj_ctx_set_logger(ctx, my_proj4_logger);
    PAFile f = pj_open_lib(ctx, pszFilename, "rb");
    if( f )
    {
        pj_ctx_fclose(ctx, f);
        size_t nPos = osMsg.find("fopen(");
        if( nPos != std::string::npos )
        {
            osFilename = osMsg.substr(nPos + strlen("fopen("));
            nPos = osFilename.find(")");
            if( nPos != std::string::npos )
                osFilename = osFilename.substr(0, nPos);
        }
    }
    pj_ctx_free(ctx);
#endif
#endif
    return osFilename;
}