We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Failed to create geographic feature resource
The files are from this resource https://www.hydroshare.org/resource/822fe5a7a83a49668c77a85179c3a7ee/. 

The error is shown as below. Thanks for looking into this issue 
![v1 ao3 o6x3ete23yl1nw](https://cloud.githubusercontent.com/assets/5016511/20231562/19a9d520-a820-11e6-9373-81cf529504b8.png)


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `hs_geographic_feature_resource/parse_lib.py`
Content:
```
1 import os
2 import xmltodict
3 import re
4 from osgeo import ogr, osr
5 try:
6     #  Python 2.6-2.7
7     from HTMLParser import HTMLParser
8 except ImportError:
9     #  Python 3
10     from html.parser import HTMLParser
11 
12 UNKNOWN_STR = "unknown"
13 
14 
15 def parse_shp(file_path):
16     # output dictionary format
17     # shp_metadata_dict["origin_projection_string"]: original projection string
18     # shp_metadata_dict["origin_projection_name"]: origin_projection_name
19     # shp_metadata_dict["origin_datum"]: origin_datum
20     # shp_metadata_dict["origin_unit"]: origin_unit
21     # shp_metadata_dict["field_meta_dict"]["field_list"]: list [fieldname1, fieldname2...]
22     # shp_metadata_dict["field_meta_dict"]["field_attr_dic"]:
23     #   dict {"fieldname": dict {
24     #                         "fieldName":fieldName,
25     #                         "fieldTypeCode":fieldTypeCode,
26     #                         "fieldType":fieldType,
27     #                         "fieldWidth:fieldWidth,
28     #                         "fieldPrecision:fieldPrecision"
29     #                          }
30     #         }
31     # shp_metadata_dict["feature_count"]: feature count
32     # shp_metadata_dict["geometry_type"]: geometry_type
33     # shp_metadata_dict["origin_extent_dict"]:
34     # dict{"west": east, "north":north, "east":east, "south":south}
35     # shp_metadata_dict["wgs84_extent_dict"]:
36     # dict{"west": east, "north":north, "east":east, "south":south}
37 
38     shp_metadata_dict = {}
39     # read shapefile
40     driver = ogr.GetDriverByName('ESRI Shapefile')
41     dataset = driver.Open(file_path)
42 
43     # get layer
44     layer = dataset.GetLayer()
45     # get spatialRef from layer
46     spatialRef_from_layer = layer.GetSpatialRef()
47 
48     if spatialRef_from_layer is not None:
49         shp_metadata_dict["origin_projection_string"] = str(spatialRef_from_layer)
50         prj_name = spatialRef_from_layer.GetAttrValue('projcs')
51         if prj_name is None:
52             prj_name = spatialRef_from_layer.GetAttrValue('geogcs')
53         shp_metadata_dict["origin_projection_name"] = prj_name
54 
55         shp_metadata_dict["origin_datum"] = spatialRef_from_layer.GetAttrValue('datum')
56         shp_metadata_dict["origin_unit"] = spatialRef_from_layer.GetAttrValue('unit')
57     else:
58         shp_metadata_dict["origin_projection_string"] = UNKNOWN_STR
59         shp_metadata_dict["origin_projection_name"] = UNKNOWN_STR
60         shp_metadata_dict["origin_datum"] = UNKNOWN_STR
61         shp_metadata_dict["origin_unit"] = UNKNOWN_STR
62 
63     field_list = []
64     filed_attr_dic = {}
65     field_meta_dict = {"field_list": field_list, "field_attr_dict": filed_attr_dic}
66     shp_metadata_dict["field_meta_dict"] = field_meta_dict
67     # get Attributes
68     layerDefinition = layer.GetLayerDefn()
69     for i in range(layerDefinition.GetFieldCount()):
70         fieldName = layerDefinition.GetFieldDefn(i).GetName()
71         field_list.append(fieldName)
72         attr_dict = {}
73         field_meta_dict["field_attr_dict"][fieldName] = attr_dict
74 
75         attr_dict["fieldName"] = fieldName
76         fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType()
77         attr_dict["fieldTypeCode"] = fieldTypeCode
78         fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode)
79         attr_dict["fieldType"] = fieldType
80         fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth()
81         attr_dict["fieldWidth"] = fieldWidth
82         fieldPrecision = layerDefinition.GetFieldDefn(i).GetPrecision()
83         attr_dict["fieldPrecision"] = fieldPrecision
84 
85     # get layer extent
86     layer_extent = layer.GetExtent()
87 
88     # get feature count
89     featureCount = layer.GetFeatureCount()
90     shp_metadata_dict["feature_count"] = featureCount
91 
92     # get a feature from layer
93     feature = layer.GetNextFeature()
94 
95     # get geometry from feature
96     geom = feature.GetGeometryRef()
97 
98     # get geometry name
99     shp_metadata_dict["geometry_type"] = geom.GetGeometryName()
100 
101     # reproject layer extent
102     # source SpatialReference
103     source = spatialRef_from_layer
104     # target SpatialReference
105     target = osr.SpatialReference()
106     target.ImportFromEPSG(4326)
107 
108     # create two key points from layer extent
109     left_upper_point = ogr.Geometry(ogr.wkbPoint)
110     left_upper_point.AddPoint(layer_extent[0], layer_extent[3])  # left-upper
111     right_lower_point = ogr.Geometry(ogr.wkbPoint)
112     right_lower_point.AddPoint(layer_extent[1], layer_extent[2])  # right-lower
113 
114     # source map always has extent, even projection is unknown
115     shp_metadata_dict["origin_extent_dict"] = {}
116     shp_metadata_dict["origin_extent_dict"]["westlimit"] = layer_extent[0]
117     shp_metadata_dict["origin_extent_dict"]["northlimit"] = layer_extent[3]
118     shp_metadata_dict["origin_extent_dict"]["eastlimit"] = layer_extent[1]
119     shp_metadata_dict["origin_extent_dict"]["southlimit"] = layer_extent[2]
120 
121     # reproject to WGS84
122     shp_metadata_dict["wgs84_extent_dict"] = {}
123 
124     if source is not None:
125         # define CoordinateTransformation obj
126         transform = osr.CoordinateTransformation(source, target)
127         # project two key points
128         left_upper_point.Transform(transform)
129         right_lower_point.Transform(transform)
130         shp_metadata_dict["wgs84_extent_dict"]["westlimit"] = left_upper_point.GetX()
131         shp_metadata_dict["wgs84_extent_dict"]["northlimit"] = left_upper_point.GetY()
132         shp_metadata_dict["wgs84_extent_dict"]["eastlimit"] = right_lower_point.GetX()
133         shp_metadata_dict["wgs84_extent_dict"]["southlimit"] = right_lower_point.GetY()
134         shp_metadata_dict["wgs84_extent_dict"]["projection"] = "WGS 84 EPSG:4326"
135         shp_metadata_dict["wgs84_extent_dict"]["units"] = "Decimal degrees"
136     else:
137         shp_metadata_dict["wgs84_extent_dict"]["westlimit"] = UNKNOWN_STR
138         shp_metadata_dict["wgs84_extent_dict"]["northlimit"] = UNKNOWN_STR
139         shp_metadata_dict["wgs84_extent_dict"]["eastlimit"] = UNKNOWN_STR
140         shp_metadata_dict["wgs84_extent_dict"]["southlimit"] = UNKNOWN_STR
141         shp_metadata_dict["wgs84_extent_dict"]["projection"] = UNKNOWN_STR
142         shp_metadata_dict["wgs84_extent_dict"]["units"] = UNKNOWN_STR
143 
144     return shp_metadata_dict
145 
146 
147 def parse_shp_xml(shp_xml_full_path):
148     """
149     Parse ArcGIS 10.X ESRI Shapefile Metadata XML.
150     :param shp_xml_full_path: Expected fullpath to the .shp.xml file
151     :return: a list of metadata dict
152     """
153     metadata = []
154 
155     try:
156         if os.path.isfile(shp_xml_full_path):
157             with open(shp_xml_full_path) as fd:
158                 xml_dict = xmltodict.parse(fd.read())
159                 if 'metadata' in xml_dict:
160                     if 'dataIdInfo' in xml_dict['metadata']:
161                         dataIdInfo_dict = xml_dict['metadata']['dataIdInfo']
162                         if 'idAbs' in dataIdInfo_dict:
163                             description_value = clean_text(dataIdInfo_dict['idAbs'])
164                             description = {'description': {'abstract': description_value}}
165                             metadata.append(description)
166                         if 'idPurp' in xml_dict['metadata']['dataIdInfo']:
167                             title_value = clean_text(dataIdInfo_dict['idPurp'])
168                             title = {'title': {'value': title_value}}
169                             metadata.append(title)
170                         if 'searchKeys' in dataIdInfo_dict:
171                             searchKeys_dict = dataIdInfo_dict['searchKeys']
172                             if 'keyword' in searchKeys_dict:
173                                 keyword_list = []
174                                 if type(searchKeys_dict["keyword"]) is list:
175                                     keyword_list += searchKeys_dict["keyword"]
176                                 else:
177                                     keyword_list.append(searchKeys_dict["keyword"])
178                                 for k in keyword_list:
179                                     metadata.append({'subject': {'value': k}})
180 
181     except Exception:
182         # Catch any exception silently and return an empty list
183         # Due to the variant format of ESRI Shapefile Metadata XML
184         # among different ArcGIS versions, an empty list will be returned
185         # if any exception occurs
186         metadata = []
187     finally:
188         return metadata
189 
190 
191 def clean_text(text):
192     #  Decode html
193 
194     h = HTMLParser()
195     return h.unescape(clean_html(text))
196 
197 
198 def clean_html(raw_html):
199     # Remove html tag from raw_html
200 
201     cleanr = re.compile('<.*?>')
202     cleantext = re.sub(cleanr, '', raw_html)
203     return cleantext
204
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/hs_geographic_feature_resource/parse_lib.py b/hs_geographic_feature_resource/parse_lib.py
--- a/hs_geographic_feature_resource/parse_lib.py
+++ b/hs_geographic_feature_resource/parse_lib.py
@@ -9,6 +9,9 @@
     #  Python 3
     from html.parser import HTMLParser
 
+from hs_core.models import Title
+
+
 UNKNOWN_STR = "unknown"
 
 
@@ -156,27 +159,36 @@
         if os.path.isfile(shp_xml_full_path):
             with open(shp_xml_full_path) as fd:
                 xml_dict = xmltodict.parse(fd.read())
-                if 'metadata' in xml_dict:
-                    if 'dataIdInfo' in xml_dict['metadata']:
-                        dataIdInfo_dict = xml_dict['metadata']['dataIdInfo']
-                        if 'idAbs' in dataIdInfo_dict:
-                            description_value = clean_text(dataIdInfo_dict['idAbs'])
-                            description = {'description': {'abstract': description_value}}
-                            metadata.append(description)
-                        if 'idPurp' in xml_dict['metadata']['dataIdInfo']:
-                            title_value = clean_text(dataIdInfo_dict['idPurp'])
-                            title = {'title': {'value': title_value}}
-                            metadata.append(title)
-                        if 'searchKeys' in dataIdInfo_dict:
-                            searchKeys_dict = dataIdInfo_dict['searchKeys']
-                            if 'keyword' in searchKeys_dict:
-                                keyword_list = []
-                                if type(searchKeys_dict["keyword"]) is list:
-                                    keyword_list += searchKeys_dict["keyword"]
-                                else:
-                                    keyword_list.append(searchKeys_dict["keyword"])
-                                for k in keyword_list:
-                                    metadata.append({'subject': {'value': k}})
+
+                dataIdInfo_dict = xml_dict['metadata']['dataIdInfo']
+                if 'idCitation' in dataIdInfo_dict:
+                    if 'resTitle' in dataIdInfo_dict['idCitation']:
+                        if '#text' in dataIdInfo_dict['idCitation']['resTitle']:
+                            title_value = dataIdInfo_dict['idCitation']['resTitle']['#text']
+                        else:
+                            title_value = dataIdInfo_dict['idCitation']['resTitle']
+
+                        title_max_length = Title._meta.get_field('value').max_length
+                        if len(title_value) > title_max_length:
+                            title_value = title_value[:title_max_length-1]
+                        title = {'title': {'value': title_value}}
+                        metadata.append(title)
+
+                if 'idAbs' in dataIdInfo_dict:
+                    description_value = clean_text(dataIdInfo_dict['idAbs'])
+                    description = {'description': {'abstract': description_value}}
+                    metadata.append(description)
+
+                if 'searchKeys' in dataIdInfo_dict:
+                    searchKeys_dict = dataIdInfo_dict['searchKeys']
+                    if 'keyword' in searchKeys_dict:
+                        keyword_list = []
+                        if type(searchKeys_dict["keyword"]) is list:
+                            keyword_list += searchKeys_dict["keyword"]
+                        else:
+                            keyword_list.append(searchKeys_dict["keyword"])
+                        for k in keyword_list:
+                            metadata.append({'subject': {'value': k}})
 
     except Exception:
         # Catch any exception silently and return an empty list

{"golden_diff": "diff --git a/hs_geographic_feature_resource/parse_lib.py b/hs_geographic_feature_resource/parse_lib.py\n--- a/hs_geographic_feature_resource/parse_lib.py\n+++ b/hs_geographic_feature_resource/parse_lib.py\n@@ -9,6 +9,9 @@\n     #  Python 3\n     from html.parser import HTMLParser\n \n+from hs_core.models import Title\n+\n+\n UNKNOWN_STR = \"unknown\"\n \n \n@@ -156,27 +159,36 @@\n         if os.path.isfile(shp_xml_full_path):\n             with open(shp_xml_full_path) as fd:\n                 xml_dict = xmltodict.parse(fd.read())\n-                if 'metadata' in xml_dict:\n-                    if 'dataIdInfo' in xml_dict['metadata']:\n-                        dataIdInfo_dict = xml_dict['metadata']['dataIdInfo']\n-                        if 'idAbs' in dataIdInfo_dict:\n-                            description_value = clean_text(dataIdInfo_dict['idAbs'])\n-                            description = {'description': {'abstract': description_value}}\n-                            metadata.append(description)\n-                        if 'idPurp' in xml_dict['metadata']['dataIdInfo']:\n-                            title_value = clean_text(dataIdInfo_dict['idPurp'])\n-                            title = {'title': {'value': title_value}}\n-                            metadata.append(title)\n-                        if 'searchKeys' in dataIdInfo_dict:\n-                            searchKeys_dict = dataIdInfo_dict['searchKeys']\n-                            if 'keyword' in searchKeys_dict:\n-                                keyword_list = []\n-                                if type(searchKeys_dict[\"keyword\"]) is list:\n-                                    keyword_list += searchKeys_dict[\"keyword\"]\n-                                else:\n-                                    keyword_list.append(searchKeys_dict[\"keyword\"])\n-                                for k in keyword_list:\n-                                    metadata.append({'subject': {'value': k}})\n+\n+                dataIdInfo_dict = xml_dict['metadata']['dataIdInfo']\n+                if 'idCitation' in dataIdInfo_dict:\n+                    if 'resTitle' in dataIdInfo_dict['idCitation']:\n+                        if '#text' in dataIdInfo_dict['idCitation']['resTitle']:\n+                            title_value = dataIdInfo_dict['idCitation']['resTitle']['#text']\n+                        else:\n+                            title_value = dataIdInfo_dict['idCitation']['resTitle']\n+\n+                        title_max_length = Title._meta.get_field('value').max_length\n+                        if len(title_value) > title_max_length:\n+                            title_value = title_value[:title_max_length-1]\n+                        title = {'title': {'value': title_value}}\n+                        metadata.append(title)\n+\n+                if 'idAbs' in dataIdInfo_dict:\n+                    description_value = clean_text(dataIdInfo_dict['idAbs'])\n+                    description = {'description': {'abstract': description_value}}\n+                    metadata.append(description)\n+\n+                if 'searchKeys' in dataIdInfo_dict:\n+                    searchKeys_dict = dataIdInfo_dict['searchKeys']\n+                    if 'keyword' in searchKeys_dict:\n+                        keyword_list = []\n+                        if type(searchKeys_dict[\"keyword\"]) is list:\n+                            keyword_list += searchKeys_dict[\"keyword\"]\n+                        else:\n+                            keyword_list.append(searchKeys_dict[\"keyword\"])\n+                        for k in keyword_list:\n+                            metadata.append({'subject': {'value': k}})\n \n     except Exception:\n         # Catch any exception silently and return an empty list\n", "issue": "Failed to create geographic feature resource\nThe files are from this resource https://www.hydroshare.org/resource/822fe5a7a83a49668c77a85179c3a7ee/. \r\n\r\nThe error is shown as below. Thanks for looking into this issue \r\n![v1 ao3 o6x3ete23yl1nw](https://cloud.githubusercontent.com/assets/5016511/20231562/19a9d520-a820-11e6-9373-81cf529504b8.png)\r\n\n", "before_files": [{"content": "import os\nimport xmltodict\nimport re\nfrom osgeo import ogr, osr\ntry:\n    #  Python 2.6-2.7\n    from HTMLParser import HTMLParser\nexcept ImportError:\n    #  Python 3\n    from html.parser import HTMLParser\n\nUNKNOWN_STR = \"unknown\"\n\n\ndef parse_shp(file_path):\n    # output dictionary format\n    # shp_metadata_dict[\"origin_projection_string\"]: original projection string\n    # shp_metadata_dict[\"origin_projection_name\"]: origin_projection_name\n    # shp_metadata_dict[\"origin_datum\"]: origin_datum\n    # shp_metadata_dict[\"origin_unit\"]: origin_unit\n    # shp_metadata_dict[\"field_meta_dict\"][\"field_list\"]: list [fieldname1, fieldname2...]\n    # shp_metadata_dict[\"field_meta_dict\"][\"field_attr_dic\"]:\n    #   dict {\"fieldname\": dict {\n    #                         \"fieldName\":fieldName,\n    #                         \"fieldTypeCode\":fieldTypeCode,\n    #                         \"fieldType\":fieldType,\n    #                         \"fieldWidth:fieldWidth,\n    #                         \"fieldPrecision:fieldPrecision\"\n    #                          }\n    #         }\n    # shp_metadata_dict[\"feature_count\"]: feature count\n    # shp_metadata_dict[\"geometry_type\"]: geometry_type\n    # shp_metadata_dict[\"origin_extent_dict\"]:\n    # dict{\"west\": east, \"north\":north, \"east\":east, \"south\":south}\n    # shp_metadata_dict[\"wgs84_extent_dict\"]:\n    # dict{\"west\": east, \"north\":north, \"east\":east, \"south\":south}\n\n    shp_metadata_dict = {}\n    # read shapefile\n    driver = ogr.GetDriverByName('ESRI Shapefile')\n    dataset = driver.Open(file_path)\n\n    # get layer\n    layer = dataset.GetLayer()\n    # get spatialRef from layer\n    spatialRef_from_layer = layer.GetSpatialRef()\n\n    if spatialRef_from_layer is not None:\n        shp_metadata_dict[\"origin_projection_string\"] = str(spatialRef_from_layer)\n        prj_name = spatialRef_from_layer.GetAttrValue('projcs')\n        if prj_name is None:\n            prj_name = spatialRef_from_layer.GetAttrValue('geogcs')\n        shp_metadata_dict[\"origin_projection_name\"] = prj_name\n\n        shp_metadata_dict[\"origin_datum\"] = spatialRef_from_layer.GetAttrValue('datum')\n        shp_metadata_dict[\"origin_unit\"] = spatialRef_from_layer.GetAttrValue('unit')\n    else:\n        shp_metadata_dict[\"origin_projection_string\"] = UNKNOWN_STR\n        shp_metadata_dict[\"origin_projection_name\"] = UNKNOWN_STR\n        shp_metadata_dict[\"origin_datum\"] = UNKNOWN_STR\n        shp_metadata_dict[\"origin_unit\"] = UNKNOWN_STR\n\n    field_list = []\n    filed_attr_dic = {}\n    field_meta_dict = {\"field_list\": field_list, \"field_attr_dict\": filed_attr_dic}\n    shp_metadata_dict[\"field_meta_dict\"] = field_meta_dict\n    # get Attributes\n    layerDefinition = layer.GetLayerDefn()\n    for i in range(layerDefinition.GetFieldCount()):\n        fieldName = layerDefinition.GetFieldDefn(i).GetName()\n        field_list.append(fieldName)\n        attr_dict = {}\n        field_meta_dict[\"field_attr_dict\"][fieldName] = attr_dict\n\n        attr_dict[\"fieldName\"] = fieldName\n        fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType()\n        attr_dict[\"fieldTypeCode\"] = fieldTypeCode\n        fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode)\n        attr_dict[\"fieldType\"] = fieldType\n        fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth()\n        attr_dict[\"fieldWidth\"] = fieldWidth\n        fieldPrecision = layerDefinition.GetFieldDefn(i).GetPrecision()\n        attr_dict[\"fieldPrecision\"] = fieldPrecision\n\n    # get layer extent\n    layer_extent = layer.GetExtent()\n\n    # get feature count\n    featureCount = layer.GetFeatureCount()\n    shp_metadata_dict[\"feature_count\"] = featureCount\n\n    # get a feature from layer\n    feature = layer.GetNextFeature()\n\n    # get geometry from feature\n    geom = feature.GetGeometryRef()\n\n    # get geometry name\n    shp_metadata_dict[\"geometry_type\"] = geom.GetGeometryName()\n\n    # reproject layer extent\n    # source SpatialReference\n    source = spatialRef_from_layer\n    # target SpatialReference\n    target = osr.SpatialReference()\n    target.ImportFromEPSG(4326)\n\n    # create two key points from layer extent\n    left_upper_point = ogr.Geometry(ogr.wkbPoint)\n    left_upper_point.AddPoint(layer_extent[0], layer_extent[3])  # left-upper\n    right_lower_point = ogr.Geometry(ogr.wkbPoint)\n    right_lower_point.AddPoint(layer_extent[1], layer_extent[2])  # right-lower\n\n    # source map always has extent, even projection is unknown\n    shp_metadata_dict[\"origin_extent_dict\"] = {}\n    shp_metadata_dict[\"origin_extent_dict\"][\"westlimit\"] = layer_extent[0]\n    shp_metadata_dict[\"origin_extent_dict\"][\"northlimit\"] = layer_extent[3]\n    shp_metadata_dict[\"origin_extent_dict\"][\"eastlimit\"] = layer_extent[1]\n    shp_metadata_dict[\"origin_extent_dict\"][\"southlimit\"] = layer_extent[2]\n\n    # reproject to WGS84\n    shp_metadata_dict[\"wgs84_extent_dict\"] = {}\n\n    if source is not None:\n        # define CoordinateTransformation obj\n        transform = osr.CoordinateTransformation(source, target)\n        # project two key points\n        left_upper_point.Transform(transform)\n        right_lower_point.Transform(transform)\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"westlimit\"] = left_upper_point.GetX()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"northlimit\"] = left_upper_point.GetY()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"eastlimit\"] = right_lower_point.GetX()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"southlimit\"] = right_lower_point.GetY()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"projection\"] = \"WGS 84 EPSG:4326\"\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"units\"] = \"Decimal degrees\"\n    else:\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"westlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"northlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"eastlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"southlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"projection\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"units\"] = UNKNOWN_STR\n\n    return shp_metadata_dict\n\n\ndef parse_shp_xml(shp_xml_full_path):\n    \"\"\"\n    Parse ArcGIS 10.X ESRI Shapefile Metadata XML.\n    :param shp_xml_full_path: Expected fullpath to the .shp.xml file\n    :return: a list of metadata dict\n    \"\"\"\n    metadata = []\n\n    try:\n        if os.path.isfile(shp_xml_full_path):\n            with open(shp_xml_full_path) as fd:\n                xml_dict = xmltodict.parse(fd.read())\n                if 'metadata' in xml_dict:\n                    if 'dataIdInfo' in xml_dict['metadata']:\n                        dataIdInfo_dict = xml_dict['metadata']['dataIdInfo']\n                        if 'idAbs' in dataIdInfo_dict:\n                            description_value = clean_text(dataIdInfo_dict['idAbs'])\n                            description = {'description': {'abstract': description_value}}\n                            metadata.append(description)\n                        if 'idPurp' in xml_dict['metadata']['dataIdInfo']:\n                            title_value = clean_text(dataIdInfo_dict['idPurp'])\n                            title = {'title': {'value': title_value}}\n                            metadata.append(title)\n                        if 'searchKeys' in dataIdInfo_dict:\n                            searchKeys_dict = dataIdInfo_dict['searchKeys']\n                            if 'keyword' in searchKeys_dict:\n                                keyword_list = []\n                                if type(searchKeys_dict[\"keyword\"]) is list:\n                                    keyword_list += searchKeys_dict[\"keyword\"]\n                                else:\n                                    keyword_list.append(searchKeys_dict[\"keyword\"])\n                                for k in keyword_list:\n                                    metadata.append({'subject': {'value': k}})\n\n    except Exception:\n        # Catch any exception silently and return an empty list\n        # Due to the variant format of ESRI Shapefile Metadata XML\n        # among different ArcGIS versions, an empty list will be returned\n        # if any exception occurs\n        metadata = []\n    finally:\n        return metadata\n\n\ndef clean_text(text):\n    #  Decode html\n\n    h = HTMLParser()\n    return h.unescape(clean_html(text))\n\n\ndef clean_html(raw_html):\n    # Remove html tag from raw_html\n\n    cleanr = re.compile('<.*?>')\n    cleantext = re.sub(cleanr, '', raw_html)\n    return cleantext\n", "path": "hs_geographic_feature_resource/parse_lib.py"}], "after_files": [{"content": "import os\nimport xmltodict\nimport re\nfrom osgeo import ogr, osr\ntry:\n    #  Python 2.6-2.7\n    from HTMLParser import HTMLParser\nexcept ImportError:\n    #  Python 3\n    from html.parser import HTMLParser\n\nfrom hs_core.models import Title\n\n\nUNKNOWN_STR = \"unknown\"\n\n\ndef parse_shp(file_path):\n    # output dictionary format\n    # shp_metadata_dict[\"origin_projection_string\"]: original projection string\n    # shp_metadata_dict[\"origin_projection_name\"]: origin_projection_name\n    # shp_metadata_dict[\"origin_datum\"]: origin_datum\n    # shp_metadata_dict[\"origin_unit\"]: origin_unit\n    # shp_metadata_dict[\"field_meta_dict\"][\"field_list\"]: list [fieldname1, fieldname2...]\n    # shp_metadata_dict[\"field_meta_dict\"][\"field_attr_dic\"]:\n    #   dict {\"fieldname\": dict {\n    #                         \"fieldName\":fieldName,\n    #                         \"fieldTypeCode\":fieldTypeCode,\n    #                         \"fieldType\":fieldType,\n    #                         \"fieldWidth:fieldWidth,\n    #                         \"fieldPrecision:fieldPrecision\"\n    #                          }\n    #         }\n    # shp_metadata_dict[\"feature_count\"]: feature count\n    # shp_metadata_dict[\"geometry_type\"]: geometry_type\n    # shp_metadata_dict[\"origin_extent_dict\"]:\n    # dict{\"west\": east, \"north\":north, \"east\":east, \"south\":south}\n    # shp_metadata_dict[\"wgs84_extent_dict\"]:\n    # dict{\"west\": east, \"north\":north, \"east\":east, \"south\":south}\n\n    shp_metadata_dict = {}\n    # read shapefile\n    driver = ogr.GetDriverByName('ESRI Shapefile')\n    dataset = driver.Open(file_path)\n\n    # get layer\n    layer = dataset.GetLayer()\n    # get spatialRef from layer\n    spatialRef_from_layer = layer.GetSpatialRef()\n\n    if spatialRef_from_layer is not None:\n        shp_metadata_dict[\"origin_projection_string\"] = str(spatialRef_from_layer)\n        prj_name = spatialRef_from_layer.GetAttrValue('projcs')\n        if prj_name is None:\n            prj_name = spatialRef_from_layer.GetAttrValue('geogcs')\n        shp_metadata_dict[\"origin_projection_name\"] = prj_name\n\n        shp_metadata_dict[\"origin_datum\"] = spatialRef_from_layer.GetAttrValue('datum')\n        shp_metadata_dict[\"origin_unit\"] = spatialRef_from_layer.GetAttrValue('unit')\n    else:\n        shp_metadata_dict[\"origin_projection_string\"] = UNKNOWN_STR\n        shp_metadata_dict[\"origin_projection_name\"] = UNKNOWN_STR\n        shp_metadata_dict[\"origin_datum\"] = UNKNOWN_STR\n        shp_metadata_dict[\"origin_unit\"] = UNKNOWN_STR\n\n    field_list = []\n    filed_attr_dic = {}\n    field_meta_dict = {\"field_list\": field_list, \"field_attr_dict\": filed_attr_dic}\n    shp_metadata_dict[\"field_meta_dict\"] = field_meta_dict\n    # get Attributes\n    layerDefinition = layer.GetLayerDefn()\n    for i in range(layerDefinition.GetFieldCount()):\n        fieldName = layerDefinition.GetFieldDefn(i).GetName()\n        field_list.append(fieldName)\n        attr_dict = {}\n        field_meta_dict[\"field_attr_dict\"][fieldName] = attr_dict\n\n        attr_dict[\"fieldName\"] = fieldName\n        fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType()\n        attr_dict[\"fieldTypeCode\"] = fieldTypeCode\n        fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode)\n        attr_dict[\"fieldType\"] = fieldType\n        fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth()\n        attr_dict[\"fieldWidth\"] = fieldWidth\n        fieldPrecision = layerDefinition.GetFieldDefn(i).GetPrecision()\n        attr_dict[\"fieldPrecision\"] = fieldPrecision\n\n    # get layer extent\n    layer_extent = layer.GetExtent()\n\n    # get feature count\n    featureCount = layer.GetFeatureCount()\n    shp_metadata_dict[\"feature_count\"] = featureCount\n\n    # get a feature from layer\n    feature = layer.GetNextFeature()\n\n    # get geometry from feature\n    geom = feature.GetGeometryRef()\n\n    # get geometry name\n    shp_metadata_dict[\"geometry_type\"] = geom.GetGeometryName()\n\n    # reproject layer extent\n    # source SpatialReference\n    source = spatialRef_from_layer\n    # target SpatialReference\n    target = osr.SpatialReference()\n    target.ImportFromEPSG(4326)\n\n    # create two key points from layer extent\n    left_upper_point = ogr.Geometry(ogr.wkbPoint)\n    left_upper_point.AddPoint(layer_extent[0], layer_extent[3])  # left-upper\n    right_lower_point = ogr.Geometry(ogr.wkbPoint)\n    right_lower_point.AddPoint(layer_extent[1], layer_extent[2])  # right-lower\n\n    # source map always has extent, even projection is unknown\n    shp_metadata_dict[\"origin_extent_dict\"] = {}\n    shp_metadata_dict[\"origin_extent_dict\"][\"westlimit\"] = layer_extent[0]\n    shp_metadata_dict[\"origin_extent_dict\"][\"northlimit\"] = layer_extent[3]\n    shp_metadata_dict[\"origin_extent_dict\"][\"eastlimit\"] = layer_extent[1]\n    shp_metadata_dict[\"origin_extent_dict\"][\"southlimit\"] = layer_extent[2]\n\n    # reproject to WGS84\n    shp_metadata_dict[\"wgs84_extent_dict\"] = {}\n\n    if source is not None:\n        # define CoordinateTransformation obj\n        transform = osr.CoordinateTransformation(source, target)\n        # project two key points\n        left_upper_point.Transform(transform)\n        right_lower_point.Transform(transform)\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"westlimit\"] = left_upper_point.GetX()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"northlimit\"] = left_upper_point.GetY()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"eastlimit\"] = right_lower_point.GetX()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"southlimit\"] = right_lower_point.GetY()\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"projection\"] = \"WGS 84 EPSG:4326\"\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"units\"] = \"Decimal degrees\"\n    else:\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"westlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"northlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"eastlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"southlimit\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"projection\"] = UNKNOWN_STR\n        shp_metadata_dict[\"wgs84_extent_dict\"][\"units\"] = UNKNOWN_STR\n\n    return shp_metadata_dict\n\n\ndef parse_shp_xml(shp_xml_full_path):\n    \"\"\"\n    Parse ArcGIS 10.X ESRI Shapefile Metadata XML.\n    :param shp_xml_full_path: Expected fullpath to the .shp.xml file\n    :return: a list of metadata dict\n    \"\"\"\n    metadata = []\n\n    try:\n        if os.path.isfile(shp_xml_full_path):\n            with open(shp_xml_full_path) as fd:\n                xml_dict = xmltodict.parse(fd.read())\n\n                dataIdInfo_dict = xml_dict['metadata']['dataIdInfo']\n                if 'idCitation' in dataIdInfo_dict:\n                    if 'resTitle' in dataIdInfo_dict['idCitation']:\n                        if '#text' in dataIdInfo_dict['idCitation']['resTitle']:\n                            title_value = dataIdInfo_dict['idCitation']['resTitle']['#text']\n                        else:\n                            title_value = dataIdInfo_dict['idCitation']['resTitle']\n\n                        title_max_length = Title._meta.get_field('value').max_length\n                        if len(title_value) > title_max_length:\n                            title_value = title_value[:title_max_length-1]\n                        title = {'title': {'value': title_value}}\n                        metadata.append(title)\n\n                if 'idAbs' in dataIdInfo_dict:\n                    description_value = clean_text(dataIdInfo_dict['idAbs'])\n                    description = {'description': {'abstract': description_value}}\n                    metadata.append(description)\n\n                if 'searchKeys' in dataIdInfo_dict:\n                    searchKeys_dict = dataIdInfo_dict['searchKeys']\n                    if 'keyword' in searchKeys_dict:\n                        keyword_list = []\n                        if type(searchKeys_dict[\"keyword\"]) is list:\n                            keyword_list += searchKeys_dict[\"keyword\"]\n                        else:\n                            keyword_list.append(searchKeys_dict[\"keyword\"])\n                        for k in keyword_list:\n                            metadata.append({'subject': {'value': k}})\n\n    except Exception:\n        # Catch any exception silently and return an empty list\n        # Due to the variant format of ESRI Shapefile Metadata XML\n        # among different ArcGIS versions, an empty list will be returned\n        # if any exception occurs\n        metadata = []\n    finally:\n        return metadata\n\n\ndef clean_text(text):\n    #  Decode html\n\n    h = HTMLParser()\n    return h.unescape(clean_html(text))\n\n\ndef clean_html(raw_html):\n    # Remove html tag from raw_html\n\n    cleanr = re.compile('<.*?>')\n    cleantext = re.sub(cleanr, '', raw_html)\n    return cleantext\n", "path": "hs_geographic_feature_resource/parse_lib.py"}]}