10#include <QScopedPointer>
15#include <boost/foreach.hpp>
18#include "IException.h"
19#include "GisGeometry.h"
21#include "PvlFlatMap.h"
163 v_globals.append(myGlobals);
166 v_globals.push_back(myGlobals);
167 v_globals.append(globals);
169 return ( v_globals );
173 const PvlObject &Strategy::getDefinition()
const {
206 QString descr =
"Strategy::" +
name() +
" is running a " +
type() +
283 cout <<
"Empty apply is called...\n";
343 if ( resource->isDiscarded() ) {
345 result +=
apply(resource, globals);
350 result +=
apply(resource, globals);
409 if ( resource->isDiscarded() ) { n++; }
425 const QString &
name)
const {
427 if ( resource->hasAsset(
name) ) {
428 QVariant v_asset = resource->asset(
name);
453 const QString &defValue)
const {
455 if ( keystore->exists(target) ) {
456 if ( keystore->count(target) > index ) {
457 return (keystore->value(target, index));
478 const QString &delimiter)
const {
497 const QString &replacement)
const {
499 str.replace(target, replacement, Qt::CaseInsensitive);
524 const QString &defValue)
const {
532 QString keyword = keyBase +
"Keyword";
533 if ( keys.
exists(keyword) ) {
534 idArgs.push_back(keys.
get(keyword));
535 value = keys.
get(keyBase,
"%1");
538 if ( keys.
exists(keyBase +
"Args") ) {
539 idArgs = keys.
allValues(keyBase +
"Args");
541 value = keys.
get(keyBase, defValue);
544 return (
processArgs(value, idArgs, globals, defValue));
572 const QString &defValue)
const {
574 QString result = value;
575 int i = argKeys.size();
576 BOOST_REVERSE_FOREACH ( QString arg, argKeys ) {
577 QString target(
"%"+QString::number(i));
597 BOOST_FOREACH ( QString key, keySources ) {
598 if ( source->exists(key) ) {
599 target->add(source->keyword(key));
639 QString
id = resourceA->name() +
"_" + resourceB->name();
644 if ( !keySources.isEmpty() ) {
646 BOOST_FOREACH ( QString key, keySources ) {
647 if ( resourceA->exists(key) ) {
649 keyword.
setName(keyword.
name().append(keySuffix.first));
653 if ( resourceB->exists(key) ) {
655 keyword.
setName(keyword.
name().append(keySuffix.second));
664 while ( rkeysA.constEnd() != pkeys) {
666 keyword.
setName(keyword.
name().append(keySuffix.first));
672 pkeys = rkeysB.begin();
673 while ( rkeysB.constEnd() != pkeys) {
675 keyword.
setName(keyword.
name().append(keySuffix.second));
709 QString geom(keys.
get(
"GisGeometry",
""));
712 if ( keys.
exists(
"GisGeometryRef") ) { giskey = keys.
get(
"GisGeometryRef"); }
713 if ( keys.
exists(
"GisGeometryKey") ) { giskey = keys.
get(
"GisGeometryKey"); }
715 if ( !giskey.isEmpty() ) {
716 if ( !resource->isNull(giskey) ) {
717 geom = resource->value(giskey);
720 if (
toBool(keys.
get(
"RemoveGisKeywordAfterImport",
"false")) ) {
721 resource->erase(giskey);
727 if ( !geom.isEmpty() ) {
730 bool repairGeom =
toBool(keys.
get(
"RepairInvalidGeometry",
"true"));
732 QString geomAction = keys.
get(
"InvalidGeometryAction",
"disable").toLower();
733 if ( !QString(
"disable error continue").contains(geomAction) ) {
735 cout <<
" Invalid value for InvalidGeometryAction (" << geomAction
736 <<
") - set to disable!\n";
738 geomAction =
"disable";
742 if ( keys.
exists(
"GisGeometryArgs") ) {
748 QString gisType = keys.
get(
"GisType");
751 double tolerance(0.0);
755 if ( !geom.isEmpty() ) {
758 if ( geosgeom.isNull() ) {
760 cout << resource->name() <<
" geometry failed to construct\n";
762 if (
"continue" == geomAction)
return (
false);
763 if (
"disable" == geomAction ) {
769 QString mess = resource->name() +
" failed to construct geometry!";
774 if ( !geosgeom->isValid() ) {
776 QString geomError = geosgeom->isValidReason();
778 cout <<
" Geometry error: " << geomError <<
"\n";
784 cout <<
" " << resource->name() <<
" geometry is invalid..."
785 <<
"attempting buffer(0) to fix it!\n";
787 geosgeom.reset( geosgeom->buffer(0) );
789 if (geosgeom.isNull() || !geosgeom->isValid() ) {
790 cout <<
" Geometry could not be repaired!\n";
793 cout <<
" Geometry was successfully repaired!\n";
801 if (geosgeom.isNull() || !geosgeom->isValid() ) {
803 cout <<
" All efforts to convert geometry failed!\n";
805 if (
"continue" == geomAction)
return (
false);
806 if (
"disable" == geomAction ) {
812 QString mess = resource->name() +
" failed to construct geometry - Error: " +
818 npointsOrg = npoints = geosgeom->points();
822 if ( !gisTolerance.isEmpty() ) {
825 if ( 0 != simple ) geosgeom.reset(simple);
826 npoints = geosgeom->points();
829 resource->add(geosgeom.take());
835 if ( !pointsKey.isEmpty() ) {
836 resource->add(pointsKey,
toString(npoints));
837 resource->add(pointsKey+
"Original",
toString(npointsOrg));
838 resource->add(pointsKey+
"Tolerance",
toString(tolerance));
843 cout <<
" " <<
type() <<
":" <<
name() <<
" has a geometry with "
844 << npoints <<
" points!\n";
845 if ( npoints != npointsOrg ) {
846 cout <<
" Geometry has been simplified/reduced from original "
847 << npointsOrg <<
" points.\n";
856 cout <<
" " <<
type() <<
":" <<
name() <<
" does not have a geometry!\n";
874 if ( !resource->isDiscarded() ) { v_active.append(resource); }
887 resource->activate();
953 const bool &withAssets)
const {
956 v_clone.append(
SharedResource(resource->clone(resource->name(), withAssets)));
1008 QString mess =
type() +
":" +
name() +
1009 "Cannot apply RTree search to bad geometry.";
1018 if (
"direct" == method ) {
1020 cout <<
"Using direct Geom intersects for " << v_active.size() <<
" geometries...\n";
1027 if ( resource->hasValidGeometry() ) {
1028 if ( geom.
intersects( *resource->geometry()) ) {
1029 overlaps.append(resource);
1039 cout <<
"Allocating " << v_active.size() <<
" RTree geometries.\n";
1041 GEOSSTRtree *rtree = GEOSSTRtree_create(v_active.size());
1043 QString mess =
"GEOS RTree allocation failed for " +
1044 toString(v_active.size()) +
" geometries.";
1049 cout <<
"GEOS RTree allocated " << v_active.size() <<
" geometry entries.\n";
1056 for (
int i = 0 ; i < v_active.size() ; i++ ) {
1057 if ( v_active[i]->hasValidGeometry() ) {
1058 GEOSSTRtree_insert(rtree, v_active[i]->geometry()->geometry(),
1067 cout <<
"Valid Geometries found: " << nvalid <<
" - running query...\n";
1072 GEOSSTRtree_destroy(rtree);
1076 cout <<
"Potential Intersections Found: " << overlaps.size() <<
"\n";
1091 resource->activate();
1092 n +=
apply(resource, globals);
1098 cout <<
"Total valid Intersections Found: " << n <<
"\n";
1122 rlist->append(*resource);
1173 QString p_text = text;
1175 bool status =
false;
1180 if (
toBool(p_var.
get(
"ShowProgress",
"false")) ) {
1182 if ( p_text.isEmpty() ) {
1191 if ( !p_text.isEmpty() )
m_progress->SetText(p_text);
1204 PvlObject::ConstPvlObjectIterator
object = source.
beginObject();
1205 while (
object != source.
endObject() ) {
1206 objList <<
object->name();
Encapsulation class provides support for GEOS-C API.
Type type() const
Returns the type (origin) of the geometry.
bool intersects(const GisGeometry &target) const
Computes a new geometry from the intersection of the two geomtries.
const GEOSGeometry * geometry() const
Returns the GEOSGeometry object to extend functionality.
bool isValid() const
Determines validity of the geometry contained in this object.
GisGeometry * simplify(const double &tolerance) const
Simplify complex or overdetermined geoemtry.
@ Programmer
This error is for when a programmer made an API call that was illegal.
Program progress reporter.
static PvlConstraints withExcludes(const QStringList &excludes)
Static method to construct a PvlConstraints object from a list of names for the PvlObjects and PvlGro...
Provides a flat map of PvlKeywords.
QStringList allValues(const QString &key) const
Gets all the values associated with a keyword in the PvlFlatMap.
QString get(const QString &key, const int &index=0) const
Gets the value of a keyword in the PvlFlatMap.
bool exists(const QString &key) const
Determines whether a given keyword exists in the PvlFlatMap.
QMap< QString, PvlKeyword >::const_iterator ConstPvlFlatMapIterator
A const iterator for the underling QMap that PvlFlatMap is built on.
A single keyword-value pair.
void setName(QString name)
Sets the keyword name.
QString name() const
Returns the keyword name.
Contains Pvl Groups and Pvl Objects.
PvlObjectIterator beginObject()
Returns the index of the beginning object.
PvlObjectIterator endObject()
Returns the index of the ending object.
This class provides a resource of PVL keywords for Strategy classes.
static void queryCallback(void *item, void *userdata)
Important GEOS query callback for class and overlap geometry.
QString type() const
Accessor method to get the type of strategy.
int apply(ResourceList &resources)
Apply algorithm to resource list.
QScopedPointer< Progress > m_progress
Progress percentage monitor.
unsigned int totalProcessed() const
Accessor for the total number of resources processed.
QString scanAndReplace(const QString &input, const QString &target, const QString &replacement) const
Performs a case insensitive scan of the input string for a substring matching the target string and r...
void propagateKeys(SharedResource &source, SharedResource &target) const
Adds the PVL definition keywords from the source to the target.
QString processArgs(const QString &value, const QStringList &argKeys, const ResourceList &globals, const QString &defValue="") const
Processes the given string value using the given argument list, resource and default resource.
int countActive(const ResourceList &resources) const
Counts the number of active (i.e.
virtual ~Strategy()
Destroys the Strategy object.
Strategy()
Constructs default Strategy object of name "Strategy" and type "Counter".
void setApplyToDiscarded()
Sets Resource as discarded.
SharedResource composite(SharedResource &resourceA, SharedResource &resourceB, const QPair< QString, QString > &keySuffix=qMakePair(QString("A"), QString("B"))) const
Create a composite Resource from a pair by merging keywords.
int applyToIntersectedGeometry(ResourceList &resources, GisGeometry &geom, const ResourceList &globals)
Identify and apply Strategy to Resources that intersect a geometry.
void deactivateList(ResourceList &resources) const
Deactivate all resources contained in the resource list.
PvlFlatMap getDefinitionMap() const
Returns the keyword definitions found in the Strategy object.
int countDiscarded(const ResourceList &resources) const
Counts the number of non-active (i.e.
QString name() const
Accessor method to get the name of the strategy.
bool importGeometry(SharedResource &resource, const ResourceList &globals) const
Imports a geometry from the given resource.
ResourceList m_globals
A shared pointer to the global Resource of keywords.
ResourceList getGlobalDefaults() const
Accessor method to get the global defaults.
QString m_name
A string containing the name of the strategy.
QString findReplacement(const QString &target, const ResourceList &globals, const int &index=0, const QString &defValue="") const
Find keyword replacement value in globals list.
QString description() const
Return description for the strategy.
ResourceList assetResourceList(const SharedResource &resource, const QString &name) const
Searches the given resource for an asset with the given name and converts it to a ResourceList,...
unsigned int processed()
Increments the total number of resources processed and returns the incremented value.
QString m_type
A string containing the type of strategy.
ResourceList copyList(const ResourceList &resources) const
Make a copy of the resource list that is independently managed.
int applyToResources(ResourceList &resources, const ResourceList &globals)
Applies the strategy algorithms to the resources in the given list.
QString translateKeywordArgs(const QString &value, const ResourceList &globals, const QString &defValue="") const
Translates the arguments of the PVL keyword in the PVL definition object.
void setName(const QString &name)
Allow derived strategies to reset name (mostly for default constructors)
ResourceList getGlobals(SharedResource &myGlobals, const ResourceList &globals) const
ResourceList activeList(ResourceList &resources) const
Get list of all active Resources only - no discarded Resources.
QStringList qualifiers(const QString &keyspec, const QString &delimiter="::") const
Splits the given keyspec string into a list using the given delimiter string.
void activateList(ResourceList &resources) const
Activate all resources contained in the resource list.
void setDoNotApplyToDiscarded()
Disables the general application of Strategy algorithm for all Resources regardless of state.
bool doShowProgress() const
ResourceList cloneList(const ResourceList &resources, const bool &withAssets=false) const
Create a clone of a Resource list.
bool isApplyToDiscarded() const
Accessor for the apply discarded variable.
bool isDebug() const
An accessor method so that inherited classes can determine whether to print debug messages for this o...
void setType(const QString &type)
Allow derived strategies to reset type (mostly for default constructors)
bool initProgress(const int &nsteps=0, const QString &text="")
Initializes strategy progress monitor if requested by user.
unsigned int m_total
The total number of resources processed.
SharedPvlObject m_definition
A shared pointer to the PvlObject that defines the strategy.
bool m_applyDiscarded
Indicates whether to apply strategy to discarded resources.
bool m_debug
Indicates whether to print debug messages.
void resetProcessed()
Resets the total number of processed resources to zero.
This is free and unencumbered software released into the public domain.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
QList< SharedResource > ResourceList
Defintion of a ResourceList, a list of SharedResource types.
bool toBool(const QString &string)
Global function to convert from a string to a boolean.
double toDouble(const QString &string)
Global function to convert from a string to a double.
QSharedPointer< Resource > SharedResource
Defintion of a SharedResource, a shared pointer to a Resource object.
Namespace for the standard library.
This is free and unencumbered software released into the public domain.