Ejemplo n.º 1
0
def main():
    args = parser.parse_args()
    sridentifier = Sridentify(mode='cli')
    sridentifier.from_file(args.prj)
    srid = sridentifier.get_epsg()
    if srid is not None:
        sys.stdout.write(str(srid) + '\n')
Ejemplo n.º 2
0
def main():
    args = parser.parse_args()
    sridentifier = Sridentify(mode='cli', call_remote_api=args.call_remote_api)
    sridentifier.from_file(args.prj)
    srid = sridentifier.get_epsg()
    if srid is not None:
        sys.stdout.write(str(srid) + '\n')
Ejemplo n.º 3
0
 def test_non_text_file_raises_exception(self):
     ident = Sridentify(call_remote_api=False)
     with self.assertRaises(UnicodeDecodeError):
         ident.from_file(
             os.path.join(
                 self.fixtures_dir,
                 'Central_Business_District.dbf'
             )
         )
Ejemplo n.º 4
0
    def test_non_text_file_does_not_raise_if_not_strict(self):

        ident = Sridentify(call_remote_api=False, strict=False)
        ident.from_file(
            os.path.join(
                self.fixtures_dir,
                'Central_Business_District.dbf'
            )
        )  # should not raise
        self.assertIsNone(ident.get_epsg())
Ejemplo n.º 5
0
def EPSGfromWKT(wkt):
    """ 
    Function to get the EPSG code from the WKT (Well-Known-Text).
    
    Arguments: 
        wkt: The WKT.
    """
    try:
        ident = Sridentify(prj=str(wkt))
        EPSG = ident.get_epsg()
    except:
        log.error('Failed to get EPSG from WKT')
        raise
    
    return str(EPSG)
def get_projection(prj_file):
    """
    Determine the EPSG code from .prj file.

    :param prj_file: filepath to the .prj file.
    :type prj_file: str.
    :return:
    :rtype: int.

    >>> get_projection('/home/example_user/example_shapefile_directory/example_shapefile.prj')
    4326
    """
    srider = Sridentify()
    srider.from_file(prj_file)
    return srider.get_epsg()
Ejemplo n.º 7
0
 def test_from_file_on_large_file_does_not_load_entirety_into_memory(self):
     ident = Sridentify(call_remote_api=False, strict=False)
     ident.from_file(
         # on my system `du -b` reports this file to be 23004 bytes
         os.path.join(
             self.fixtures_dir,
             'Central_Business_District.shp'
         )
     )
     self.assertLess(
         sys.getsizeof(ident.prj),
         1200
         # from_file only requests the first 1024 bytes, but
         # there could be some GC overhead with sys.getsizeof,
         # so we mark it up a little bit here.
         # https://docs.python.org/3/library/sys.html#sys.getsizeof
     )
Ejemplo n.º 8
0
def lookupSpatialReferenceID(wellKnownText):
    """
    This function can be used to look up the EPSG spatial reference system using the web service available at:
    http://prj2epsg.org

    Args:
        wellKnownText (str): The Well Known Text definition of the spatial reference system.

    Returns:
        int: Spatial Reference ID or None if not found.
    """
    code = None

    try:
        # Optionally use epsg-ident - calls to local database
        if sridentify_enabled:
            ident = Sridentify(prj=wellKnownText, call_remote_api=False)
            code = ident.get_epsg()

        if not code:
            # Attempt to lookup using web service
            payload = {'mode': 'wkt', 'terms': wellKnownText}
            r = requests.get('http://prj2epsg.org/search.json',
                             params=payload,
                             timeout=10)

            if r.status_code == 200:
                json = r.json()

                for code in json['codes']:
                    code = code['code']

        if not code:
            log.warning(
                "SRID Lookup Error: Spatial reference ID could not be identified."
            )

    except (requests.exceptions.ConnectionError, requests.exceptions.Timeout,
            RuntimeError):
        log.warning(
            "SRID Lookup Error: Could not automatically determine spatial "
            "reference ID, because there is no internet connection. "
            "Please check connection and try again.")
    return code
Ejemplo n.º 9
0
class SridentifyAPIModeTest(unittest.TestCase):

    def setUp(self):
        self.this_dir = os.path.abspath(os.path.dirname(__file__))
        self.fixtures_dir = os.path.join(self.this_dir, 'fixtures')
        self.prj_file = os.path.join(self.fixtures_dir, 'Central_Business_District.prj')
        self.ident = Sridentify()

    def test_from_file_raises_if_prj_does_not_exist(self):
        with self.assertRaises(FileNotFoundError):
            self.ident.from_file('foo.prj')
            self.ident.from_file('')

    def test_get_epsg_from_file(self):
        self.ident.from_file(self.prj_file)
        self.assertEqual(self.ident.get_epsg(), 3435)

    def test_get_epsg_from_text_string(self):
        self.ident.prj = """PROJCS["NAD_1983_StatePlane_Illinois_East_FIPS_1201_Feet",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",984250.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-88.33333333333333],PARAMETER["Scale_Factor",0.999975],PARAMETER["Latitude_Of_Origin",36.66666666666666],UNIT["Foot_US",0.3048006096012192]]"""
        self.assertEqual(self.ident.get_epsg(), 3435)
Ejemplo n.º 10
0
class SridentifyAPIModeTest(unittest.TestCase):
    def setUp(self):
        self.this_dir = os.path.abspath(os.path.dirname(__file__))
        self.fixtures_dir = os.path.join(self.this_dir, 'fixtures')
        self.prj_file = os.path.join(self.fixtures_dir,
                                     'Central_Business_District.prj')
        self.ident = Sridentify()

    def test_from_file_raises_if_prj_does_not_exist(self):
        with self.assertRaises(FileNotFoundError):
            self.ident.from_file('foo.prj')
            self.ident.from_file('')

    def test_get_epsg_from_file(self):
        self.ident.from_file(self.prj_file)
        self.assertEqual(self.ident.get_epsg(), 3435)

    def test_get_epsg_from_text_string(self):
        self.ident.prj = """PROJCS["NAD_1983_StatePlane_Illinois_East_FIPS_1201_Feet",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",984250.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-88.33333333333333],PARAMETER["Scale_Factor",0.999975],PARAMETER["Latitude_Of_Origin",36.66666666666666],UNIT["Foot_US",0.3048006096012192]]"""
        self.assertEqual(self.ident.get_epsg(), 3435)
Ejemplo n.º 11
0
 def setUp(self):
     self.this_dir = os.path.abspath(os.path.dirname(__file__))
     self.fixtures_dir = os.path.join(self.this_dir, 'fixtures')
     self.prj_file = os.path.join(self.fixtures_dir, 'Central_Business_District.prj')
     self.ident = Sridentify()
Ejemplo n.º 12
0
def do_shp_dir(dir_input=''):
    program_shp2pgsql = 'shp2pgsql'
    #do log file
    dir_out = get_output_directory()
    file_csv = str(os.path.join(dir_out, cfg.file_log))

    for handler in logging.root.handlers[:]:  #Remove all handlers associated with the root logger object.
        logging.root.removeHandler(handler)
    logging.basicConfig(filename=file_csv,
                        format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.DEBUG,
                        filemode='w')  #
    logging.info(file_csv)

    sql1 = 'DROP SCHEMA ' + cfg.schema + ' CASCADE;'
    psql_run(cfg.host, cfg.database_gis, cfg.user, cfg.user_password, sql1)
    logging.info(sql1)
    sql2 = 'CREATE SCHEMA ' + cfg.schema + ';'
    psql_run(cfg.host, cfg.database_gis, cfg.user, cfg.user_password, sql2)
    logging.info(sql2)

    #do main part
    try:
        for root, subdirs, files in os.walk(dir_input):
            for file in os.listdir(root):
                _srid = ''
                _codepage = ''
                file_path = str(os.path.join(root, file)).lower()
                ext = '.'.join(file.split('.')[1:]).lower()
                table_name = file.split('.')[0]
                if file_path.endswith('shp'):  # ext == "shp":
                    file_name = file_path.split('.')[0]
                    file_prj = file_name + '.prj'
                    file_cp = file_name + '.cpg'
                    if os.path.isfile(file_prj) and os.path.isfile(file_cp):
                        logging.info(file_path)

                        if os.path.isfile(file_cp):
                            _codepage = get_codepage_from_file(file_cp)
                            str_err = str(file_cp + ' has codepage ' +
                                          _codepage)
                            str_err = str_err.encode("cp1251").decode('cp1251')
                            #encode("cp1251").decode('cp1251').decode('utf8')
                            logging.info(str_err)
                            #print(str_err)

                        if os.path.isfile(file_prj):
                            try:
                                ident = Sridentify(
                                    call_remote_api=False
                                )  # Sridentify() # if we need  remote call
                                ident.from_file(file_prj)
                                _srid = ident.get_epsg()
                            except Exception as e:
                                logging.error('Ошибка в файле: ' + file_prj)
                                logging.error("Exception occurred",
                                              exc_info=True)
                                logging.exception(e)

                        if str(_srid) != 'None':
                            srid_source = ' -s ' + str(_srid) + ':4326 '
                            file_sql = str(
                                os.path.join(dir_out, table_name + '.sql'))
                            cmd_line = program_shp2pgsql + ' -d -I -W '+ ' \"' + _codepage + '\" '\
                                    + srid_source \
                                    + ' ' + file_path \
                                    + ' \"' + cfg.schema + '\".\"' \
                                    + table_name + "\"" \
                                    + ' -h ' + cfg.host \
                                    + ' -u ' + cfg.user \
                                    + ' -P ' + cfg.user_password\
                                    + ' > ' + file_sql
                            #+ ' |psql -d ' + cfg.database_gis \
                            #+ ' -U ' + cfg.user
                            print(cmd_line)
                            p = subprocess.Popen(cmd_line,
                                                 shell=True,
                                                 stdout=subprocess.PIPE,
                                                 stderr=subprocess.STDOUT)
                            for line in p.stdout.readlines():
                                print(line)
                                logging.info(line)
                            retval = p.wait()

                            # cmd_line_psql = 'psql -d ' + cfg.database_gis + ' -U ' + cfg.user + ' -f ' + file_sql
                            # psql "dbname='gisdb' user='******' password='******' host='10.57.10.45'" - f / home / glory / 1 / out / газопровод_сводный_2015.sql
                            #file_sql_run(cfg.host, cfg.database_gis, cfg.user, cfg.user_password, file_sql)
                            cmd_line_psql = 'psql ' + '\" dbname=\'' + cfg.database_gis + '\'' + ' ' \
                                                                                                 ' user=\'' + cfg.user + '\'' + \
                                            ' password=\'' + cfg.user_password + '\'' + \
                                            ' host=\'' + cfg.host + '\'' + '\"' + \
                                            ' -f ' + file_sql
                            # if You want run from cmd 'psql .....etc...' line - uncomment please follow lines
                            # import subprocess
                            print(cmd_line_psql)
                            p = subprocess.Popen(cmd_line_psql,
                                                 shell=True,
                                                 stdout=subprocess.PIPE,
                                                 stderr=subprocess.STDOUT)
                            for line in p.stdout.readlines():
                                print(line)
                            retval = p.wait()

                    else:
                        logging.error("Filename " + file_prj + ' or ' +
                                      file_cp + ' does not exist.')

    except Exception as e:
        logging.error("Exception occurred", exc_info=True)
        logging.exception(e)
Ejemplo n.º 13
0
 def setUp(self):
     self.this_dir = os.path.abspath(os.path.dirname(__file__))
     self.fixtures_dir = os.path.join(self.this_dir, 'fixtures')
     self.prj_file = os.path.join(self.fixtures_dir,
                                  'Central_Business_District.prj')
     self.ident = Sridentify()
def do_shp_dir(dir_input=''):
    _yes = cfg.value_yes
    _no = cfg.value_no
    _error = cfg.value_error
    # file_csv = cfg.file_csv

    file_csv = str(
        os.path.join(get_output_directory(), cfg.file_csv)
    )  # str(strftime("%Y-%m-%d") + "_shp_info_in_folder_" + ".csv")
    # file_csv = cfg.file_csv     #str(strftime("%Y-%m-%d") + "_shp_info_in_folder_" + ".csv")

    if os.path.isfile(file_csv):
        os.remove(file_csv)

    csv_dict = {
        'FILENAME': '',
        'PRJ': '',
        'SRID': '',
        'METADATA': '',
        'CODEPAGE': '',
        'HAS_DEFIS': '',
        'DATA_CREATION': '',
        'DATA_MODIFY': '',
        'DATA_LASTACCESS': '',
        'DATA_SCRIPT_RUN': '',
        'PRJ_INFO': '',
        'COUNT_REC': '',
        'COUNT_FIELDS': '',
        'FIELDS': ''
    }  # 'CODEPAGE_DBF': '', # CODEPAGE_DBF -  work a long time

    with open(file_csv, 'w', newline='',
              encoding='utf-8') as csv_file:  # Just use 'w' mode in 3.x

        csv_file_open = csv.DictWriter(csv_file,
                                       csv_dict.keys(),
                                       delimiter=cfg.csv_delimiter)
        csv_file_open.writeheader()
        for root, subdirs, files in os.walk(dir_input):
            for file in os.listdir(root):
                file_path = str(os.path.join(root, file)).lower()
                ext = '.'.join(file.split('.')[1:]).lower()
                if file_path.endswith('shp'):  #ext == "shp":
                    for key in csv_dict:
                        csv_dict[key] = ''
                    csv_dict['DATA_SCRIPT_RUN'] = str(
                        time.strftime("%Y-%m-%d"))
                    csv_dict['FILENAME'] = file_path
                    file_name = file_path.split('.')[0]

                    csv_dict['DATA_CREATION'] = str(
                        datetime.fromtimestamp(
                            os.path.getctime(file_path)).strftime('%Y-%m-%d'))
                    csv_dict['DATA_MODIFY'] = str(
                        datetime.fromtimestamp(
                            os.path.getmtime(file_path)).strftime('%Y-%m-%d'))
                    csv_dict['DATA_LASTACCESS'] = str(
                        datetime.fromtimestamp(
                            os.path.getatime(file_path)).strftime('%Y-%m-%d'))

                    # Prj file exist
                    file_prj = file_name + '.prj'
                    if os.path.isfile(file_prj):
                        csv_dict['PRJ_INFO'] = file_get_first_line(file_prj)
                        csv_dict['PRJ'] = _yes
                        try:
                            ident = Sridentify(
                                call_remote_api=False
                            )  # Sridentify() # if we need  remote call
                            ident.from_file(file_prj)
                            srid = ident.get_epsg()
                            if len(str(srid)):
                                csv_dict['SRID'] = str(srid)
                            else:
                                csv_dict['SRID'] = _no
                        except:
                            csv_dict['SRID'] = _error
                            pass
                    else:
                        csv_dict['PRJ'] = _no
                        csv_dict['SRID'] = _no

                    # Metadata exist
                    file_prj = file_name + '.shp.xml'
                    if os.path.isfile(file_prj):
                        csv_dict['METADATA'] = _yes
                    else:
                        csv_dict['METADATA'] = _no

                    # Codepage exist
                    file_cp = file_name + '.cpg'
                    if os.path.isfile(file_cp):
                        csv_dict['CODEPAGE'] = str(
                            file_get_first_line(file_cp))
                    else:
                        csv_dict['CODEPAGE'] = _no

                    # Codepage DBF - work long time
                    # file_dbf = file_name + '.dbf'
                    # if os.path.isfile(file_dbf):
                    #
                    #     csv_dict['CODEPAGE_DBF'] = get_encoding(file_dbf)
                    # else:
                    #     csv_dict['CODEPAGE_DBF'] = _no

                    # Get records Count from DBF - work long time
                    file_dbf = file_name + '.dbf'
                    if os.path.isfile(file_dbf):
                        sf = shapefile.Reader(file_dbf)
                        fields = sf.fields
                        fields_count = str(len(fields) - 1)
                        ss = ''
                        for field in fields:
                            ss = ss + str(field[0])
                            ss = ss + ", "
                        ss = ss.strip('DeletionFlag')
                        if ss.endswith(', '):
                            ss = ss.lstrip(', ')
                            ss = ss.rstrip(', ')

                        records = sf.records()
                        _count = len(records)

                        csv_dict['COUNT_REC'] = str(_count)
                        csv_dict['COUNT_FIELDS'] = fields_count
                        csv_dict['FIELDS'] = str(ss)

                    else:
                        csv_dict['COUNT_REC'] = 0
                        csv_dict['COUNT_FIELDS'] = 0
                        csv_dict['FIELDS'] = 0

                    # defis symbol has found in file name
                    file_1 = str(file)
                    if file_1.find('-') != -1:
                        csv_dict['HAS_DEFIS'] = _yes
                    else:
                        csv_dict['HAS_DEFIS'] = _no

                    # if len(str_log):
                    csv_file_open.writerow(csv_dict)
                    # print(str(csv_dict.values()))
        csv_file.close()
Ejemplo n.º 15
0
def do_shp_dir(dir_input=''):
    program_shp2pgsql = 'ogr2ogr'
    #do log file
    dir_out = get_output_directory()
    file_csv = str(os.path.join(dir_out, cfg.file_log))

    for handler in logging.root.handlers[:]:  #Remove all handlers associated with the root logger object.
        logging.root.removeHandler(handler)
    logging.basicConfig(filename=file_csv,
                        format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.DEBUG,
                        filemode='w')  #
    logging.info(file_csv)

    #do main part
    try:
        for root, subdirs, files in os.walk(dir_input):
            for file in os.listdir(root):
                _srid = ''
                _codepage = ''
                file_path = str(os.path.join(root, file)).lower()
                ext = '.'.join(file.split('.')[1:]).lower()
                table_name = file.split('.')[0]
                if file_path.endswith('shp'):  # ext == "shp":
                    file_name = file_path.split('.')[0]
                    file_prj = file_name + '.prj'
                    file_cp = file_name + '.cpg'
                    codepage = ''
                    if os.path.isfile(file_prj) and os.path.isfile(file_cp):
                        logging.info(file_path)

                        if os.path.isfile(file_cp):
                            codepage = get_codepage_from_file(file_cp)
                            str_err = str(file_cp + ' has codepage ' +
                                          codepage)
                            str_err = str_err.encode("cp1251").decode('cp1251')
                            logging.info(str_err)
                        if os.path.isfile(file_prj):
                            try:
                                ident = Sridentify(
                                    call_remote_api=False
                                )  # Sridentify() # if we need  remote call
                                ident.from_file(file_prj)
                                _srid = ident.get_epsg()
                            except Exception as e:
                                logging.error('Ошибка в файле: ' + file_prj)
                                logging.error("Exception occurred",
                                              exc_info=True)
                                logging.exception(e)

                        if str(_srid) != 'None':
                            srid_source = ' -s ' + str(_srid) + ':4326 '
                            file_kml = str(
                                os.path.join(dir_out, table_name + '.kml'))
                            cmd_line = program_shp2pgsql + ' -skipfailures  -overwrite  -lco  ENCODING=UTF-8  ' + \
                                ' -a_srs \"EPSG:4326\"' + \
                                ' --config SHAPE_ENCODING ' +       codepage + ' ' +\
                                ' -f ' + '\"KML\"' + \
                                ' ' + file_kml + \
                                ' ' + file_path

                            print(cmd_line)
                            # p = subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                            # for line in p.stdout.readlines():
                            #     print(line)
                            #     logging.info(line)
                            # retval = p.wait()

                    else:
                        logging.error("Filename " + file_prj + ' or ' +
                                      file_cp + ' does not exist.')

    except Exception as e:
        logging.error("Exception occurred", exc_info=True)
        logging.exception(e)
Ejemplo n.º 16
0
from sridentify import Sridentify
ident = Sridentify()
# from file
ident.from_file('/Users/olgabuchel/Downloads/ba_comunas/ba_comunas.prj')
print(ident.get_epsg())
'''
# from WKT
ident = Sridentify(prj="""GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]])
ident.get_epsg()
4326/Users/olgabuchel/Downloads/ba_comunas/ba_comunas.prj 

'''