26#ifndef __TERRALIB_POSTGIS_INTERNAL_UTILS_H
27#define __TERRALIB_POSTGIS_INTERNAL_UTILS_H
55#include <boost/date_time/time_zone_base.hpp>
61 typedef struct pg_conn
PGconn;
146 if(strcmp(t,
"btree") == 0)
150 else if(strcmp(t,
"gist") == 0)
154 else if(strcmp(t,
"gin") == 0)
174 std::string enve(str);
175 size_t st = enve.find(
",");
176 if(
st != std::string::npos)
177 enve.replace(
st, 1,
";");
178 double dval = atof(
"1,2");
182 while(
st != std::string::npos)
184 enve.replace(
st, 1,
",");
188 const char* enveStr = enve.c_str();
193 mbr->
m_llx = atof(enveStr);
195 while(*enveStr !=
' ')
200 mbr->
m_lly = atof(enveStr);
202 while(*enveStr !=
';')
207 mbr->
m_urx = atof(enveStr);
209 while(*enveStr !=
' ')
214 mbr->
m_ury = atof(enveStr);
230 output +=
"ST_MakeEnvelope(";
290 julian = y * 365 - 32167;
291 julian += y / 4 - century + century / 4;
292 julian += 7834 * m / 256 + d;
307 inline void Julian2Date(
const int jd,
int* year,
int* month,
int* day)
309 unsigned int julian = 0;
310 unsigned int quad = 0;
311 unsigned int extra = 0;
316 quad = julian / 146097;
317 extra = (julian - quad * 146097) * 4 + 3;
318 julian += 60 + quad * 3 + extra / 146097;
319 quad = julian / 1461;
320 julian -= quad * 1461;
321 y = julian * 4 / 1461;
322 julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366)) + 123;
325 quad = julian * 2141 / 65536;
326 *day = julian - 7834 * quad / 256;
327 *month = (quad + 10) % 12 + 1;
348 int year, month, day;
351 te::dt::DateTime* result =
new te::dt::Date(
static_cast<unsigned short>(year),
static_cast<unsigned short>(month),
static_cast<unsigned short>(day));
367 int hour = (int)(tval / 3600000000UL);
368 boost::int64_t timeval = tval - hour * (boost::int64_t)3600000000UL;
369 int min = (int)(timeval / 60000000);
370 timeval -= min * (boost::int64_t)60000000;
371 int sec = (int)(timeval / 1000000);
372 timeval -= sec * (boost::int64_t)1000000;
373 int fsec = (int) timeval;
375 boost::posix_time::time_duration td(hour, min, sec, fsec);
382 int hour = (int)(tval / 3600000000UL);
383 boost::int64_t timeval = tval - hour * (boost::int64_t)3600000000UL;
384 int min = (int)(timeval / 60000000);
385 timeval -= min * (boost::int64_t)60000000;
386 int sec = (int)(timeval / 1000000);
387 timeval -= sec * (boost::int64_t)1000000;
388 int fsec = (int) timeval;
390 boost::posix_time::time_duration td(hour, min, sec, fsec);
392 sprintf(buf,
"%d", z);
393 std::string sz =
"OFF";
397 boost::local_time::time_zone_ptr zp(
new boost::local_time::posix_time_zone(sz));
399 boost::posix_time::ptime pt(boost::gregorian::date(2000, 01, 01), td);
400 boost::local_time::local_date_time tz(pt, zp);
418 boost::int64_t dateval;
419 boost::int64_t timeval = ival;
422 dateval = timeval / 86400000000LL;
424 timeval -= dateval * 86400000000LL;
428 timeval += 86400000000LL;
442 boost::int64_t dateval;
443 boost::int64_t timeval = ival;
446 dateval = timeval / 86400000000LL;
448 timeval -= dateval * 86400000000LL;
452 timeval += 86400000000LL;
487 bool attNotNull,
const char* fmt,
488 bool attHasDefault,
const char* attDefValue,
489 unsigned int pgisGeomTypeOid,
490 unsigned int pgisRasterTypeOid)
495 std::string* defaultValue = (attHasDefault ?
new std::string(attDefValue) :
nullptr);
496 std::string name = std::string(attName);
498 if(attType == pgisGeomTypeOid)
500 else if(attType == pgisRasterTypeOid)
512 if (defaultValue != 0 && defaultValue->find(
"nextval(") == 0)
522 if(defaultValue != 0 && defaultValue->find(
"nextval(") == 0)
539 unsigned int precision = (fmt && (*(fmt + 7) !=
'\0') ? atoi(fmt + 8) : 0);
540 const char* sprecision = fmt ? strstr(fmt,
",") : 0;
541 unsigned int scale = (sprecision ? atoi(sprecision + 1) : 0);
549 unsigned int size = fmt ? atoi(fmt + 18) : 0;
588 unsigned int size = (fmt && (*(fmt + 9) !=
'\0') ? atoi(fmt + 10) : 0);
624 bool attNotNull,
const char* fmt,
625 bool attHasDefault,
const char* attDefValue,
626 int ndims,
unsigned int pgisGeomTypeOid,
627 unsigned int pgisRasterTypeOid)
631 attHasDefault, attDefValue,
632 pgisGeomTypeOid, pgisRasterTypeOid);
634 std::string* defaultValue = (attHasDefault ?
new std::string(attDefValue) : 0);
635 std::string name = std::string(attName);
639 for(
int i = 0; i < ndims - 1; ++i)
642 std::unique_ptr<te::dt::ArrayProperty>
at(
new te::dt::ArrayProperty(name, arrayElementProperty, attNotNull, defaultValue, attNum));
649 if(arrayElementProperty)
652 at->setElementType(p);
659 if(arrayElementProperty)
662 at->setElementType(p);
669 if(arrayElementProperty)
672 at->setElementType(p);
679 if(arrayElementProperty)
682 at->setElementType(p);
691 if(arrayElementProperty)
694 at->setElementType(p);
703 if(arrayElementProperty)
706 at->setElementType(p);
713 if(arrayElementProperty)
716 at->setElementType(p);
723 if(arrayElementProperty)
726 at->setElementType(p);
733 if(arrayElementProperty)
736 at->setElementType(p);
744 if(arrayElementProperty)
747 at->setElementType(p);
754 if(arrayElementProperty)
757 at->setElementType(p);
764 if(arrayElementProperty)
767 at->setElementType(p);
774 if(arrayElementProperty)
777 at->setElementType(p);
784 if(arrayElementProperty)
787 at->setElementType(p);
794 if(arrayElementProperty)
797 at->setElementType(p);
804 if(arrayElementProperty)
807 at->setElementType(p);
814 if(arrayElementProperty)
817 at->setElementType(p);
822 return Convert2TerraLib(attNum, attName, attType, attNotNull, fmt, attHasDefault, attDefValue, pgisGeomTypeOid, pgisRasterTypeOid);
840 return "ST_Intersects";
843 return "ST_Disjoint";
849 return "ST_Overlaps";
858 return "ST_Contains";
864 return "ST_CoveredBy";
920 unsigned int pgisGeomTypeOid,
921 unsigned int pgisRasterTypeOid,
922 std::vector<int>& teTypes);
934 if(gType & 0xF0000000)
938 gType = (gType & 0x0FFFFFFF) | 0xBB8;
942 gType = (gType & 0x0FFFFFFF) | 0x3E8;
946 gType = (gType & 0x0FFFFFFF) | 0x7D0;
950 gType = (gType & 0x0FFFFFFF);
958 const std::string* defaultSchema,
959 std::string& schemaName,
960 std::string& tableName);
964 if((gType & 0xF00) == 0xB00)
969 else if((gType & 0x0F00) == 0x300)
974 else if((gType & 0xF00) == 0x700)
989 std::string
GetBindableWhereSQL(
const std::vector<te::dt::Property*>& properties,
const std::size_t offset = 0);
The type for variable-length multidimensional arrays.
Utility functions to swap bytes: double, int and unsigned int.
The type for date and time types.
A base class for date data types.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
The type for arbitrary precision numbers, like numeric(p, q).
An atomic property like an integer or double.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
Utility functions for dealing with strings.
A class to represent time duration.
A class to represent time instant with time zone.
A class to represent time instant.
A class to store the proxy information that must be used to access data located in URIs.
A class that models the description of a dataset.
A dataset is the unit of information manipulated by the data access module of TerraLib.
The type for variable-length multidimensional arrays.
void setElementType(Property *t)
It sets the type of array elements.
The type for date and time types: date, date period, date duration, time duration,...
A base class for date data types.
const boost::gregorian::date & getDate() const
It returns the internal boost date type.
The type for arbitrary precison numbers, like numeric(p, q).
It models a property definition.
An atomic property like an integer or double.
void setAutoNumber(bool a)
It tells if the property is an autonumber or not.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
A class to represent time duration with nano-second/micro-second resolution.
A class to represent time instant with time zone.
const boost::local_time::local_date_time & getTimeInstantTZ() const
It returns the boost time instant with time zone type.
A class to represent time instant.
An Envelope defines a 2D rectangular region.
double m_llx
Lower left corner x-coordinate.
double m_urx
Upper right corner x-coordinate.
double m_ury
Upper right corner y-coordinate.
double m_lly
Lower left corner y-coordinate.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
An static class with global definitions.
General enumerations for the Data Access module.
An Envelope defines a 2D rectangular region.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Namespace for the DataAccess API of TerraLib.
FKActionType
Type of action performed on the foreign key data.
Namespace for the Data Type module of TerraLib.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
SpatialRelation
Spatial relations between geometric objects.
Namespace for the TerraLib PostGIS driver implementation.
std::string GetBindableUpdateSQL(const std::vector< te::dt::Property * > &properties)
Given a list of properties it constructs a string with bindable parameters that can be used inside an...
void Convert2PostGIS(const te::gm::Envelope *e, int srid, std::string &output)
It converts the envelope into a PostGIS BOX3D.
std::string GetLoadDataRow(const te::da::DataSetType *dt, te::da::DataSet *d, PGconn *conn)
std::string GetSQLValues(const te::da::DataSetType *dt, te::da::DataSet *d, PGconn *conn)
te::dt::DateTime * Internal2TimeStampTZ(boost::int64_t ival, int z)
void Convert2PostGISWKBType(unsigned int &gType)
std::string GetSQLBindValues(std::size_t nproperties)
void SplitTableName(const std::string &fullName, const std::string *defaultSchema, std::string &schemaName, std::string &tableName)
std::string MakeConnectionStr(const te::core::URI &connInfo)
te::da::IndexType GetIndexType(const char *t)
It converts the PostgreSQL index string to a TerraLib index type.
te::dt::Property * Convert2TerraLib(unsigned int attNum, const char *attName, unsigned int attType, bool attNotNull, const char *fmt, bool attHasDefault, const char *attDefValue, unsigned int pgisGeomTypeOid, unsigned int pgisRasterTypeOid)
It creates a PropertyType from a PostgreSQL attribute description.
const std::string & GetGeometryName(te::gm::GeomType t)
It returns the geometry names as usual for PostGIS.
te::dt::DateTime * Internal2Time(boost::int64_t tval)
It returns a DateTime type from a time loaded by PostgreSQL.
te::dt::DateTime * Internal2Date(const long dDate)
It returns a DateTime type from a date loaded by PostgreSQL.
te::dt::DateTime * Internal2TimeTZ(boost::int64_t tval, int z)
std::string GetBindableWhereSQL(const std::vector< te::dt::Property * > &properties, const std::size_t offset=0)
Given a list of properties it creates an AND connected expression with bindable parameters ($n).
bool SetColumnDef(std::string &s, const te::dt::Property *p, bool justDataType=false)
It writes the PostgreSQL column definition to the output string.
std::string GetBoxSpatialRelation(te::gm::SpatialRelation rel)
It converts the spatial relationship to PostGIS dialect.
int Date2Julian(int y, int m, const int d)
It returns a julian date (in seconds) from a gregorian date.
te::gm::Envelope * GetEnvelope(const char *str)
It converts the pgType to a valid TerraLib data type.
te::dt::DateTime * Internal2TimeStamp(boost::int64_t ival)
It returns a DateTime type from a timestamp loaded by PostgreSQL.
std::string GetSQLValue(const te::dt::Property *p, std::size_t propertyPos, te::da::DataSet *d, PGconn *conn)
void Julian2Date(const int jd, int *year, int *month, int *day)
It returns a gregorian date from a julian date (in seconds).
te::da::FKActionType GetAction(char a)
It converts the PostgreSQL foreign key modifier to a TerraLib data type.
std::string GetSpatialRelation(te::gm::SpatialRelation rel)
It converts the spatial relationship to PostGIS dialect.
void Convert2OGCWKBType(unsigned int &gType)
It converts the PostGIS geometry type to a pure OGC WKB code.
void ScapeString(PGconn *conn, const std::string &s, std::string &output)
It escapes a string for use within an SQL command.
Namespace for the TerraLib ST module.
struct pg_result PGresult
Configuration flags for the PostGIS Driver Implementation of TerraLib.
#define PG_FLOAT8_ARRAY_TYPE
#define PG_TEXT_ARRAY_TYPE
#define PG_OID_VECTOR_TYPE
#define PG_BYTEA_ARRAY_TYPE
#define PG_VARCHAR_ARRAY_TYPE
#define PG_INT4_ARRAY_TYPE
#define PG_CHARACTER_ARRAY_TYPE
#define PG_FLOAT4_ARRAY_TYPE
#define PG_INT2_VECTOR_TYPE
#define PG_TIMESTAMPTZ_ARRAY_TYPE
#define PG_DATE_ARRAY_TYPE
#define PG_TIMESTAMPTZ_TYPE
#define PG_TIME_ARRAY_TYPE
#define PG_TIMESTAMP_ARRAY_TYPE
#define PG_NUMERIC_ARRAY_TYPE
#define PG_INT2_ARRAY_TYPE
#define PG_TIMESTAMP_TYPE
#define PG_BOOL_ARRAY_TYPE
#define PG_INT8_ARRAY_TYPE
#define PG_OID_ARRAY_TYPE
#define TE_EWKB_ZM_OFFSET
#define PG_NAME_ARRAY_TYPE
#define PG__INT2_VECTOR_TYPE
#define PG_CHARACTER_TYPE
#define PG_CHAR_ARRAY_TYPE
#define PG_TIMETZ_ARRAY_TYPE