/scripts/import_maps.py

https://github.com/cholmes/mapstory · Python · 143 lines · 125 code · 17 blank · 1 comment · 22 complexity · 7c2ebe814c8966d7a386364c16163517 MD5 · raw file

  1. # export list of maps/layers/maplayer objects that can be used for import script
  2. from django.core import serializers
  3. from django.conf import settings
  4. from django.contrib.auth.models import User
  5. from optparse import OptionParser
  6. import json
  7. import psycopg2
  8. import sys
  9. import os
  10. import tempfile
  11. from geonode.maps.models import Map
  12. from geonode.maps.models import MapLayer
  13. from import_layer import import_layer
  14. from update_thumb_specs import make_thumbnail_updater
  15. from update_thumb_specs import make_maplayer_updater
  16. def import_maps(gs_data_dir, conn, zipfile,
  17. no_password=False, chown_to=None, do_django_layer_save=True,
  18. from_string=None, to_string=None):
  19. tempdir = tempfile.mkdtemp()
  20. temppath = lambda *p: os.path.join(tempdir, *p)
  21. os.system('unzip %s -d %s' % (zipfile, tempdir))
  22. for layer_name in os.listdir(temppath('layers')):
  23. import_layer(gs_data_dir, conn,
  24. temppath('layers', layer_name), layer_name,
  25. no_password, chown_to, do_django_layer_save)
  26. conn.commit()
  27. print 'layers import complete'
  28. def import_models(path, add_owner=False):
  29. with open(path, 'r') as f:
  30. models = serializers.deserialize('json', f)
  31. for model in models:
  32. if add_owner:
  33. owner = User.objects.filter(pk=model.object.owner_id)
  34. if not owner:
  35. model.object.owner = User.objects.get(pk=1)
  36. model.save()
  37. return models
  38. print 'importing maps'
  39. import_models(temppath('maps.json'), add_owner=True)
  40. print 'importing map layers'
  41. maplayer_models = import_models(temppath('maplayers.json'))
  42. print 'importing users'
  43. map_comment_models = import_models(temppath('comment_users.json'))
  44. print 'importing map comments'
  45. map_comment_models = import_models(temppath('map_comments.json'))
  46. print 'importing map publishing status'
  47. map_comment_models = import_models(temppath('map_publishing_status.json'))
  48. print 'importing map thumb specs'
  49. with open(temppath('map_thumb_specs.json')) as f:
  50. map_thumbs = json.load(f)
  51. thumb_updater = (make_thumbnail_updater(from_string, to_string)
  52. if (from_string is not None and to_string is not None)
  53. else lambda m:m.save())
  54. for mapid, thumb_spec in map_thumbs:
  55. try:
  56. m = Map.objects.get(pk=mapid)
  57. except Map.DoesNotExist:
  58. print 'No map "%s" for importing thumb spec' % mapid
  59. else:
  60. m.set_thumbnail(thumb_spec)
  61. t = m.get_thumbnail()
  62. thumb_updater(t)
  63. if from_string is not None and to_string is not None:
  64. print 'adjusting maplayer params'
  65. updater = make_maplayer_updater(from_string, to_string)
  66. for maplayer_model in maplayer_models:
  67. try:
  68. maplayer = MapLayer.objects.get(pk=maplayer_model.object.id)
  69. except MapLayer.DoesNotExist:
  70. print 'No maplayer "%s" for updating layer params' % maplayer.object.id
  71. else:
  72. updater(maplayer)
  73. if __name__ == '__main__':
  74. gs_data_dir = '/var/lib/geoserver/geonode-data/'
  75. parser = OptionParser('usage: %s [options] maps_import_file.zip' % sys.argv[0])
  76. parser.add_option('-d', '--data-dir',
  77. dest='data_dir',
  78. default=gs_data_dir,
  79. help='geoserver data dir')
  80. parser.add_option('-P', '--no-password',
  81. dest='no_password', action='store_true',
  82. help='Add the --no-password option to the pg_restore'
  83. 'command. This assumes the user has a ~/.pgpass file'
  84. 'with the credentials. See the pg_restore man page'
  85. 'for details.',
  86. default=False,
  87. )
  88. parser.add_option('-c', '--chown-to',
  89. dest='chown_to',
  90. help='If set, chown the files copied into the'
  91. 'geoserver data directory to a particular'
  92. 'user. Assumes the user running is root or has'
  93. 'permission to do so. This is useful to chown the'
  94. 'files to something like tomcat6 afterwards.',
  95. )
  96. parser.add_option('-L', '--skip-django-layer-save',
  97. dest='do_django_layer_save',
  98. default=True,
  99. action='store_false',
  100. help='Whether to skip loading the django layer models'
  101. )
  102. parser.add_option('-f', '--from-string',
  103. dest='from_string',
  104. help='Used as the source string to use when replacing the thumb_spec and maplayer params',
  105. )
  106. parser.add_option('-t', '--to-string',
  107. dest='to_string',
  108. help='Used as the replacement string to use when replacing the thumb_spec and maplayer params',
  109. )
  110. (options, args) = parser.parse_args()
  111. if len(args) != 1:
  112. parser.error('please provide the maps import zip file')
  113. if not os.path.exists(options.data_dir):
  114. parser.error("geoserver data directory %s not found" % options.data_dir)
  115. conn = psycopg2.connect("dbname='" + settings.DB_DATASTORE_DATABASE +
  116. "' user='" + settings.DB_DATASTORE_USER +
  117. "' password='" + settings.DB_DATASTORE_PASSWORD +
  118. "' port=" + settings.DB_DATASTORE_PORT +
  119. " host='" + settings.DB_DATASTORE_HOST + "'")
  120. zipfile = args[0]
  121. import_maps(options.data_dir, conn, zipfile,
  122. options.no_password, options.chown_to,
  123. options.do_django_layer_save,
  124. options.from_string, options.to_string)
  125. conn.commit()
  126. conn.close()