示例#1
0
    def ClipRasterOld(self,geoFormatD):
    
        layerIn_proj = mj_gis.MjProj()
        layerIn_proj.SetFromWKT(geoFormatD['projection'])
        layerIn_proj.ReadSpatialRef()
        #BALLE
        #if layerIn_proj.epsg != tar_epsg:
        #print  bounds_epsg, tar_epsg
        if self.params.t_epsg == 0 or self.params.t_epsg == layerIn_proj.epsg:
            print ('setting dst epsg to src epsg')
            dst_epsg = layerIn_proj.epsg
        elif self.params.t_epsg != self.params.bounds_epsg:
            self.SetTarEPSGCoordinates(self.params.bounds_epsg, self.params.t_epsg)

        if self.params.t_epsg == layerIn_proj.epsg:
            gdalcmd = '/Library/Frameworks/GDAL.framework/Versions/1.11/Programs/gdal_translate'
            gdalcmd = '%(cmd)s -projwin %(ulx)f %(uly)f %(lrx)f %(lry)f' %{'cmd':gdalcmd, 'ulx':self.ulx, 'uly':self.uly, 'lrx':self.lrx, 'lry':self.lry}
            gdalcmd = '%(cmd)s %(src)s %(dst)s' %{'cmd':gdalcmd, 'src':self.layerInFPN, 'dst':self.layerOutFPN}
            print (gdalcmd)
            BALLE
            os.system(gdalcmd)                  
                #print '    Registering',self.layerOutD[key].comp.band,self.layerOutD[key].FPN
        else:
            layerOut_proj = mj_gis.MjProj()
            layerOut_proj.SetFromEPSG(self.params.t_epsg)
            layerOut_proj.SetProj4()
            gdalcmd = '/Library/Frameworks/GDAL.framework/Versions/1.11/Programs/gdalwarp -t_srs "%(cs)s"' %{'cs':layerOut_proj.proj4}
            #if self.aD['celltype'] != 'auto':
            #    gdalcmd = ' %(cmd)s -ot %(ot)s ' %{'cmd':gdalcmd, 'ot': self.aD['celltype']}

            if self.params.xres == self.params.yres == 0:
                pass 
            else:
                gdalcmd = ' %(cmd)s -tr %(tr_x)1.9f %(tr_y)1.9f ' %{'cmd':gdalcmd, 'tr_x': self.params.tr_xres, 'tr_y': self.params.tr_yres}
            gdalcmd = ' %(cmd)s -te %(ulx)s %(lry)s %(lrx)s %(uly)s ' %{'cmd':gdalcmd, 'ulx': self.ulx,'uly': self.uly, 'lrx': self.lrx, 'lry': self.lry}
            if self.params.resample:
                gdalcmd = ' %(cmd)s -r %(resample)s ' %{'cmd':gdalcmd, 'resample': self.params.resample}
    
            #linestr = ' %(s1)s -dstnodata %(nodata)d -r %(resample)s -ot %(celltype)s -of ERS' %{'s1':linestr, 'nodata': self.nodata, 'resample': self.resample, 'celltype': self.cellType}
            gdalcmd = '%(cmd)s %(src)s %(dst)s' %{'cmd':gdalcmd, 'src':self.srcFPN, 'dst':self.dstFPN}
            print (gdalcmd)
            os.system(gdalcmd)
示例#2
0
 def SetTarEPSGCoordinates(self,srcEPSG,tarEPSG):
     srcProj = mj_gis.MjProj()
     srcProj.SetFromEPSG(srcEPSG)
     #if not roiLayer.Exists() or self.process.overwrite: #or overwrite
     #    mj_gis.CreateESRIPolygonPtL(roiLayer.FPN, roiLayer.fieldDefL, region.BoundsPtL, projection.proj_cs, region.regionid)
     #Get the bounds in the original projection
     #boundsD = mj_gis.GetFeatureBounds(roiLayer.FPN,'REGIONID')
     boundsPtL = [(self.ulx, self.uly),(self.lrx,self.uly), (self.lrx,self.lry), (self.ulx,self.lry)]
     #Set lonlat projection
     tarProj = mj_gis.MjProj()
     tarProj.SetFromEPSG(tarEPSG)
     #Get the corners in lonlat
     #print 'srcProj.proj_cs',srcProj.proj_cs
     #print 'tarProj.proj_cs',tarProj.proj_cs
     coords = mj_gis.ReprojectBoundsToAny(boundsPtL,srcProj.proj_cs, tarProj.proj_cs)
     #print coords
     self.ulx = min(coords['ulx'],coords['llx'])
     self.uly = max(coords['uly'],coords['ury'])
     self.lrx = max(coords['urx'],coords['lrx'])
     self.lry = min(coords['lly'],coords['lry'])
示例#3
0
    def ClipVector(self):
        '''
        '''
        #Get the projection and other metadata for the source layer (mosaic)
        self._GetVectorProjection()

        if self.params.t_epsg == 0 or self.params.t_epsg == self.spatialRef.epsg:
            dstEPSG = self.spatialRef.epsg
        elif self.params.t_epsg != self.spatialRef.epsg:
            if self.params.t_epsg != 4326:
                #not lonlat
                self.SetTarEPSGCoordinates(self.params.bounds_epsg, self.params.t_epsg)
            else:
                dstEPSG = 4326

        if self.params.t_epsg == self.spatialRef.epsg:
            #No reprojection, just cut
            gdalcmd = os.path.join(GDALpath,'ogr2ogr')
            gdalcmd = '%(cmd)s -overwrite -clipdst %(ulx)f %(lry)f %(lrx)f %(uly)f ' %{'cmd':gdalcmd, 'ulx':self.ulx, 'uly':self.uly, 'lrx':self.lrx, 'lry':self.lry}
            if self.params.tolerance: 
                gdalcmd += '-simplify %s ' %(self.params.tolerance)
            
            gdalcmd = '%(cmd)s %(dst)s %(src)s ' %{'cmd':gdalcmd, 'src':self.srcFPN, 'dst':self.dstFPN}

            os.system(gdalcmd)                  
            #print '    Registering',self.layerOutD[key].comp.band,self.layerOutD[key].FPN
        else:
            dstProj = mj_gis.MjProj()
            dstProj.SetFromEPSG(dstEPSG)
            dstProj.SetProj4()
            gdalcmd = '/Library/Frameworks/GDAL.framework/Versions/2.1/Programs/ogr2ogr -t_srs "%(cs)s"' %{'cs':dstProj.proj4}    
            if self.params.xres == self.params.yres == 0:
                pass 
            else:
                gdalcmd = ' %(cmd)s -tr %(tr_x)1.9f %(tr_y)1.9f ' %{'cmd':gdalcmd, 'tr_x': self.params.xres, 'tr_y': self.params.yres}
            gdalcmd = ' %(cmd)s -te %(ulx)s %(lry)s %(lrx)s %(uly)s ' %{'cmd':gdalcmd, 'ulx': self.ulx,'uly': self.uly, 'lrx': self.lrx, 'lry': self.lry}
            if self.params.resample:
                gdalcmd = ' %(cmd)s -r %(resample)s ' %{'cmd':gdalcmd, 'resample': self.params.resample}

            gdalcmd = '%(cmd)s %(src)s %(dst)s' %{'cmd':gdalcmd, 'src':self.srcFPN, 'dst':self.dstFPN}
            print (gdalcmd)

            os.system(gdalcmd)
示例#4
0
 def SetTargetProj(self,tarEPSG):
     self.tarProj = mj_gis.MjProj()
     self.tarProj.SetFromEPSG(tarEPSG)
示例#5
0
    def _DefaultRegion(self, srcLayer, datum, comp, parentcat):
        '''THIS IS A DUPLICATE, SHOULD BE FIXED
        '''
        '''
        <parameter paramid = 'copycomp' paramtyp = 'text' tagorattr = 'Attr' required = 'N' defaultvalue = 'tractproject' ></parameter>
            <parameter paramid = 'defaultregion' paramtyp = 'text' tagorattr = 'Attr' required = 'Y' defaultvalue = '' ></parameter>
            <parameter paramid = 'compid' paramtyp = 'text' tagorattr = 'Attr' required = 'Y' defaultvalue = '' ></parameter>
            <parameter paramid = 'tractid' paramtyp = 'text' tagorattr = 'Attr' required = 'Y' defaultvalue = ''></parameter>
            <parameter paramid = 'tractname' paramtyp = 'text' tagorattr = 'Attr' required = 'Y' defaultvalue = ''></parameter>
            <parameter paramid = 'projid' paramtyp = 'text' tagorattr = 'Attr' required = 'Y' defaultvalue = '' ></parameter>
            <parameter paramid = 'projname' paramtyp = 'text' tagorattr = 'Attr' required = 'Y' defaultvalue = '' ></parameter>
            <parameter paramid = 'projtitle' paramtyp = 'text' tagorattr = 'Tag' required = 'Y' defaultvalue = '' ></parameter>
            <parameter paramid = 'projlabel' paramtyp = 'text' tagorattr = 'Tag' required = 'Y' defaultvalue = '' ></parameter>    
            <parameter paramid = 'tracttitle' paramtyp = 'text' tagorattr = 'Tag' required = 'Y' defaultvalue = '' ></parameter>
            <parameter paramid = 'tractlabel' paramtyp = 'text' tagorattr = 'Tag' required = 'Y' defaultvalue = '' ></parameter>
        '''

        dstLayer = self.process.dstLayerD[
            self.process.params.tractid][datum][comp]
        print(dstLayer.FPN)

        if not dstLayer._Exists() or self.process.proc.overwrite:

            #fieldDD = self._SetfieldD( query['regionid'], query['regionname'], query['regioncat'], query['stratum'], query['parentid'], query['parentcat'])
            fieldDD = self._SetfieldD(self.process.params.tractid,
                                      self.process.params.tractname, 'tract',
                                      12, self.process.params.defaultregion,
                                      parentcat)

            dstLayer.CreateVectorAttributeDef(fieldDD)

            srcLayer._GetBounds()

            if srcLayer.spatialRef.epsg == 4326:

                mj_gis.CreateESRIPolygonPtL(dstLayer.FPN, dstLayer.fieldDefL,
                                            srcLayer.BoundsPtL,
                                            srcLayer.spatialRef.proj_cs,
                                            self.process.params.tractid)
                valueL = []
                paramL = [
                    'ullon', 'ullat', 'urlon', 'urlat', 'lrlon', 'lrlat',
                    'lllon', 'lllat'
                ]
                ptL = [
                    item for sublist in srcLayer.BoundsPtL for item in sublist
                ]

                llD = dict(zip(paramL, ptL))
                bounds = (srcLayer.minx, srcLayer.miny, srcLayer.maxx,
                          srcLayer.maxy)

            else:
                print(srcLayer.spatialRef.epsg)
                GEOTRANSRUN

                dstLayer._SetBounds(srcLayer.epsg, minlon, minlat, maxlon,
                                    maxlat)

                projection = mj_gis.MjProj()
                projection.SetFromEPSG(epsg)

                if not dstLayer._Exists() or self.process.proc.overwrite:
                    mj_gis.CreateESRIPolygonPtL(dstLayer.FPN,
                                                dstLayer.fieldDefL,
                                                dstLayer.BoundsPtL,
                                                projection.proj_cs,
                                                query['regionid'])
                boundsD = mj_gis.GetFeatureBounds(layer.FPN, 'REGIONID')

                #Set lonlat projection
                lonlatproj = mj_gis.MjProj()
                lonlatproj.SetFromEPSG(4326)

                #Get the corners in lonlat
                llD = mj_gis.ReprojectBounds(layer.BoundsPtL,
                                             projection.proj_cs,
                                             lonlatproj.proj_cs)
            print(dstLayer.locus.locus)
            print(dstLayer.comp)
            print(llD)
            #self.process, layer, query, boundsD[query['regionid']], llD
            #query['regioncat'], query['regionid'], query['regionname'], query['parentid'], query['title'], query['label'])
            query = {
                'regionvcat': 'tract',
                'regionid': self.process.params.tractid,
                'regionname': self.process.params.tractname,
                'parentid': self.process.params.defaultregion,
                'title': self.process.params.tracttitle,
                'label': self.process.params.tractlabel,
                'regioncat': 'tract',
                'parentcat': parentcat,
                'epsg': srcLayer.spatialRef.epsg
            }

            self.session._InsertTractRegion(self.process, dstLayer, query,
                                            bounds, llD)
        return dstLayer
示例#6
0
def MGRSorginalNGAtiles(FP, dstFN):
    '''Expects a folder with all NGA original tiles assembled per Grid Zone Designator in shapefiles
    '''
    from math import sqrt
    driver = ogr.GetDriverByName("ESRI Shapefile")
    dstFPN = os.path.join(FP, dstFN)
    if os.path.exists(dstFPN):
        driver.DeleteDataSource(dstFPN)
    dstDS = driver.CreateDataSource(dstFPN)
    dst_srs = osr.SpatialReference()
    dst_srs.ImportFromEPSG(4326)
    dstLayer = dstDS.CreateLayer('tiles', dst_srs, geom_type=ogr.wkbPolygon)
    dst_srs = mj_gis.MjProj()
    dst_srs.SetFromEPSG(4326)
    mgrs_field = ogr.FieldDefn("MGRS", ogr.OFTString)
    mgrs_field.SetWidth(8)
    dstLayer.CreateField(mgrs_field)

    gzd_field = ogr.FieldDefn("GZD", ogr.OFTString)
    gzd_field.SetWidth(8)
    dstLayer.CreateField(gzd_field)

    for FPN in glob.glob(os.path.join(FP, '*.zip')):
        if 'arctic' in FPN.lower():
            continue
        FN = os.path.split(FPN)[1]
        #print ('FN',FN)
        zipFPN = os.path.join(FP, FN)
        #print ('zipFPN',zipFPN)

        zipFP = os.path.splitext(zipFPN)[0]
        print(zipFP)
        '''
        zip_ref = zipfile.ZipFile(zipFPN, 'r')
        zip_ref.extractall(zipFP)
        zip_ref.close()
        '''

        for root, dirs, files in os.walk(zipFP):
            shpFPN = ''
            for name in files:
                if name.endswith("shp"):
                    shpFPN = os.path.join(root, name)

            if os.path.isfile(shpFPN):
                #print ('    root',root)
                #print ('    ',shpFPN)
                srcDS = driver.Open(shpFPN, 0)
                srcLayer = srcDS.GetLayer()
                spatialRef = srcLayer.GetSpatialRef()
                src_srs = mj_gis.MjProj()
                src_srs.SetProj(spatialRef)
                mgrsL = []
                layerDefinition = srcLayer.GetLayerDefn()
                fieldNameL = []
                for i in range(layerDefinition.GetFieldCount()):
                    fieldNameL.append(
                        layerDefinition.GetFieldDefn(i).GetName())
                for feature in srcLayer:
                    if 'MGRS' in fieldNameL:
                        MGRS = feature.GetField("MGRS")
                    elif ('GZD' in fieldNameL and '100kmSQ_ID' in fieldNameL):
                        MGRS = '%(gzd)s%(EN)s' % {
                            'gzd': feature.GetField("GZD"),
                            'EN': feature.GetField("100kmSQ_ID")
                        }
                    else:
                        print(fieldNameL)
                        UNKNOWN
                    srcGeom = mj_gis.Geometry()
                    srcGeom.GeomFromFeature(feature)

                    if srcGeom.shapelyGeom.geom_type == 'MultiPolygon':
                        print('    ', MGRS, srcGeom.shapelyGeom.geom_type)
                        #print (srcGeom.shapelyGeom)
                        polygeom = mj_gis.Geometry()
                        polygeom.MultPolyToSinglePoly(srcGeom.shapelyGeom)
                        #print (polygeom.shapelyGeom)
                        #BALLE
                        #for poly in srcGeom.shapelyGeom:
                        print(polygeom.shapelyGeom)
                        edgeL = list(polygeom.shapelyGeom.exterior.coords)
                    else:
                        edgeL = list(srcGeom.shapelyGeom.exterior.coords)

                    boundsL = srcGeom.BoundsToPoly()

                    cL = ['ul', 'ur', 'lr', 'll']
                    cornerD = {}
                    cornerL = list(boundsL.exterior.coords)

                    for i, c in enumerate(cL):
                        dist = 100000
                        for j, d in enumerate(edgeL):
                            testdist = sqrt((cornerL[i][0] - d[0])**2 +
                                            (cornerL[i][1] - d[1])**2)
                            if testdist < dist:
                                dp = j
                                dist = testdist
                        if dp >= len(edgeL) or dist == 100000:
                            print('MGRS', MGRS)
                            print('boundsL', boundsL)
                            print('dist', dist)
                            print('dp', dp)
                            print('edgeL', edgeL)
                            print('edgeL', len(edgeL))
                            BALLE
                        cornerD[c] = edgeL[dp]
                    #print (cornerD)
                    '''
                    print ('corner',cornerL)
                    FISK
                    print ('MGRS',MGRS)
                    print ('len',len(cornerL))
                    
                    
                    if len(cornerL) > 5:
                        tol = 1
                        factor = 1.1
                        while len(cornerL) > 5:
                            tol *= factor
                            corners = srcGeom.shapelyGeom.simplify(tol, preserve_topology=False)
                            if not corners.is_empty:
                                cornerL = list(corners.exterior.coords)
                            if tol > 1000000:
                                factor = 0.9

                   
                    coords = list(corners.exterior.coords)
                    if len(coords) == 5:
                        coords.pop(4)
                    if len(coords) != 4:
                        print ('coords',coords)
                        ERROR
    
                    ysortedCoords = sorted(coords, key=itemgetter(1))
                    minyCoords = ysortedCoords[0:2]
                    maxyCoords = ysortedCoords[2:4]
                    cornerD = {}
                    if minyCoords[0][0] < minyCoords[1][0]:
                        cornerD['ll'] = minyCoords[0]
                        cornerD['lr'] = minyCoords[1]
                    else:
                        cornerD['ll'] = minyCoords[1]
                        cornerD['lr'] = minyCoords[0]
                                    
                    if maxyCoords[0][0] < maxyCoords[1][0]:
                        cornerD['ul'] = maxyCoords[0]
                        cornerD['ur'] = maxyCoords[1]
                    else:
                        cornerD['ul'] = maxyCoords[1]
                        cornerD['ur'] = maxyCoords[0]
                    
                    #print (cornerD)
                    '''

                    cornerPtL = (cornerD['ul'], cornerD['ur'], cornerD['lr'],
                                 cornerD['ll'])

                    cornerGeom = mj_gis.Geometry()
                    cornerGeom.PointsToPolygonGeom(cornerPtL)
                    #print (cornerGeom.shapelyGeom)
                    #cornerGeom.SetShapelyGeom(corners)

                    #print ('corners',corners)
                    #cornerGeom = mj_gis.Geometry()
                    #cornerGeom.SetShapelyGeom(corners)

                    #BALLE
                    #bounds = srcGeom.BoundsToPoly()
                    #print (bounds)
                    '''
                    featureDefn = dstLayer.GetLayerDefn()
                    transfeat = ogr.Feature(featureDefn)       
                    transfeat.SetGeometry(srcGeom.ogrGeom)
                    transfeat.SetField("MGRS", 'orig')
                            
                    transfeat.SetField("GZD", 'orig')
                    dstLayer.CreateFeature(transfeat)
                    transfeat = None
                    
                    
                    featureDefn = dstLayer.GetLayerDefn()
                    transfeat = ogr.Feature(featureDefn) 
                    #      
                    transfeat.SetGeometry(cornerGeom.ogrGeom)
                    transfeat.SetField("MGRS", 'bounds')
                            
                    transfeat.SetField("GZD", 'bounds')
                    dstLayer.CreateFeature(transfeat)
                    transfeat = None
                            
                    srcDS = None 
                    dstDS = None      
                            
                    BALLE
                    '''
                    dstGeom = src_srs.ReprojectGeom(cornerGeom, dst_srs)

                    #bounds = dstGeom.BoundsToPoly()
                    #print (bounds)

                    if 'MGRS' in fieldNameL:
                        MGRS = feature.GetField("MGRS")
                    elif ('GZD' in fieldNameL and '100kmSQ_ID' in fieldNameL):
                        MGRS = '%(gzd)s%(EN)s' % {
                            'gzd': feature.GetField("GZD"),
                            'EN': feature.GetField("100kmSQ_ID")
                        }
                    else:
                        print(fieldNameL)
                        UNKNOWN
                    if MGRS in mgrsL:
                        #continue
                        pass
                    else:
                        boundsL = list(dstGeom.shapelyGeom.exterior.coords)
                        print(len(boundsL), boundsL,
                              abs(int(round(boundsL[0][0]))))
                        if MGRS[0:2] == '01' and abs(int(round(
                                boundsL[0][0]))) == 180:
                            print('    fixing')
                            boundsL[0] = (-180, boundsL[0][1])
                            boundsL[3] = (-180, boundsL[3][1])
                            if len(boundsL) > 4:
                                boundsL[4] = (-180, boundsL[4][1])

                        if MGRS[0:2] == '60' and abs(int(round(
                                boundsL[0][1]))) == 180:
                            boundsL[1] = (180, boundsL[1][1])
                            boundsL[2] = (180, boundsL[2][1])
                        lonL = [p[0] for p in boundsL]
                        latL = [p[1] for p in boundsL]
                        if boundsL[0][0] > boundsL[1][0]:
                            printstr = 'Skipping %(mgrs)s' % {'mgrs': MGRS}
                            #if MGRS[0:2] == '01':
                            #    boundsL[0][0] = boundsL[3][0] = boundsL[4][0] = -180
                            print(printstr)
                            print(boundsL)
                            BALLE

                        elif boundsL[1][0] - boundsL[0][0] > 6.0001:
                            printstr = 'Skipping %(mgrs)s' % {'mgrs': MGRS}

                            print(printstr)
                            print(boundsL[0][0], boundsL[1][0])
                            print(boundsL)

                            testGeom = mj_gis.Geometry()
                            testGeom.GeomFromFeature(feature)
                            print(testGeom.shapelyGeom)

                            BALLE
                        elif round(min(lonL), 2) < -180 or round(max(lonL),
                                                                 2) > 180:
                            printstr = 'Skipping %(mgrs)s' % {'mgrs': MGRS}

                            print(printstr)
                            print(boundsL)
                            BALLE
                        elif round(min(latL), 2) < -90 or round(max(latL),
                                                                2) > 90:
                            printstr = 'Skipping %(mgrs)s' % {'mgrs': MGRS}

                            print(printstr)
                            print(boundsL)
                            BALLE

                        else:

                            featureDefn = dstLayer.GetLayerDefn()
                            transfeat = ogr.Feature(featureDefn)

                            transfeat.SetGeometry(dstGeom.ogrGeom)

                            transfeat.SetField("MGRS", MGRS)

                            transfeat.SetField("GZD", feature.GetField("GZD"))
                            dstLayer.CreateFeature(transfeat)
                            transfeat = None
                            mgrsL.append(MGRS)
                # Save and close DataSource
                srcDS = None
    dstDS = None
示例#7
0
def MGRSorginalNGAtilesV2(FP, dstFN):
    '''Expects a folder with all NGA original tiles assembled per Grid Zone Designator in shapefiles
    '''
    driver = ogr.GetDriverByName("ESRI Shapefile")
    dstFPN = os.path.join(FP, dstFN)
    if os.path.exists(dstFPN):
        driver.DeleteDataSource(dstFPN)
    dstDS = driver.CreateDataSource(dstFPN)
    dst_srs = osr.SpatialReference()
    dst_srs.ImportFromEPSG(4326)
    dstLayer = dstDS.CreateLayer('tiles', dst_srs, geom_type=ogr.wkbPolygon)
    dst_srs = mj_gis.MjProj()
    dst_srs.SetFromEPSG(4326)
    mgrs_field = ogr.FieldDefn("MGRS", ogr.OFTString)
    mgrs_field.SetWidth(8)
    dstLayer.CreateField(mgrs_field)

    gzd_field = ogr.FieldDefn("GZD", ogr.OFTString)
    gzd_field.SetWidth(8)
    dstLayer.CreateField(gzd_field)

    area_field = ogr.FieldDefn("AREAKM2", ogr.OFTReal)
    area_field.SetWidth(8)
    area_field.SetPrecision(2)
    dstLayer.CreateField(area_field)

    easting_field = ogr.FieldDefn("EASTING", ogr.OFTString)
    easting_field.SetWidth(12)
    dstLayer.CreateField(easting_field)

    northing_field = ogr.FieldDefn("NORTHING", ogr.OFTString)
    northing_field.SetWidth(12)
    dstLayer.CreateField(northing_field)

    maxwidth_field = ogr.FieldDefn("MAXWIDTH", ogr.OFTInteger)
    maxwidth_field.SetWidth(8)
    dstLayer.CreateField(maxwidth_field)

    maxheight_field = ogr.FieldDefn("MAXHEIGHT", ogr.OFTInteger)
    maxheight_field.SetWidth(8)
    dstLayer.CreateField(maxheight_field)

    for FPN in glob.glob(os.path.join(FP, '*.zip')):
        if 'arctic' in FPN.lower():
            continue
        FN = os.path.split(FPN)[1]
        zipFPN = os.path.join(FP, FN)

        zipFP = os.path.splitext(zipFPN)[0]
        print(zipFP)
        '''
        zip_ref = zipfile.ZipFile(zipFPN, 'r')
        zip_ref.extractall(zipFP)
        zip_ref.close()
        '''
        for root, dirs, files in os.walk(zipFP):
            shpFPN = ''
            for name in files:
                if name.endswith("shp"):
                    shpFPN = os.path.join(root, name)

            if os.path.isfile(shpFPN):
                srcDS = driver.Open(shpFPN, 0)
                srcLayer = srcDS.GetLayer()
                spatialRef = srcLayer.GetSpatialRef()
                src_srs = mj_gis.MjProj()
                src_srs.SetProj(spatialRef)
                mgrsL = []
                layerDefinition = srcLayer.GetLayerDefn()
                fieldNameL = []
                for i in range(layerDefinition.GetFieldCount()):
                    fieldNameL.append(
                        layerDefinition.GetFieldDefn(i).GetName())
                for feature in srcLayer:
                    if 'MGRS' in fieldNameL:
                        MGRS = feature.GetField("MGRS")
                    elif ('GZD' in fieldNameL and '100kmSQ_ID' in fieldNameL):
                        MGRS = '%(gzd)s%(EN)s' % {
                            'gzd': feature.GetField("GZD"),
                            'EN': feature.GetField("100kmSQ_ID")
                        }
                    else:
                        print(fieldNameL)
                        exit()
                    srcGeom = mj_gis.Geometry()
                    srcGeom.GeomFromFeature(feature)

                    if srcGeom.shapelyGeom.geom_type == 'MultiPolygon':
                        #Special routine converting MultiPolygon to Polygon
                        polygeom = mj_gis.Geometry()
                        polygeom.MultPolyToSinglePoly(srcGeom.shapelyGeom)
                        edgeL = list(polygeom.shapelyGeom.exterior.coords)
                    else:
                        edgeL = list(srcGeom.shapelyGeom.exterior.coords)
                    edgeGeom = mj_gis.Geometry()
                    edgeGeom.PointsToPolygonGeom(edgeL)
                    areakm2 = int(round(edgeGeom.shapelyGeom.area / 1000000))

                    #Retrieve the maximum width and the maximum height
                    xL = [p[0] for p in edgeL]
                    yL = [p[1] for p in edgeL]
                    maxwidth = int(round(max(xL) - min(xL)))
                    maxheight = int(round(max(yL) - min(yL)))

                    dstGeom = src_srs.ReprojectGeom(edgeGeom, dst_srs)

                    if 'MGRS' in fieldNameL:
                        MGRS = feature.GetField("MGRS")
                    elif ('GZD' in fieldNameL and '100kmSQ_ID' in fieldNameL):
                        MGRS = '%(gzd)s%(EN)s' % {
                            'gzd': feature.GetField("GZD"),
                            'EN': feature.GetField("100kmSQ_ID")
                        }
                    else:
                        print(fieldNameL)
                        exit()

                    if 'EASTING' in fieldNameL:
                        easting = feature.GetField("EASTING")
                    elif 'Easting' in fieldNameL:
                        easting = feature.GetField("Easting")
                    elif 'EASTINT' in fieldNameL:
                        easting = feature.GetField("EASTINT")
                    elif 'E3' in fieldNameL:
                        easting = feature.GetField("E3")
                    else:
                        print(fieldNameL)
                        exit()

                    if MGRS in mgrsL:
                        #continue
                        pass
                    else:
                        boundsL = list(dstGeom.shapelyGeom.exterior.coords)

                        fixBoundsL = []
                        #
                        #Force the edges longitude = -180 and 180
                        if MGRS[0:2] == '01':
                            for bounds in boundsL:
                                if round(bounds[0], 4) == -180.0000:
                                    #print(bounds )
                                    fixBoundsL.append((-180.0, bounds[1]))
                                elif int(round(bounds[0])) == 180:
                                    fixBoundsL.append((-180.0, bounds[1]))
                                else:
                                    fixBoundsL.append(bounds)
                        elif MGRS[0:2] == '60':
                            for bounds in boundsL:
                                if round(bounds[0], 4) == 180.0000:
                                    #print(bounds )
                                    fixBoundsL.append((180.0, bounds[1]))
                                elif int(round(bounds[0])) == -180:
                                    fixBoundsL.append((180.0, bounds[1]))
                                else:
                                    fixBoundsL.append(bounds)
                        else:
                            fixBoundsL = boundsL
                        #Set the geometry
                        dstGeom = mj_gis.Geometry()
                        dstGeom.PointsToPolygonGeom(fixBoundsL)
                        #Create the feature
                        featureDefn = dstLayer.GetLayerDefn()
                        transfeat = ogr.Feature(featureDefn)

                        transfeat.SetGeometry(dstGeom.ogrGeom)

                        transfeat.SetField("MGRS", MGRS)

                        transfeat.SetField("GZD", feature.GetField("GZD"))

                        transfeat.SetField("AREAKM2", areakm2)

                        transfeat.SetField("EASTING", easting)

                        transfeat.SetField("NORTHING",
                                           feature.GetField("NORTHING"))

                        transfeat.SetField("MAXWIDTH", maxwidth)

                        transfeat.SetField("MAXHEIGHT", maxheight)

                        dstLayer.CreateFeature(transfeat)
                        transfeat = None
                        mgrsL.append(MGRS)
                # Save and close DataSource
                srcDS = None
    # Save and close DataSource
    dstDS = None
示例#8
0
    def _DefaultRegion(self, session):
        '''
        '''
        for locus in self.process.dstLayerD:
            for datum in self.process.dstLayerD[locus]:
                for comp in self.process.dstLayerD[locus][datum]:
                    query = self.process.proc.paramsD
                    fieldDD = self._SetfieldD(query['regionid'],
                                              query['regionname'],
                                              query['regioncat'],
                                              query['stratum'],
                                              query['parentid'],
                                              query['parentcat'])
                    layer = self.process.dstLayerD[locus][datum][comp]

                    #The destination region must be forced,this is because the locus to be created did not exists when chekcing for the feault locus
                    if self.verbose:
                        print('        forcing region from to', locus,
                              self.process.proc.paramsD['regionid'])

                    if layer.locus.locus != self.process.proc.paramsD[
                            'regionid']:
                        layer.locus.locus = self.process.proc.paramsD[
                            'regionid']

                    if layer.locus.path != self.process.proc.paramsD[
                            'regionid']:
                        layer.locus.path = self.process.proc.paramsD[
                            'regionid']

                    layer._SetPath()

                    if self.verbose:
                        print('        filepath', layer.FPN)

                    layer.CreateVectorAttributeDef(fieldDD)
                    layer._SetBounds(query['epsg'], query['minlon'],
                                     query['minlat'], query['maxlon'],
                                     query['maxlat'])

                    projection = mj_gis.MjProj()
                    projection.SetFromEPSG(query['epsg'])

                    if not layer._Exists() or self.process.proc.overwrite:
                        mj_gis.CreateESRIPolygonPtL(layer.FPN, layer.fieldDefL,
                                                    layer.BoundsPtL,
                                                    projection.proj_cs,
                                                    query['regionid'])
                    boundsD = mj_gis.GetFeatureBounds(layer.FPN, 'REGIONID')

                    #Set lonlat projection
                    lonlatproj = mj_gis.MjProj()
                    lonlatproj.SetFromEPSG(4326)

                    #Get the corners in lonlat
                    llD = mj_gis.ReprojectBounds(layer.BoundsPtL,
                                                 projection.proj_cs,
                                                 lonlatproj.proj_cs)

                    session._InsertDefRegion(self.process, layer, query,
                                             boundsD[query['regionid']], llD,
                                             self.process.proc.overwrite,
                                             self.process.proc.delete)
示例#9
0
def ModisTileCoords():
    '''
    '''
    #Check  the karttur password (local .netrc file
    session = SelectUser()
    userid = 'karttur'
    userData = session._SelectUserCreds(userid, '')
    if userData[2] < 10:
        exitstr = 'The user does not have sufficient rights for this process'
        exit(exitstr)

    #Open the db session for MODIS
    session = ManageRegion()
    SINproj = mj_gis.MjProj()
    SINproj.SetFromProj4(
        '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs'
    )
    LatLonproj = mj_gis.MjProj()
    LatLonproj.SetFromProj4(
        '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0')
    ptL = []
    for lon in range(360):
        ptL.append((lon - 180, 90))
    for lat in range(180):
        ptL.append((180, -1 * (lat - 90)))
    for lon in range(360):
        ptL.append((-1 * (lon - 180), -90))
    for lat in range(180):
        ptL.append((-180, lat - 90))
    #worldgeom = mj_gis.ShapelyPoygon('polygon',ptL)
    #print ('ptL',ptL)
    #worldgeom = mj_gis.Geometry()
    #worldgeom.PointsToPolygonGeom(ptL)
    #print (worldgeom.shapelyGeom)
    #SNULLEKOLLA
    worldgeom = mj_gis.ShapelyPolyGeom(ptL)

    worldgeom.ShapelyToOgrGeom()
    worldgeom.GeoTransform(LatLonproj, SINproj)
    worldgeom.OgrGeomToShapely()

    home = path.expanduser("~")
    tarShpFP = path.join(path.dirname(__file__), 'data')

    if not path.exists(tarShpFP):
        makedirs(tarShpFP)
    FN = 'modtiles-multi_karttur_global_epsg6842.shp'
    tarShpFPN = path.join(tarShpFP, FN)
    fieldDefD = {
        'type': 'string',
        'transfer': 'constant',
        'source': 'globe',
        'width': 8
    }
    fieldDefL = [mj_gis.FieldDef('name', fieldDefD)]
    mj_gis.CreateESRIPolygonGeom(tarShpFPN, fieldDefL, worldgeom,
                                 SINproj.proj_cs, 'globe')
    # Create a shape file for all individual tiles in SIN proj
    FN = 'modtiles-single_karttur_global_epsg6842.shp'
    tarShpFPN = path.join(tarShpFP, FN)
    tarDS, tarLayer = mj_gis.ESRICreateDSLayer(tarShpFPN, SINproj.proj_cs,
                                               'polygon', 'tiles', fieldDefL)
    # Create a shape file for all individual tiles in Geographic coordinates
    FN = 'modtiles_karttur_global_0.shp'
    tarShpFPN = path.join(tarShpFP, FN)
    tarDSLonLat, tarLayerLonLat = mj_gis.ESRICreateDSLayer(
        tarShpFPN, LatLonproj.proj_cs, 'polygon', 'tiles', fieldDefL)

    #create a region with all tiles
    tlen = 20015109.3539999984204769
    tlen /= 18
    #regioncat = 'globe'
    #regionid = 'globe'
    for h in range(36):
        minx = tlen * (18 - 36) + h * tlen
        maxx = minx + tlen
        for v in range(18):
            maxy = tlen * (9 - 18) + (18 - v) * tlen
            miny = maxy - tlen
            ptL = [(minx, maxy), (maxx, maxy), (maxx, miny), (minx, miny)]
            #TGTODO MOVE TO mj_gis
            tilegeom = mj_gis.ShapelyMultiPointGeom(ptL)
            #convert to ogr
            tilegeom.ShapelyToOgrGeom()

            #write target feature

            tilegeom.GeoTransform(SINproj, LatLonproj)
            tilegeom.OgrGeomToShapely()
            coordL = []
            for point in [ptgeom for ptgeom in tilegeom.shapelyGeom]:
                coordL.extend(
                    [list(point.coords)[0][0],
                     list(point.coords)[0][1]])
            ullon, ullat, urlon, urlat, lrlon, lrlat, lllon, lllat = coordL
            tilepoly = mj_gis.ShapelyPolyGeom([(minx, maxy), (maxx, maxy),
                                               (maxx, miny), (minx, miny)])
            #Test if this tile is inside the globe

            if tilepoly.shapelyGeom.intersects(worldgeom.shapelyGeom):
                if h < 10:
                    htile = 'h0%s' % (h)
                else:
                    htile = 'h%s' % (h)
                if v < 10:
                    vtile = 'v0%s' % (v)
                else:
                    vtile = 'v%s' % (v)
                hvtile = '%s%s' % (htile, vtile)
                polytilegeom = mj_gis.ShapelyPolyGeom(ptL)
                polytilegeom.ShapelyToOgrGeom()
                fieldDefD = {
                    'type': 'string',
                    'transfer': 'constant',
                    'source': hvtile,
                    'width': 8
                }
                fieldDefL = [mj_gis.FieldDef('name', fieldDefD)]
                #create target feature
                tarFeat = mj_gis.ogrFeature(tarLayer)
                print(polytilegeom.shapelyGeom)
                tarFeat.CreateOgrFeature(polytilegeom.ogrGeom, fieldDefL)
                if h == 17:
                    pass
                else:
                    #to be correct 5 points are needed and also the lat must be fitted
                    if h < 18 and ullon > 0:
                        ullon = -180
                        #continue
                    if h < 18 and lllon > 0:
                        lllon = -180
                        #continue
                    if h < 18 and urlon > 0:
                        urlon = -180
                        #continue
                    if h < 18 and lrlon > 0:
                        lrlon = -180
                        #continue
                    if h > 18 and urlon < 0:
                        urlon = 180
                        #continue
                    if h > 18 and lrlon < 0:
                        lrlon = 180
                        #continue
                    if h > 18 and ullon < 0:
                        ullon = 180
                        #continue
                    if h > 18 and lllon < 0:
                        lllon = 180
                    if hvtile == 'h24v01':
                        urlon = 180
                    if hvtile == 'h24v16':
                        lrlon = 180
                    if hvtile == 'h11v01':
                        ullon = -180
                    if hvtile == 'h11v16':
                        lllon = -180

                if ullon > urlon:
                    print('ERROR', 'ullon > urlon', hvtile, ullon, urlon)

                if lllon > lrlon:
                    print('ERROR', 'lllon > lrlon', hvtile, lllon, lrlon)

                #
                polytilegeom = mj_gis.ShapelyPolyGeom([(ullon, ullat),
                                                       (urlon, urlat),
                                                       (lrlon, lrlat),
                                                       (lllon, lllat)])
                polytilegeom.ShapelyToOgrGeom()
                #polytilegeom.GeoTransform(SINproj,LatLonproj)
                #create target feature
                tarLonLatFeat = mj_gis.ogrFeature(tarLayerLonLat)
                tarLonLatFeat.CreateOgrFeature(polytilegeom.ogrGeom, fieldDefL)
                west, south, east, north = polytilegeom.shapelyGeom.bounds

                session._InsertModisTileCoord(hvtile, h, v, minx, maxy, maxx,
                                              miny, west, south, east, north,
                                              ullat, ullon, lrlon, lrlat,
                                              urlon, urlat, lllon, lllat)
                #print ('session.name',session.name)
                query = {
                    'system': 'system',
                    'table': 'regions',
                    'h': h,
                    'v': v,
                    'hvtile': hvtile,
                    'regionid': 'global',
                    'regioncat': 'global',
                    'regiontype': 'default',
                    'delete': False
                }
                session._InsertModisRegionTile(query)
    tarDS.CloseDS()
    tarDSLonLat.CloseDS()
    session._Close()
    print('Check the shaoe file', tarShpFPN)
    return (tarShpFPN)
示例#10
0
    def _DefaultRegFromVec(self, session):
        for locus in self.process.dstLayerD:
            if self.verbose:
                print('locus', locus)
            for datum in self.process.dstLayerD[locus]:
                if self.verbose:
                    print('datum', datum)
                for comp in self.process.dstLayerD[locus][datum]:

                    dstLayer = self.process.dstLayerD[locus][datum][comp]
                    #if not dstLayer._Exists() or self.process.proc.overwrite:
                    srcLayer = self.process.srcLayerD[locus][datum][comp]
                    if not path.isfile(srcLayer.FPN):
                        exitstr = 'setup_process_class: No source layer in _DefaultRegFromVec', srcLayer.FPN
                        exit(exitstr)
                    p = self.process.params
                    fieldL = [
                        p.idcol, p.namecol, p.categorycol, p.parentidcol,
                        p.parentcatcol, p.stratumcol, p.titlecol, p.labelcol
                    ]
                    fieldD = mj_gis.GetFeatureAttributeList(
                        srcLayer.FPN, fieldL, p.idcol)
                    if not fieldD:
                        exit(
                            'setup_process_class: fieldD failed in _DefaultRegFromVec'
                        )
                    for key in fieldD:
                        fieldDD = self._SetfieldD(
                            str(fieldD[key][p.idcol]),
                            str(fieldD[key][p.namecol]),
                            str(fieldD[key][p.categorycol]),
                            int(fieldD[key][p.stratumcol]),
                            str(fieldD[key][p.parentidcol]),
                            str(fieldD[key][p.parentcatcol]))
                        regionid, regioncat, parentid, regionname, parentcat = fieldD[
                            key][p.idcol], fieldD[key][p.categorycol], fieldD[
                                key][p.parentidcol], fieldD[key][
                                    p.namecol], fieldD[key][p.parentcatcol]
                        parentid = str(parentid.lower()).replace(' ', '-')
                        #regionname = str(regionname.lower()).replace(' ', '-')
                        regionid = str(regionid.lower()).replace(' ', '-')
                        if self.verbose:
                            print('        forcing dst region from to', locus,
                                  regionid)

                        dstLayer.locus.locus = regionid
                        dstLayer.locus.path = regionid
                        if self.verbose:
                            print(
                                '        forcing comp band and prefix region to "roi"'
                            )
                        #print ('dstLayer.comp.band',dstLayer.comp.band)
                        dstLayer.comp.band = dstLayer.comp.prefix = 'roi'
                        dstLayer.comp._SetCompid()
                        #print ('dstLayer.comp.compid',dstLayer.comp.compid)
                        dstLayer._SetPath()

                        dstLayer.CreateVectorAttributeDef(fieldDD)
                        fieldname = p.idcol
                        valueLL = [[fieldD[key][p.idcol]]]
                        if not dstLayer._Exists(
                        ) or self.process.proc.overwrite:
                            mj_gis.ExtractFeaturesToNewDS(
                                srcLayer.FPN, dstLayer.FPN, fieldname, valueLL,
                                dstLayer.fieldDefL)
                            fieldname = 'REGIONID'
                            #Get the epsg and bounds
                            boundsD = mj_gis.GetFeatureBounds(
                                dstLayer.FPN, fieldname)
                            if len(boundsD) != 1:
                                exitstr = 'Default regions must consist on only one (1) feature (polygon or multipolygon): %s' % (
                                    dstLayer.FPN)
                                exit(exitstr)
                            projection = mj_gis.GetVectorProjection(
                                dstLayer.FPN)
                            k = list(boundsD)[0]
                            bounds = boundsD[k]

                            dstLayer._SetBounds(projection.epsg, boundsD[k][0],
                                                boundsD[k][1], boundsD[k][2],
                                                boundsD[k][3])

                            #Set lonlat projection
                            lonlatproj = mj_gis.MjProj()
                            lonlatproj.SetFromEPSG(4326)
                            #Get the corners in lonlat
                            llD = mj_gis.ReprojectBounds(
                                dstLayer.BoundsPtL, projection.proj_cs,
                                lonlatproj.proj_cs)

                            title = label = 'default region %s' % (regionid)
                            query = {
                                'regionname': regionname,
                                'regioncat': regioncat,
                                'parentid': parentid,
                                'parentcat': parentcat,
                                'regionid': regionid,
                                'title': title,
                                'label': label,
                                'epsg': projection.epsg
                            }
                            session._InsertDefRegion(self.process, dstLayer,
                                                     query, bounds, llD,
                                                     self.process.overwrite,
                                                     self.process.delete)

                        else:
                            boundsD = mj_gis.GetFeatureBounds(
                                dstLayer.FPN, "REGIONID")
                            key = list(boundsD.keys())[0]
                            bounds = boundsD[key]
                            minx, miny, maxx, maxy = bounds
                            llparams = [
                                'ullon', 'ullat', 'urlon', 'urlat', 'lrlon',
                                'lrlat', 'lllon', 'lllat'
                            ]
                            llvalues = [
                                minx, maxy, maxx, maxy, maxx, miny, minx, miny
                            ]
                            llD = dict(zip(llparams, llvalues))
                            title = label = 'default region %s' % (regionid)
                            query = {
                                'regionname': regionname,
                                'regioncat': regioncat,
                                'parentid': parentid,
                                'parentcat': parentcat,
                                'regionid': regionid,
                                'title': title,
                                'label': label,
                                'epsg': 4326
                            }

                            session._InsertDefRegion(self.process, dstLayer,
                                                     query, bounds, llD,
                                                     self.process.overwrite,
                                                     self.process.delete)

                            if self.verbose:
                                printstr = '    Layer %s already exists, skipping' % (
                                    dstLayer.FPN)
                                print(printstr)