Sha256: 3254a8bd98a0f3d20d1e835e743d1ee04d5d6b29b862ebe44cfff6c4c1092168

Contents?: true

Size: 1.44 KB

Versions: 13

Compression:

Stored size: 1.44 KB

Contents

from chardet.universaldetector import UniversalDetector
import os.path
import sys
import dbfUtils
import sys
from osgeo import osr
from urllib import urlencode
from urllib2 import urlopen
import json
import subprocess

shp_file = sys.argv[1]
name = sys.argv[2]

dbf_file = shp_file[0:-4] + '.dbf'
prj_file = shp_file[0:-4] + '.prj'

srid = 4326
#Try detecting the SRID
if os.path.isfile(prj_file):
  prj_filef = open(prj_file, 'r')
  prj_txt = prj_filef.read()
  prj_filef.close()
  srs = osr.SpatialReference()
  srs.ImportFromESRI([prj_txt])
  srs.AutoIdentifyEPSG()
  code = srs.GetAuthorityCode(None)
  if code:
    srid = code
  else:
    #Ok, no luck, lets try with the OpenGeo service
    query = urlencode({
        'exact' : True,
        'error' : True,
        'mode' : 'wkt',
        'terms' : prj_txt})
    webres = urlopen('http://prj2epsg.org/search.json', query)
    jres = json.loads(webres.read())
    if jres['codes']:
      srid = int(jres['codes'][0]['code'])
    
try:
    #Try to detect the encoding
    dbf = open(dbf_file, 'rb')
    db = dbfUtils.dbfreader(dbf)
    detector = UniversalDetector()
    for row in db:
      detector.feed(str(row))
      if detector.done: break
    detector.close()
    dbf.close()
    encoding = detector.result["encoding"]
    if encoding=="ascii":
        encoding="LATIN1"
except:
    #if encoding detection fails, attempt default UTF8
    encoding = "UTF8"

print "%s,%s,%s,%s" % (srid,encoding,shp_file,name)

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
cartodb-importer-0.2.19 misc/shp_normalizer.py
cartodb-importer-0.2.18 misc/shp_normalizer.py
cartodb-importer-0.2.17 misc/shp_normalizer.py
cartodb-importer-0.2.16 misc/shp_normalizer.py
cartodb-importer-0.2.15 misc/shp_normalizer.py
cartodb-importer-0.2.14 misc/shp_normalizer.py
cartodb-importer-0.2.13 misc/shp_normalizer.py
cartodb-importer-0.2.12 misc/shp_normalizer.py
cartodb-importer-0.2.11 misc/shp_normalizer.py
cartodb-importer-0.2.10 misc/shp_normalizer.py
cartodb-importer-0.2.9 misc/shp_normalizer.py
cartodb-importer-0.2.8 misc/shp_normalizer.py
cartodb-importer-0.2.7 misc/shp_normalizer.py