Пример #1
0
    def testGetFromWKTUsingLookup_GDA(self):
        test = crs()
        test.getFromWKT(EPSG_28354_WKT)

        self.assertEqual(EPSG_28354_WKT, test.crs_wkt)
        self.assertEqual(test.epsg, from_epsg(test.epsg_number))
        # self.assertEqual(test.proj4, '+proj=utm +zone=54 +south +ellps=GRS80 +units=m +no_defs ')
        self.assertEqual(test.epsg_predicted, False)

        test = crs()
        test.getFromWKT(ESRI_54_WKT_1)

        self.assertEqual(ESRI_54_WKT_1, test.crs_wkt)
        self.assertEqual(test.epsg, from_epsg(test.epsg_number))
        # self.assertEqual(test.proj4, '+proj=utm +zone=54 +south +ellps=GRS80 +units=m +no_defs ')
        if parse_version(pyproj.__version__) >= parse_version('2.5.0'):
            self.assertEqual(test.epsg_predicted, False)
        else:
            self.assertEqual(test.epsg_predicted, True)

        test = crs()
        test.getFromWKT(ESRI_54_WKT_2)

        self.assertEqual(ESRI_54_WKT_2, test.crs_wkt)
        self.assertEqual(test.epsg, from_epsg(test.epsg_number))
        # self.assertEqual(test.proj4, '+proj=utm +zone=54 +south +ellps=GRS80 +units=m +no_defs ')
        if parse_version(pyproj.__version__) >= parse_version('2.5.0'):
            self.assertEqual(test.epsg_predicted, False)
        else:
            self.assertEqual(test.epsg_predicted, True)
Пример #2
0
    def test_getFromEPSG(self):
        test = crs()
        test.getFromEPSG(28354)
        self.assertEqual(test.epsg_number, 28354)
        self.assertEqual(test.epsg, from_epsg(test.epsg_number))
        self.assertEqual(EPSG_28354_WKT[:154], test.crs_wkt[:154])

        self.assertEqual('+proj=utm +zone=54 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs',
                         test.proj4.strip())
Пример #3
0
    def test_getFromWKT_GDA1(self):
        test = crs()
        test.getFromWKT(EPSG_28354_WKT)

        self.assertEqual(test.epsg_number, 28354)
        self.assertEqual(test.epsg, from_epsg(test.epsg_number))
        self.assertEqual('+proj=utm +zone=54 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs',
                         test.proj4.strip())

        self.assertEqual(EPSG_28354_WKT, test.crs_wkt)

        test.getFromWKT(ESRI_54_WKT_2)
        self.assertEqual(28354, test.epsg_number)
        self.assertEqual(from_epsg(test.epsg_number), test.epsg)
        self.assertEqual('+proj=utm +zone=54 +south +ellps=GRS80 +units=m +no_defs', test.proj4.strip())
        self.assertEqual(ESRI_54_WKT_2, test.crs_wkt)
Пример #4
0
    def testGetFromWKTUsingOnlineLookup_GDA(self):

        with warnings.catch_warnings(record=True) as w:
            test = crs()
            test.getFromWKT(ESRI_54_WKT_1)

            if len(w) == 1:
                print('Timeout Occurred')
                self.assertIn('could not be reached. Timeout after 10 seconds',
                              str(w[-1].message))
            else:
                self.assertEqual(ESRI_54_WKT_1, test.crs_wkt)

                self.assertEqual(test.epsg, from_epsg(test.epsg_number))
                self.assertEqual(
                    test.proj4, '+proj=utm +zone=54 +south +ellps=GRS80'
                    ' +units=m +no_defs ')
                self.assertEqual(test.epsg_predicted, False)
Пример #5
0
    def test_TTestAnalysis(self):
        columns = [
            'FID', 'TrialID', 'Strip_Name', 'PointID', 'DistOnLine', 'geometry'
        ]
        data = [[30, 1, "Strip", 1, 2.5,
                 Point(300169, 6181357)],
                [31, 1, "E Strip", 1, 2.5,
                 Point(300189, 6181363)],
                [32, 1, "W Strip", 1, 2.5,
                 Point(300150, 6181352)],
                [33, 1, "Strip", 2, 22.5,
                 Point(300174, 6181338)],
                [34, 1, "E Strip", 2, 22.5,
                 Point(300194, 6181343)],
                [35, 1, "W Strip", 2, 22.5,
                 Point(300155, 6181333)],
                [36, 1, "Strip", 3, 42.5,
                 Point(300180, 6181319)],
                [37, 1, "E Strip", 3, 42.5,
                 Point(300199, 6181324)],
                [38, 1, "W Strip", 3, 42.5,
                 Point(300160, 6181314)],
                [39, 1, "Strip", 4, 62.5,
                 Point(300185, 6181299)],
                [40, 1, "E Strip", 4, 62.5,
                 Point(300204, 6181305)],
                [41, 1, "W Strip", 4, 62.5,
                 Point(300166, 6181294)],
                [42, 1, "Strip", 5, 82.5,
                 Point(300190, 6181280)],
                [43, 1, "E Strip", 5, 82.5,
                 Point(300209, 6181285)],
                [44, 1, "W Strip", 5, 82.5,
                 Point(300171, 6181275)],
                [45, 1, "Strip", 6, 102.5,
                 Point(300195, 6181261)],
                [46, 1, "E Strip", 6, 102.5,
                 Point(300215, 6181266)],
                [47, 1, "W Strip", 6, 102.5,
                 Point(300176, 6181256)],
                [48, 1, "Strip", 7, 122.5,
                 Point(300200, 6181242)],
                [49, 1, "E Strip", 7, 122.5,
                 Point(300220, 6181247)],
                [50, 1, "W Strip", 7, 122.5,
                 Point(300181, 6181236)],
                [99, 3, "Strip", 4, 67.6,
                 Point(300937, 6181897)],
                [100, 3, "SE Strip", 4, 67.6,
                 Point(300949, 6181881)],
                [102, 3, "Strip", 5, 87.6,
                 Point(300921, 6181884)],
                [103, 3, "SE Strip", 5, 87.6,
                 Point(300933, 6181869)],
                [105, 3, "Strip", 6, 107.6,
                 Point(300905, 6181872)],
                [106, 3, "SE Strip", 6, 107.6,
                 Point(300917, 6181856)],
                [108, 3, "Strip", 7, 127.6,
                 Point(300889, 6181860)],
                [109, 3, "SE Strip", 7, 127.6,
                 Point(300901, 6181844)],
                [111, 3, "Strip", 8, 147.6,
                 Point(300873, 6181848)],
                [112, 3, "SE Strip", 8, 147.6,
                 Point(300886, 6181832)],
                [114, 3, "Strip", 9, 167.6,
                 Point(300858, 6181836)],
                [115, 3, "SE Strip", 9, 167.6,
                 Point(300870, 6181820)],
                [117, 3, "Strip", 10, 187.6,
                 Point(300842, 6181823)],
                [118, 3, "SE Strip", 10, 187.6,
                 Point(300854, 6181807)],
                [120, 3, "Strip", 11, 207.6,
                 Point(300826, 6181811)],
                [121, 3, "SE Strip", 11, 207.6,
                 Point(300838, 6181795)],
                [123, 3, "Strip", 12, 227.6,
                 Point(300810, 6181799)],
                [124, 3, "SE Strip", 12, 227.6,
                 Point(300822, 6181783)],
                [126, 3, "Strip", 13, 247.6,
                 Point(300794, 6181787)],
                [127, 3, "SE Strip", 13, 247.6,
                 Point(300806, 6181771)]]

        gdf_points = GeoDataFrame(data,
                                  columns=columns,
                                  geometry='geometry',
                                  crs={'init': 'epsg:28354'})
        crs_points = crs()
        crs_points.getFromEPSG(28354)

        values_rast = os.path.realpath(
            this_dir + "/data/rasters/Yield_withStrip_PRED_2m.tif")
        control_raster = os.path.realpath(
            this_dir + "/data/rasters/Yield_withoutStrip_PRED_2m.tif")
        zone_raster = os.path.realpath(
            this_dir + "/data/rasters/k-means_3clusters_3rasters_2m.tif")

        result = ttest_analysis(gdf_points,
                                crs_points,
                                values_rast,
                                TmpDir,
                                zone_raster=zone_raster,
                                control_raster=control_raster)

        self.assertEqual(13, len(result.columns))
        file_csv = glob.glob(os.path.join(TmpDir, '*.csv'))
        file_graph = glob.glob(os.path.join(TmpDir, '*graph.png'))
        file_map = glob.glob(os.path.join(TmpDir, '*map.png'))

        self.assertEqual(4, len(file_csv))
        self.assertEqual(4, len(file_graph))
        self.assertEqual(2, len(file_map))
Пример #6
0
    def test_CreateStripTreatmentPoints(self):
        import pandas
        import geopandas
        print('Pandas Version is ', pandas.__version__)
        print('GeoPandas Version is ', geopandas.__version__)

        out_points_file = os.path.join(TmpDir,
                                       'testCreateStripTreatment_Points.shp')
        out_lines_file = os.path.join(TmpDir,
                                      'testCreateStripTreatment_Lines.shp')

        in_lines_gdf = GeoDataFrame(
            {
                'geometry': [
                    LineString([(740800, 6169700, 5), (741269, 6169700, 5)]),
                    LineString([(741000, 6169800, 6), (741003, 6170012, 6)]),
                    LineString([(741400, 6169800, 7), (741355, 6169780, 7),
                                (741300, 6169800, 7), (741250, 6169950, 7),
                                (741150, 6169950, 7), (741100, 6170000, 7)]),
                    LineString([(740800, 6169912, 8), (740900, 6170094, 8)])
                ],
                'TrialID': [1, 2, 3, 4]
            },
            crs={'init': 'epsg:28354'})

        gdf_lines_crs = crs()
        gdf_lines_crs.getFromEPSG(28354)

        out_points_gdf, out_crs, out_lines_gdf = \
            create_points_along_line(in_lines_gdf, gdf_lines_crs, 7, 25,
                                     out_points_shapefile=out_points_file,
                                     out_lines_shapefile=out_lines_file)

        # Test Points Output-----------------------------------------------------------
        self.assertIsInstance(out_points_gdf, GeoDataFrame)
        self.assertEqual(573, len(out_points_gdf))
        self.assertEqual(28354, out_crs.epsg_number)
        self.assertTrue(os.path.exists(out_points_file))
        self.assertEqual({'Point'}, set(out_points_gdf.geom_type))
        self.assertEqual([
            'E Strip', 'N Strip', 'NE Strip', 'NW Strip', 'S Strip',
            'SE Strip', 'SW Strip', 'Strip', 'W Strip'
        ], sorted(list(out_points_gdf['Strip_Name'].unique())))

        self.assertEqual(out_points_gdf.crs, out_lines_gdf.crs)

        # Test Line Output-----------------------------------------------------------
        self.assertIsInstance(out_lines_gdf, GeoDataFrame)
        self.assertEqual(12, len(out_lines_gdf))

        self.assertTrue(os.path.exists(out_lines_file))
        self.assertEqual({'LineString'}, set(out_lines_gdf.geom_type))
        self.assertEqual([
            'E Strip', 'N Strip', 'NE Strip', 'NW Strip', 'S Strip',
            'SE Strip', 'SW Strip', 'Strip', 'W Strip'
        ], sorted(list(out_lines_gdf['Strip_Name'].unique())))

        del out_points_gdf, out_lines_gdf, out_crs

        lines_crs = pyprecag_crs.crs()
        lines_crs.getFromEPSG(28353)
        lines = GeoSeries([
            LineString(
                ((740811.9268002876, 6169890.435495311),
                 (740949.7570626185, 6170048.754039882), (741145.3270294399,
                                                          6170037.578613207),
                 (741309.2332873486, 6169946.312628689), (741318.5461429111,
                                                          6169847.596359722)))
        ])
        in_lines_gdf = GeoDataFrame({'geometry': lines, 'block': ['test']})

        # without saving to file on a single multi vertex line
        out_points_gdf, out_crs, out_lines_gdf = create_points_along_line(
            in_lines_gdf, lines_crs, 31, 25)

        self.assertIsInstance(out_points_gdf, GeoDataFrame)
        self.assertEqual(69, len(out_points_gdf))

        self.assertEqual(lines_crs.epsg_number, out_crs.epsg_number)
        self.assertEqual(out_points_gdf.crs, out_lines_gdf.crs)

        self.assertEqual({'Point'}, set(out_points_gdf.geom_type))
        self.assertEqual(['N Strip', 'S Strip', 'Strip'],
                         sorted(list(out_points_gdf['Strip_Name'].unique())))

        self.assertEqual(3, len(out_lines_gdf))
        self.assertEqual(['N Strip', 'S Strip', 'Strip'],
                         (list(out_lines_gdf['Strip_Name'].unique())))
    def accept(self, *args, **kwargs):
        if not self.validate():
            return False

        try:
            # disable form via a frame, this will still allow interaction with the message bar
            self.stackedWidget.setDisabled(True)

            # clean gui and Qgis messagebars
            self.cleanMessageBars(True)
            # self.iface.messageBar().clearWidgets()

            # Change cursor to Wait cursor
            QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)

            self.iface.mainWindow().statusBar().showMessage('Processing {}'.format(self.windowTitle()))
            self.send_to_messagebar("Please wait.. QGIS will be locked... See log panel for progress.",
                                    level=Qgis.Warning,
                                    duration=0, addToLog=False, core_QGIS=False, showLogPanel=True)
            gp_layer_name = ''

            LOGGER.info('{st}\nProcessing {}'.format(self.windowTitle(), st='*' * 50))

            # Add settings to log
            settingsStr = 'Parameters:---------------------------------------'
            if self.optFile.isChecked():
                settingsStr += '\n    {:30}\t{}'.format('File:', self.lneInCSVFile.text())
                settingsStr += '\n    {:30}\t{}, {}'.format('Geometry Fields:', self.cboXField.currentText(),
                                                            self.cboYField.currentText())
                settingsStr += '\n    {:30}\t{} - {}'.format('CSV Coordinate System:', self.qgsCRScsv.crs().authid(),
                                                              self.qgsCRScsv.crs().description())
            else:
                if self.chkUseSelected.isChecked():
                    settingsStr += '\n    {:30}\t{} with {} selected features'.format('Layer:',
                                                                                      self.mcboTargetLayer.currentLayer().name(),
                                                                                      self.mcboTargetLayer.currentLayer().selectedFeatureCount())
                else:
                    settingsStr += '\n    {:30}\t{}'.format('Layer:', self.mcboTargetLayer.currentLayer().name())

            crs_units = QgsUnitTypes.toString(self.mCRSoutput.crs().mapUnits())
            settingsStr += '\n    {:30}\t{} {}'.format('Thinning Distance:', self.dsbThinDist.value(),crs_units)
            settingsStr += '\n    {:30}\t{} {}'.format("Aggregate Distance:", self.dsbAggregateDist.value(),crs_units)
            settingsStr += '\n    {:30}\t{} {}'.format("Buffer Distance:", self.dsbBufferDist.value(),crs_units)
            settingsStr += '\n    {:30}\t{} {}'.format("Shrink Distance:", self.dsbShrinkDist.value(),crs_units)

            settingsStr += '\n    {:30}\t{}'.format('Output Polygon Shapefile:', self.lneSavePolyFile.text())

            if self.lneSavePointsFile.text() == '':
                settingsStr += '\n    {:30}\t{}'.format('Saved Points Shapefile:', self.lneSavePointsFile.text())

            settingsStr += '\n    {:30}\t{} - {}\n\n'.format('Output Projected Coordinate System:',
                                                              self.mCRSoutput.crs().authid(),
                                                              self.mCRSoutput.crs().description())

            LOGGER.info(settingsStr)
            stepTime = time.time()

            if self.optFile.isChecked():
                in_epsg = int(self.qgsCRScsv.crs().authid().replace('EPSG:',''))
                in_crs = self.qgsCRScsv.crs()
            else:
                in_epsg =self.mcboTargetLayer.currentLayer().crs().authid().replace('EPSG:','')
                in_crs = self.mcboTargetLayer.currentLayer().crs()

            out_epsg = int(self.mCRSoutput.crs().authid().replace('EPSG:',''))

            filePoly = None

            gdfPoints = None
            filePoints = None

            if self.optFile.isChecked():
                if self.DEBUG:
                    filePoints = os.path.join(TEMPDIR, os.path.splitext(os.path.basename(self.lneSavePolyFile.text()))[0] + '_table2pts.shp')

                if os.path.splitext(self.lneInCSVFile.text())[-1] == '.csv':
                    gdfPoints, gdfPtsCrs = convert.convert_csv_to_points(self.lneInCSVFile.text() , out_shapefilename=filePoints,
                                                                         coord_columns=[self.cboXField.currentText(),
                                                                                        self.cboYField.currentText()],
                                                                         coord_columns_epsg=in_epsg)

                elif os.path.splitext(self.lneInCSVFile.text())[-1] in ['.xls', '.xlsx', '.ods']:
                    xls_file = pd.ExcelFile(self.lneInCSVFile.text())
                    pdfxls = xls_file.parse(self.sheet(), skiprows=self.linesToIgnore() - 1)
                    del xls_file

                    gdfPoints, gdfPtsCrs = convert.add_point_geometry_to_dataframe(pdfxls,
                                                                                   coord_columns=[
                                                                                       self.cboXField.currentText(),
                                                                                       self.cboYField.currentText()],
                                                                                   coord_columns_epsg=in_epsg)
                    del pdfxls

                LOGGER.info('{:<30} {d:<15} {}'.format('Add Geometry to Table','',
                                                          d=str(timedelta(seconds=time.time() - stepTime))))
                stepTime = time.time()
                
                if filePoints is not None:
                    describe.save_geopandas_tofile(gdfPoints, filePoints) #, file_encoding=self.file_encoding)

                if self.DISP_TEMP_LAYERS and filePoints is not None:
                    addVectorFileToQGIS(filePoints, layer_name=os.path.splitext(os.path.basename(filePoints))[0],
                                        group_layer_name='DEBUG', atTop=True)

            else:
                layerPts = self.mcboTargetLayer.currentLayer()

                if layerPts.providerType() == 'delimitedtext' or \
                        os.path.splitext(get_layer_source(layerPts))[-1] == '.vrt' or \
                        self.chkUseSelected.isChecked() or self.optFile.isChecked():

                    filePoints = os.path.join(TEMPDIR, "{}_points.shp".format(layerPts.name()))

                    if self.chkUseSelected.isChecked():
                        filePoints = os.path.join(TEMPDIR, "{}_selected_points.shp".format(layerPts.name()))

                    if os.path.exists(filePoints):
                        removeFileFromQGIS(filePoints)

                    ptsLayer = copyLayerToMemory(layerPts, layerPts.name() + "_memory", bAddUFI=True,
                                                 bOnlySelectedFeat=self.chkUseSelected.isChecked())

                    _writer = QgsVectorFileWriter.writeAsVectorFormat(ptsLayer, filePoints, "utf-8",
                                                                      self.mCRSoutput.crs(), driverName="ESRI Shapefile")

                    LOGGER.info('{:<30} {d:<15} {}'.format('Save layer/selection to file',filePoints,
                                                          d=str(timedelta(seconds=time.time() - stepTime) )))
                    stepTime = time.time()

                    del ptsLayer

                    if self.DISP_TEMP_LAYERS:
                        addVectorFileToQGIS(filePoints, layer_name=os.path.splitext(os.path.basename(filePoints))[0],
                                            group_layer_name='DEBUG', atTop=True)

                else:
                    filePoints = get_layer_source(layerPts)

            if gdfPoints is None:
                ptsDesc = describe.VectorDescribe(filePoints)
                gdfPtsCrs = ptsDesc.crs
                gdfPoints = ptsDesc.open_geo_dataframe()

            if in_crs.authid() != self.mCRSoutput.crs().authid():

                gdfPoints = gdfPoints.to_crs(epsg=out_epsg)
                gdfPtsCrs = pyprecag_crs.crs()
                gdfPtsCrs.getFromEPSG(out_epsg)

                LOGGER.info('{:<30} {d:<15} {} to {}'.format('Reproject points', in_crs.authid(),
                                                             self.mCRSoutput.crs().authid(),
                                                             d=str(timedelta(seconds=time.time() - stepTime))))

                if self.DEBUG:
                    filePoints = os.path.join(TEMPDIR, os.path.basename(self.lneSavePolyFile.text().replace('.csv', '_ptsprj.shp')))

                    removeFileFromQGIS(filePoints)
                    describe.save_geopandas_tofile(gdfPoints, filePoints)
                    if self.DISP_TEMP_LAYERS:
                        if self.DEBUG:
                            addVectorFileToQGIS(filePoints,
                                                layer_name=os.path.splitext(os.path.basename(filePoints))[0],
                                                group_layer_name='DEBUG', atTop=True)
                        else:
                            addVectorFileToQGIS(filePoints,
                                                layer_name=os.path.splitext(os.path.basename(filePoints))[0],
                                                atTop=True)
            stepTime = time.time()
            result = processing.create_polygon_from_point_trail(gdfPoints, gdfPtsCrs,
                                                                out_filename=self.lneSavePolyFile.text(),
                                                                thin_dist_m=self.dsbThinDist.value(),
                                                                aggregate_dist_m=self.dsbAggregateDist.value(),
                                                                buffer_dist_m=self.dsbBufferDist.value(),
                                                                shrink_dist_m=self.dsbShrinkDist.value())

            addVectorFileToQGIS(self.lneSavePolyFile.text(), atTop=True)

            self.cleanMessageBars(True)
            self.stackedWidget.setDisabled(False)
            QApplication.restoreOverrideCursor()
            self.iface.messageBar().popWidget()
            self.iface.mainWindow().statusBar().clearMessage()

            if result is not None:
                self.fraMain.setDisabled(False)
                self.send_to_messagebar(result, level=Qgis.Warning, duration=0, addToLog=False)
                return False  # leave dialog open

            return super(PointTrailToPolygonDialog, self).accept(*args, **kwargs)

        except Exception as err:
            QApplication.restoreOverrideCursor()
            self.iface.mainWindow().statusBar().clearMessage()
            self.cleanMessageBars(True)
            self.stackedWidget.setDisabled(False)

            self.send_to_messagebar(str(err), level=Qgis.Critical,
                                    duration=0, addToLog=True, core_QGIS=False, showLogPanel=True,
                                    exc_info=sys.exc_info())

            return False  # leave dialog open