def processAlgorithm(self, progress):
        vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Union.INPUT))
        vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Union.INPUT2))
        GEOS_EXCEPT = True
        FEATURE_EXCEPT = True
        vproviderA = vlayerA.dataProvider()
        allAttrsA = vproviderA.attributeIndexes()
        vproviderA.select( allAttrsA )
        vproviderB = vlayerB.dataProvider()
        allAttrsB = vproviderB.attributeIndexes()
        vproviderB.select( allAttrsB )

        # check for crs compatibility
        crsA = vproviderA.crs()
        crsB = vproviderB.crs()
        if not crsA.isValid() or not crsB.isValid():
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Union. Invalid CRS. Results might be unexpected")
        else:
            if not crsA != crsB:
                SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Union. Non-matching CRSs. Results might be unexpected")
        fields = ftools_utils.combineVectorFields(vlayerA, vlayerB )
        longNames = ftools_utils.checkFieldNameLength( fields )
        if not longNames.isEmpty():
            raise GeoAlgorithmExecutionException("Following field names are longer than 10 characters:\n" +  longNames.join('\n') )
        writer = self.getOutputFromName(Union.OUTPUT).getVectorWriter(fields, vproviderA.geometryType(), vproviderA.crs() )
        inFeatA = QgsFeature()
        inFeatB = QgsFeature()
        outFeat = QgsFeature()
        indexA = ftools_utils.createIndex( QGisLayers.features(vlayerB)  )
        indexB = ftools_utils.createIndex( QGisLayers.features(vlayerA)  )
        nFeat = vproviderA.featureCount() + vproviderB.featureCount()
        vproviderA.rewind()
        count = 0
        nElement = 0

        featuresA = QGisLayers.features(vlayerA)
        nFeat = len(featuresA)
        for inFeatA in featuresA:
          progress.setPercentage(nElement/float(nFeat) * 50)
          nElement += 1
          found = False
          geom = QgsGeometry( inFeatA.geometry() )
          diff_geom = QgsGeometry( geom )
          atMapA = inFeatA.attributeMap()
          intersects = indexA.intersects( geom.boundingBox() )
          if len( intersects ) < 1:
            try:
              outFeat.setGeometry( geom )
              outFeat.setAttributeMap( atMapA )
              writer.addFeature( outFeat )
            except:
              # this really shouldn't happen, as we
              # haven't edited the input geom at all
              FEATURE_EXCEPT = False
          else:
            for id in intersects:
              count += 1
              vproviderB.featureAtId( int( id ), inFeatB , True, allAttrsB )
              atMapB = inFeatB.attributeMap()
              tmpGeom = QgsGeometry( inFeatB.geometry() )
              try:
                if geom.intersects( tmpGeom ):
                  found = True
                  int_geom = geom.intersection( tmpGeom )

                  if int_geom is None:
                    # There was a problem creating the intersection
                    GEOS_EXCEPT = False
                    int_geom = QgsGeometry()
                  else:
                    int_geom = QgsGeometry(int_geom)

                  if diff_geom.intersects( tmpGeom ):
                    diff_geom = diff_geom.difference( tmpGeom )
                    if diff_geom is None:
                      # It's possible there was an error here?
                      diff_geom = QgsGeometry()
                    else:
                      diff_geom = QgsGeometry(diff_geom)

                  if int_geom.wkbType() == 0:
                    # intersection produced different geomety types
                    temp_list = int_geom.asGeometryCollection()
                    for i in temp_list:
                      if i.type() == geom.type():
                          int_geom = QgsGeometry( i )
                  try:
                    outFeat.setGeometry( int_geom )
                    outFeat.setAttributeMap( ftools_utils.combineVectorAttributes( atMapA, atMapB ) )
                    writer.addFeature( outFeat )
                  except Exception, err:
                    FEATURE_EXCEPT = False
                else:
                  # this only happends if the bounding box
                  # intersects, but the geometry doesn't
                  try:
                    outFeat.setGeometry( geom )
                    outFeat.setAttributeMap( atMapA )
                    writer.addFeature( outFeat )
                  except:
                    # also shoudn't ever happen
                    FEATURE_EXCEPT = False
              except Exception, err:
                GEOS_EXCEPT = False
                found = False
Example #2
0
    def processAlgorithm(self, progress):
        vlayerA = QGisLayers.getObjectFromUri(
            self.getParameterValue(Union.INPUT))
        vlayerB = QGisLayers.getObjectFromUri(
            self.getParameterValue(Union.INPUT2))
        GEOS_EXCEPT = True
        FEATURE_EXCEPT = True
        vproviderA = vlayerA.dataProvider()
        allAttrsA = vproviderA.attributeIndexes()
        vproviderA.select(allAttrsA)
        vproviderB = vlayerB.dataProvider()
        allAttrsB = vproviderB.attributeIndexes()
        vproviderB.select(allAttrsB)

        # check for crs compatibility
        crsA = vproviderA.crs()
        crsB = vproviderB.crs()
        if not crsA.isValid() or not crsB.isValid():
            SextanteLog.addToLog(
                SextanteLog.LOG_WARNING,
                "Union. Invalid CRS. Results might be unexpected")
        else:
            if not crsA != crsB:
                SextanteLog.addToLog(
                    SextanteLog.LOG_WARNING,
                    "Union. Non-matching CRSs. Results might be unexpected")
        fields = ftools_utils.combineVectorFields(vlayerA, vlayerB)
        longNames = ftools_utils.checkFieldNameLength(fields)
        if not longNames.isEmpty():
            raise GeoAlgorithmExecutionException(
                "Following field names are longer than 10 characters:\n" +
                longNames.join('\n'))
        writer = self.getOutputFromName(Union.OUTPUT).getVectorWriter(
            fields, vproviderA.geometryType(), vproviderA.crs())
        inFeatA = QgsFeature()
        inFeatB = QgsFeature()
        outFeat = QgsFeature()
        indexA = ftools_utils.createIndex(QGisLayers.features(vlayerB))
        indexB = ftools_utils.createIndex(QGisLayers.features(vlayerA))
        nFeat = vproviderA.featureCount() + vproviderB.featureCount()
        vproviderA.rewind()
        count = 0
        nElement = 0

        featuresA = QGisLayers.features(vlayerA)
        nFeat = len(featuresA)
        for inFeatA in featuresA:
            progress.setPercentage(nElement / float(nFeat) * 50)
            nElement += 1
            found = False
            geom = QgsGeometry(inFeatA.geometry())
            diff_geom = QgsGeometry(geom)
            atMapA = inFeatA.attributeMap()
            intersects = indexA.intersects(geom.boundingBox())
            if len(intersects) < 1:
                try:
                    outFeat.setGeometry(geom)
                    outFeat.setAttributeMap(atMapA)
                    writer.addFeature(outFeat)
                except:
                    # this really shouldn't happen, as we
                    # haven't edited the input geom at all
                    FEATURE_EXCEPT = False
            else:
                for id in intersects:
                    count += 1
                    vproviderB.featureAtId(int(id), inFeatB, True, allAttrsB)
                    atMapB = inFeatB.attributeMap()
                    tmpGeom = QgsGeometry(inFeatB.geometry())
                    try:
                        if geom.intersects(tmpGeom):
                            found = True
                            int_geom = geom.intersection(tmpGeom)

                            if int_geom is None:
                                # There was a problem creating the intersection
                                GEOS_EXCEPT = False
                                int_geom = QgsGeometry()
                            else:
                                int_geom = QgsGeometry(int_geom)

                            if diff_geom.intersects(tmpGeom):
                                diff_geom = diff_geom.difference(tmpGeom)
                                if diff_geom is None:
                                    # It's possible there was an error here?
                                    diff_geom = QgsGeometry()
                                else:
                                    diff_geom = QgsGeometry(diff_geom)

                            if int_geom.wkbType() == 0:
                                # intersection produced different geomety types
                                temp_list = int_geom.asGeometryCollection()
                                for i in temp_list:
                                    if i.type() == geom.type():
                                        int_geom = QgsGeometry(i)
                            try:
                                outFeat.setGeometry(int_geom)
                                outFeat.setAttributeMap(
                                    ftools_utils.combineVectorAttributes(
                                        atMapA, atMapB))
                                writer.addFeature(outFeat)
                            except Exception, err:
                                FEATURE_EXCEPT = False
                        else:
                            # this only happends if the bounding box
                            # intersects, but the geometry doesn't
                            try:
                                outFeat.setGeometry(geom)
                                outFeat.setAttributeMap(atMapA)
                                writer.addFeature(outFeat)
                            except:
                                # also shoudn't ever happen
                                FEATURE_EXCEPT = False
                    except Exception, err:
                        GEOS_EXCEPT = False
                        found = False
Example #3
0
    def processAlgorithm(self, progress):
        vlayerA = QGisLayers.getObjectFromUri(
            self.getParameterValue(Intersection.INPUT))
        vlayerB = QGisLayers.getObjectFromUri(
            self.getParameterValue(Intersection.INPUT2))
        GEOS_EXCEPT = True
        FEATURE_EXCEPT = True
        vproviderA = vlayerA.dataProvider()
        allAttrsA = vproviderA.attributeIndexes()
        vproviderA.select(allAttrsA)
        vproviderB = vlayerB.dataProvider()
        allAttrsB = vproviderB.attributeIndexes()
        vproviderB.select(allAttrsB)
        # check for crs compatibility
        crsA = vproviderA.crs()
        crsB = vproviderB.crs()
        if not crsA.isValid() or not crsB.isValid():
            SextanteLog.addToLog(
                SextanteLog.LOG_WARNING,
                "Intersection. Invalid CRS. Results might be unexpected")
        else:
            if not crsA != crsB:
                SextanteLog.addToLog(
                    SextanteLog.LOG_WARNING,
                    "Intersection. Non-matching CRSs. Results might be unexpected"
                )
        fields = ftools_utils.combineVectorFields(vlayerA, vlayerB)
        longNames = ftools_utils.checkFieldNameLength(fields)
        if not longNames.isEmpty():
            raise GeoAlgorithmExecutionException(
                "Following field names are longer than 10 characters:\n" +
                longNames.join('\n'))
        writer = self.getOutputFromName(Intersection.OUTPUT).getVectorWriter(
            fields, vproviderA.geometryType(), vproviderA.crs())
        inFeatA = QgsFeature()
        inFeatB = QgsFeature()
        outFeat = QgsFeature()
        index = ftools_utils.createIndex(QGisLayers.features(vlayerB))
        nElement = 0
        selectionA = QGisLayers.features(vlayerA)
        nFeat = len(selectionA)
        for inFeatA in selectionA:
            nElement += 1
            progress.setPercentage(nElement / float(nFeat) * 100)
            geom = QgsGeometry(inFeatA.geometry())
            atMapA = inFeatA.attributeMap()
            intersects = index.intersects(geom.boundingBox())
            for id in intersects:
                vproviderB.featureAtId(int(id), inFeatB, True, allAttrsB)
                tmpGeom = QgsGeometry(inFeatB.geometry())
                try:
                    if geom.intersects(tmpGeom):
                        atMapB = inFeatB.attributeMap()
                        int_geom = QgsGeometry(geom.intersection(tmpGeom))
                        if int_geom.wkbType() == 7:
                            int_com = geom.combine(tmpGeom)
                            int_sym = geom.symDifference(tmpGeom)
                            int_geom = QgsGeometry(int_com.difference(int_sym))
                    try:
                        outFeat.setGeometry(int_geom)
                        outFeat.setAttributeMap(
                            ftools_utils.combineVectorAttributes(
                                atMapA, atMapB))
                        writer.addFeature(outFeat)
                    except:
                        FEATURE_EXCEPT = False
                        continue
                except:
                    GEOS_EXCEPT = False
                    break

        del writer
        if not GEOS_EXCEPT:
            SextanteLog.addToLog(
                SextanteLog.LOG_WARNING,
                "Geometry exception while computing intersection")
        if not FEATURE_EXCEPT:
            SextanteLog.addToLog(
                SextanteLog.LOG_WARNING,
                "Feature exception while computing intersection")
    def processAlgorithm(self, progress):
        vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT))
        vlayerB = QGisLayers.getObjectFromUri(self.getParameterValue(Intersection.INPUT2))
        GEOS_EXCEPT = True
        FEATURE_EXCEPT = True
        vproviderA = vlayerA.dataProvider()
        allAttrsA = vproviderA.attributeIndexes()
        vproviderA.select(allAttrsA)
        vproviderB = vlayerB.dataProvider()
        allAttrsB = vproviderB.attributeIndexes()
        vproviderB.select(allAttrsB)
        # check for crs compatibility
        crsA = vproviderA.crs()
        crsB = vproviderB.crs()
        if not crsA.isValid() or not crsB.isValid():
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Intersection. Invalid CRS. Results might be unexpected")
        else:
            if not crsA != crsB:
                SextanteLog.addToLog(
                    SextanteLog.LOG_WARNING, "Intersection. Non-matching CRSs. Results might be unexpected"
                )
        fields = ftools_utils.combineVectorFields(vlayerA, vlayerB)
        longNames = ftools_utils.checkFieldNameLength(fields)
        if not longNames.isEmpty():
            raise GeoAlgorithmExecutionException(
                "Following field names are longer than 10 characters:\n" + longNames.join("\n")
            )
        writer = self.getOutputFromName(Intersection.OUTPUT).getVectorWriter(
            fields, vproviderA.geometryType(), vproviderA.crs()
        )
        inFeatA = QgsFeature()
        inFeatB = QgsFeature()
        outFeat = QgsFeature()
        index = ftools_utils.createIndex(QGisLayers.features(vlayerB))
        nElement = 0
        selectionA = QGisLayers.features(vlayerA)
        nFeat = len(selectionA)
        for inFeatA in selectionA:
            nElement += 1
            progress.setPercentage(nElement / float(nFeat) * 100)
            geom = QgsGeometry(inFeatA.geometry())
            atMapA = inFeatA.attributeMap()
            intersects = index.intersects(geom.boundingBox())
            for id in intersects:
                vproviderB.featureAtId(int(id), inFeatB, True, allAttrsB)
                tmpGeom = QgsGeometry(inFeatB.geometry())
                try:
                    if geom.intersects(tmpGeom):
                        atMapB = inFeatB.attributeMap()
                        int_geom = QgsGeometry(geom.intersection(tmpGeom))
                        if int_geom.wkbType() == 7:
                            int_com = geom.combine(tmpGeom)
                            int_sym = geom.symDifference(tmpGeom)
                            int_geom = QgsGeometry(int_com.difference(int_sym))
                    try:
                        outFeat.setGeometry(int_geom)
                        outFeat.setAttributeMap(ftools_utils.combineVectorAttributes(atMapA, atMapB))
                        writer.addFeature(outFeat)
                    except:
                        FEATURE_EXCEPT = False
                        continue
                except:
                    GEOS_EXCEPT = False
                    break

        del writer
        if not GEOS_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing intersection")
        if not FEATURE_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing intersection")