<commit_before>.. _export:

Export Options
==============
In order to provide the user the opportunity to export their shapes into formats 
that are viewable (among other things) outside of MarineMap, 
MarineMap provides an Export option allowing the user to export individual 
shapes as well as arrays in a variety of formats.

Exporting as kmz and exporting as image are both built in to Lingcod and will 
appear by default in the Export drop-down menu.  

Exporting as shapefile requires the enabling of a urlpattern as explained below.

Atom links for each of these Export options can be found in ``lingcod/kmlapp/templates/placemarks.kml``.

as kmz (Google Earth)
---------------------

as image (PNG)
--------------

as shapefile
------------
The `Export as shapefile` option can be enabled by adding a urlpattern named ``mpa_shapefile``.

.. code-block:: python

    urlpatterns = patterns('',
        url(r'shapefile/mpa/(?P<mpa_id_list_str>(\d+,?)+)/$', mpa_shapefile, name='mpa_shapefile'),
    )

This urlpattern refers to a view, which we have called ``mpa_shapefile``.  This view will utilize the built-in ``ShpResponder`` 
class to generate a shapefile from the ``export_query_set`` property of your model (we are using ``MPA`` as 
the model in this example).  

.. code-block:: python

    from lingcod.common import utils
    from lingcod.sharing.utils import get_viewable_object_or_respond
    from lingcod.shapes.views import ShpResponder
    from django.template.defaultfilters import slugify

    def mpa_shapefile(request, mpa_id_list_str):
        mpa_class = utils.get_mpa_class()
        mpa_id_list = mpa_id_list_str.split(',')
        mpa_id = mpa_id_list[0] # only expecting one 
        mpa = get_viewable_object_or_respond(mpa_class, mpa_id, request.user)
        shp_response = ShpResponder(mpa.export_query_set)
        shp_response.file_name = slugify(mpa.name[0:8])
        return shp_response()

In order for this view to function, your model (``MPA`` in this case) needs to have an ``export_query_set`` property defined.

.. code-block:: python

    @property
    def export_query_set(self):
        return MpaShapefile.objects.filter(pk=self.export_version.pk)
    
This ``export_query_set`` property requires an additional model (which we will call ``MpaShapefile``),
and an additional property in your original model, ``export_version``, which will be responsible for
generating an instance of this new model ``MpaShapefile``.
This additional model, ``MpaShapefile``, will provide the fields you wish to have present in the attribute table of 
the exported shapefile.  

.. code-block:: python

    class MpaShapefile(models.Model):
        """
        This model will provide the correct fields for the export of shapefiles.
        """
        name = models.CharField(max_length=255)
        mpa_id_num = models.IntegerField(blank=True, null=True)
        geometry = models.PolygonField(srid=settings.GEOMETRY_DB_SRID,blank=True,null=True)
        mpa = models.OneToOneField(MPA, related_name="mpa")<commit_msg>a couple minor changes to the export.rst text<commit_after>.. _export:

Export Options
==============
In order to provide the user the opportunity to make use of their shapes outside of the MarineMap environment, 
MarineMap provides an `Export` option which allows the user to export individual 
shapes and arrays of shapes in a variety of formats.

Exporting as kmz and exporting as image are both built in to Lingcod and will 
appear by default in the `Export` drop-down menu.  

Exporting as shapefile requires the enabling of a urlpattern as explained below.

Atom links for each of these Export options can be found in ``lingcod/kmlapp/templates/placemarks.kml``.

as kmz (Google Earth)
---------------------

as image (PNG)
--------------

as shapefile
------------
The `Export as shapefile` option can be enabled by adding a urlpattern named ``mpa_shapefile``.

.. code-block:: python

    urlpatterns = patterns('',
        url(r'shapefile/mpa/(?P<mpa_id_list_str>(\d+,?)+)/$', mpa_shapefile, name='mpa_shapefile'),
    )

This urlpattern refers to a view, which, in this example, is called ``mpa_shapefile``.  
This view will utilize the built-in ``ShpResponder`` class to generate a shapefile from the 
``export_query_set`` property of your model (``MPA`` will serve as the model in this example).  

.. code-block:: python

    from lingcod.common import utils
    from lingcod.sharing.utils import get_viewable_object_or_respond
    from lingcod.shapes.views import ShpResponder
    from django.template.defaultfilters import slugify

    def mpa_shapefile(request, mpa_id_list_str):
        mpa_class = utils.get_mpa_class()
        mpa_id_list = mpa_id_list_str.split(',')
        mpa_id = mpa_id_list[0] # only expecting one 
        mpa = get_viewable_object_or_respond(mpa_class, mpa_id, request.user)
        shp_response = ShpResponder(mpa.export_query_set)
        shp_response.file_name = slugify(mpa.name[0:8])
        return shp_response()

In order for this view to function, your model (``MPA`` in this case) should have an ``export_query_set`` property defined.

.. code-block:: python

    @property
    def export_query_set(self):
        return MpaShapefile.objects.filter(pk=self.export_version.pk)
    
This ``export_query_set`` property requires an additional model (which we will call ``MpaShapefile``),
and an additional property in your original model, ``export_version``, which will be responsible for
generating an instance of this new model ``MpaShapefile``.
This additional model, ``MpaShapefile``, will provide the fields you wish to have present in the attribute table of 
the exported shapefile.  

.. code-block:: python

    class MpaShapefile(models.Model):
        """
        This model will provide the correct fields for the export of shapefiles.
        """
        name = models.CharField(max_length=255)
        mpa_id_num = models.IntegerField(blank=True, null=True)
        geometry = models.PolygonField(srid=settings.GEOMETRY_DB_SRID,blank=True,null=True)
        mpa = models.OneToOneField(MPA, related_name="mpa")
{'fraction_non_alphanumeric': 0.10111074812152891, 'fraction_numerical': 0.0019601437438745506, 'mean_word_length': 3.7136258660508084, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3897570', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1806, 'n_words': 631}