26 #ifndef __TERRALIB_POSTGIS_INTERNAL_UTILS_H
27 #define __TERRALIB_POSTGIS_INTERNAL_UTILS_H
30 #include "../common/ByteSwapUtils.h"
31 #include "../common/Globals.h"
32 #include "../common/StringUtils.h"
33 #include "../dataaccess/Enums.h"
34 #include "../datatype/ArrayProperty.h"
35 #include "../datatype/Date.h"
36 #include "../datatype/DateTimeProperty.h"
37 #include "../datatype/NumericProperty.h"
38 #include "../datatype/SimpleProperty.h"
39 #include "../datatype/StringProperty.h"
40 #include "../datatype/TimeDuration.h"
41 #include "../datatype/TimeInstant.h"
42 #include "../datatype/TimeInstantTZ.h"
43 #include "../geometry/Envelope.h"
44 #include "../geometry/Geometry.h"
45 #include "../geometry/GeometryProperty.h"
46 #include "../raster/RasterProperty.h"
55 #include <boost/date_time/time_zone_base.hpp>
61 typedef struct pg_conn
PGconn;
70 namespace core {
class URI; }
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);
1011 #endif // __TERRALIB_POSTGIS_INTERNAL_UTILS_H