Skip to content

Commit

Permalink
Fix #925 -- Remove project slug from file names
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick authored and amplifi committed Dec 21, 2016
1 parent 24d6126 commit c5c0f16
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
23 changes: 11 additions & 12 deletions cadasta/organization/download/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ def write_features(self, layers, filename):
values = self.get_values(su, model_attrs, schema_attrs)
csvwriter.writerow(values)

def create_datasource(self, dst_dir, f_name):
def create_datasource(self, dst_dir):
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
path = os.path.join(dst_dir, f_name + '-point.shp')
path = os.path.join(dst_dir, 'point.shp')
driver = ogr.GetDriverByName('ESRI Shapefile')
return driver.CreateDataSource(path)

def create_shp_layers(self, datasource, f_name):
def create_shp_layers(self, datasource):
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)

layers = (
datasource.CreateLayer(f_name + '-point', srs, geom_type=1),
datasource.CreateLayer(f_name + '-line', srs, geom_type=2),
datasource.CreateLayer(f_name + '-polygon', srs, geom_type=3)
datasource.CreateLayer('point', srs, geom_type=1),
datasource.CreateLayer('line', srs, geom_type=2),
datasource.CreateLayer('polygon', srs, geom_type=3)
)

for layer in layers:
Expand All @@ -92,8 +92,8 @@ def create_shp_layers(self, datasource, f_name):
def make_download(self, f_name):
dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/{}'.format(f_name))

ds = self.create_datasource(dst_dir, self.project.slug)
layers = self.create_shp_layers(ds, self.project.slug)
ds = self.create_datasource(dst_dir)
layers = self.create_shp_layers(ds)

self.write_features(layers, os.path.join(dst_dir, 'locations.csv'))
self.write_relationships(os.path.join(dst_dir, 'relationships.csv'))
Expand All @@ -104,12 +104,11 @@ def make_download(self, f_name):
path = os.path.join(settings.MEDIA_ROOT, 'temp/{}.zip'.format(f_name))
readme = render_to_string(
'organization/download/shp_readme.txt',
{'project_name': self.project.name,
'project_slug': self.project.slug}
{'project_name': self.project.name}
)
with ZipFile(path, 'a') as myzip:
myzip.writestr('README.txt', readme)
for file in os.listdir(dst_dir):
myzip.write(os.path.join(dst_dir, file), arcname=file)
for f in os.listdir(dst_dir):
myzip.write(os.path.join(dst_dir, f), arcname=f)

return path, MIME_TYPE
42 changes: 21 additions & 21 deletions cadasta/organization/tests/test_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def test_create_datasource(self):
exporter = ShapeExporter(project)

dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file')
ds = exporter.create_datasource(dst_dir, 'file0')
ds = exporter.create_datasource(dst_dir)
assert (ds.GetName() ==
os.path.join(settings.MEDIA_ROOT, 'temp/file/file0-point.shp'))
os.path.join(settings.MEDIA_ROOT, 'temp/file/point.shp'))
ds.Destroy()

def test_create_shp_layers(self):
Expand All @@ -140,12 +140,12 @@ def test_create_shp_layers(self):
exporter = ShapeExporter(project)

dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file6')
ds = exporter.create_datasource(dst_dir, 'file6')
layers = exporter.create_shp_layers(ds, 'file6')
ds = exporter.create_datasource(dst_dir)
layers = exporter.create_shp_layers(ds)
assert len(layers) == 3
assert layers[0].GetName() == 'file6-point'
assert layers[1].GetName() == 'file6-line'
assert layers[2].GetName() == 'file6-polygon'
assert layers[0].GetName() == 'point'
assert layers[1].GetName() == 'line'
assert layers[2].GetName() == 'polygon'
ds.Destroy()

def test_write_items(self):
Expand Down Expand Up @@ -228,8 +228,8 @@ def test_write_features(self):
attributes={'key': 'value 2'})

dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4')
ds = exporter.create_datasource(dst_dir, 'file4')
layers = exporter.create_shp_layers(ds, 'file4')
ds = exporter.create_datasource(dst_dir)
layers = exporter.create_shp_layers(ds)

csvfile = os.path.join(dst_dir, 'locations.csv')
exporter.write_features(layers, csvfile)
Expand Down Expand Up @@ -322,18 +322,18 @@ def test_make_download(self):

with ZipFile(path, 'r') as testzip:
assert len(testzip.namelist()) == 16
assert project.slug + '-point.dbf' in testzip.namelist()
assert project.slug + '-point.prj' in testzip.namelist()
assert project.slug + '-point.shp' in testzip.namelist()
assert project.slug + '-point.shx' in testzip.namelist()
assert project.slug + '-line.dbf' in testzip.namelist()
assert project.slug + '-line.prj' in testzip.namelist()
assert project.slug + '-line.shp' in testzip.namelist()
assert project.slug + '-line.shx' in testzip.namelist()
assert project.slug + '-polygon.dbf' in testzip.namelist()
assert project.slug + '-polygon.prj' in testzip.namelist()
assert project.slug + '-polygon.shp' in testzip.namelist()
assert project.slug + '-polygon.shx' in testzip.namelist()
assert 'point.dbf' in testzip.namelist()
assert 'point.prj' in testzip.namelist()
assert 'point.shp' in testzip.namelist()
assert 'point.shx' in testzip.namelist()
assert 'line.dbf' in testzip.namelist()
assert 'line.prj' in testzip.namelist()
assert 'line.shp' in testzip.namelist()
assert 'line.shx' in testzip.namelist()
assert 'polygon.dbf' in testzip.namelist()
assert 'polygon.prj' in testzip.namelist()
assert 'polygon.shp' in testzip.namelist()
assert 'polygon.shx' in testzip.namelist()
assert 'relationships.csv' in testzip.namelist()
assert 'parties.csv' in testzip.namelist()
assert 'locations.csv' in testzip.namelist()
Expand Down
24 changes: 12 additions & 12 deletions cadasta/organization/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,18 +1110,18 @@ def test_get_shape_download(self):

with ZipFile(path, 'r') as testzip:
assert len(testzip.namelist()) == 16
assert project.slug + '-point.dbf' in testzip.namelist()
assert project.slug + '-point.prj' in testzip.namelist()
assert project.slug + '-point.shp' in testzip.namelist()
assert project.slug + '-point.shx' in testzip.namelist()
assert project.slug + '-line.dbf' in testzip.namelist()
assert project.slug + '-line.prj' in testzip.namelist()
assert project.slug + '-line.shp' in testzip.namelist()
assert project.slug + '-line.shx' in testzip.namelist()
assert project.slug + '-polygon.dbf' in testzip.namelist()
assert project.slug + '-polygon.prj' in testzip.namelist()
assert project.slug + '-polygon.shp' in testzip.namelist()
assert project.slug + '-polygon.shx' in testzip.namelist()
assert 'point.dbf' in testzip.namelist()
assert 'point.prj' in testzip.namelist()
assert 'point.shp' in testzip.namelist()
assert 'point.shx' in testzip.namelist()
assert 'line.dbf' in testzip.namelist()
assert 'line.prj' in testzip.namelist()
assert 'line.shp' in testzip.namelist()
assert 'line.shx' in testzip.namelist()
assert 'polygon.dbf' in testzip.namelist()
assert 'polygon.prj' in testzip.namelist()
assert 'polygon.shp' in testzip.namelist()
assert 'polygon.shx' in testzip.namelist()
assert 'relationships.csv' in testzip.namelist()
assert 'parties.csv' in testzip.namelist()
assert 'locations.csv' in testzip.namelist()
Expand Down
2 changes: 1 addition & 1 deletion cadasta/templates/organization/download/shp_readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ You have downloaded all data from project "{{ project_name }}" in shapefile form

Besides this README, the ZIP archive contains the shape files and CSV files containing the project data.

Shape files can only store geometries of a single type: either point, line or polygon. Because of this, you will find three shape files ({{ project_slug }}-point.shp, {{ project_slug }}-line.shp, and {{ project_slug }}-polygon.shp) containing the geometries of all locations in project "{{ project_name }}", along with the corresponding *.shx, *.prj and *.dbf files. The attribute table of each shapefile contains only the location ID.
Shape files can only store geometries of a single type: either point, line or polygon. Because of this, you will find three shape files (point.shp, line.shp, and polygon.shp) containing the geometries of all locations in project "{{ project_name }}", along with the corresponding *.shx, *.prj and *.dbf files. The attribute table of each shapefile contains only the location ID.

The attributes for locations, parties and relationships are provided in CSV files called locations.csv, parties.csv and relationships.csv. We use CSV instead of dBase files to avoid certain restrictions of the dBase format that would cause some data to be truncated.
{% endblocktrans %}

0 comments on commit c5c0f16

Please sign in to comment.