o al@sdZddlZddlmZmZddlmZddlm Z ddl m Z m Z m Z mZmZmZddlmZmZmZmZmZmZmZddlmZmZdd lmZmZmZm Z dd l!m"Z"Gd d d e#Z$Gd dde$Z%Gddde$Z&Gddde$Z'Gddde$Z(GdddZ)dS)a The LayerMapping class provides a way to map the contents of OGR vector files (e.g. SHP files) to Geographic-enabled Django models. For more information, please consult the GeoDjango documentation: https://docs.djangoproject.com/en/dev/ref/contrib/gis/layermapping/ N)DecimalInvalidOperation)Path) GeometryField)CoordTransform DataSource GDALException OGRGeometry OGRGeomTypeSpatialReference)OFTDate OFTDateTime OFTInteger OFTInteger64OFTReal OFTStringOFTTime)FieldDoesNotExistObjectDoesNotExist) connectionsmodelsrouter transaction) force_strc@ eZdZdS) LayerMapErrorN__name__ __module__ __qualname__r r G/usr/lib/python3/dist-packages/django/contrib/gis/utils/layermapping.pyrrc@r) InvalidStringNrr r r r!r# r"r#c@r)InvalidDecimalNrr r r r!r$$r"r$c@r)InvalidIntegerNrr r r r!r%(r"r%c@r)MissingForeignKeyNrr r r r!r&,r"r&c @seZdZdZdeddeddededjed ed jed ed jed iZieje ej e ej e ej e eefeje eefeje efejeejeejeejeeje efejeejeejeejeejeeje eefej e eefej!e eefej"e eefej#e eefiZ$    d0ddZ%ddZ&ddZ'ddZ(ddZ)ddZ*dd Z+d!d"Z,d#d$Z-d%d&Z.d'd(Z/d)d*Z0d+d,Z1d-d-d-d-d-e2j3d-fd.d/Z4dS)1 LayerMappingz1A class that maps OGR Layers to GeoDjango Models. MultiPointMultiLineString MultiPolygonPoint25D MultiPoint25D LineString25DMultiLineString25D Polygon25DMultiPolygon25DrNutf-8commit_on_successTc Cs(t|ttfrt||d|_n||_|j||_| dur| nt||_t |jj |_ ||_ ||_ |t |jjjrB||_nd}|rR|||_||_n||_|reddlm} | |||_nd|_| ru|| d}| |_nd|_||_|dkrd|_dS|dkrtj|_dSt d|) a A LayerMapping object is initialized using the given Model (not an instance), a DataSource (or string path to an OGR-supported data file), and a mapping dictionary. See the module level docstring for more details and keyword argument usage. )encodingNFr)lookup autocommitr5z!Unrecognized transaction mode: %s)! isinstancestrrrdslayerr db_for_writeusingropsspatial_backendmappingmodel check_layerfeaturessupports_transformgeometry_field geo_field check_srs source_srscoord_transform transformcodecsr7r6 check_uniqueuniquetransaction_modetransaction_decoratorratomicr) selfrBdatarAr<rIr6rOrKrNr>r7r r r!__init__Vs@          zLayerMapping.__init__cCs0|rt|ttfr t|St|tr|StdS)zCheck the `fid_range` keyword.N)r9tuplelistslice TypeError)rR fid_ranger r r!check_fid_ranges zLayerMapping.check_fid_rangec sd|_i|_|jj|jj}fdd}|jD]\}}z |jj|}Wn t y3t d|w|j j }t |tr|jrDt d|j}z|dkrSt|d}nt|}Wn tyet d|w|jj} | j|js|| |st d ||dkrd nd | f||_||_|} net |tjrt |tr|jj} |D]!\} } || }z| j| Wqt yt d | | j j fw| } n-td |j |jvrt d|||}||} t| |j|j st d| | j |f|} | |j|<qdS)z Check the Layer metadata and ensure that it's compatible with the mapping information and model. Unlike previous revisions, there is no need to increment through each feature in the Layer. Fcs,z|}W|Stytd|w)Nz4Given mapping OGR field "%s" not found in OGR Layer.)index ValueErrorr) ogr_map_fldidx ogr_fieldsr r! check_ogr_flds   z/LayerMapping.check_layer..check_ogr_fldz3Given mapping field "%s" not in given Model fields.zDLayerMapping does not support more than one GeometryField per model.r,25Dz'Invalid mapping for GeometryField "%s".zDInvalid mapping geometry; model has %s%s, layer geometry type is %s.z(dim=3)z/ForeignKey mapping field "%s" not in %s fields.z.ForeignKey mapping must be of dictionary type.z0Django field type "%s" has no OGR mapping (yet).z:OGR field "%s" (of type %s) cannot be mapped to Django %s.N) geom_fieldfieldsr< field_typesrAitemsrB_meta get_fieldrr __class__rr9rdimr r geom_typename startswith make_multi coord_dimr ForeignKeydict remote_fieldrX FIELD_TYPES issubclass)rRogr_field_typesra field_nameogr_name model_fieldfld_namerpgtypeltype fields_val rel_modelrel_name ogr_fieldr^r r_r!rCsr                zLayerMapping.check_layercCsXt|tr|}nt||jr|j}nt|ttfr t|}n|jj}|s*td|S)z>Check the compatibility of the given spatial reference object.z#No source reference system defined.) r9r r@spatial_ref_syssrsintr:r<r)rRrIsrr r r!rH s  zLayerMapping.check_srscCsNt|ttfr|D] }||jvrtq dSt|tr#||jvr!tdStd)zDCheck the `unique` keyword parameter -- may be a sequence or string.zBUnique keyword argument must be set with a tuple, list, or string.N)r9rVrUrAr\r:rX)rRrNattrr r r!rMs   zLayerMapping.check_uniquec Csi}|jD]>\}}|j|}t|tr*z ||j|}Wn"ty)tdwt|t j j r9| |||}n| |||}|||<q|S)z{ Given an OGR Feature, return a dictionary of keyword arguments for constructing the mapped model. z)Could not retrieve geometry from feature.)rArgrer9r verify_geomgeomrrrbase ModelBase verify_fkverify_ogr_field)rRfeatkwargsrwrxryvalr r r!feature_kwargs,s    zLayerMapping.feature_kwargscs0t|jtr|j|jiSfdd|jDS)z Given the feature keyword arguments (from `feature_kwargs`), construct and return the uniqueness keyword arguments -- a subset of the feature kwargs. csi|]}||qSr r ).0fldrr r! Vsz.LayerMapping.unique_kwargs..)r9rNr:)rRrr rr! unique_kwargsMs zLayerMapping.unique_kwargsc Cst|tr>t|tjtjfr>|jr|jdurt|j|j}n|j}|jr<|durgetrr&r)rRrr~ rel_mapping fk_kwargsrwrxr r r!rs  zLayerMapping.verify_fkcCs`|j|jkr |j|_||j|r"|j|jj}t|}||n|}|jr-||j|jS)z Verify the geometry -- construct and return a GeometryCollection if necessary (for example if the model field is MultiPolygonField while the mapped shapefile only contains Polygons). ) rprorl MULTI_TYPESnumr addrKwkt)rRrry multi_typegr r r!rs   zLayerMapping.verify_geomc CsX|j}z|j|jj|jjdj}t|j |WSt y+}zt d|d}~ww)z,Return the coordinate transformation object.)sridz?Could not translate between the data source and model geometry.N) r@rrr>rrGrrrrI Exceptionr)rR SpatialRefSys target_srsexcr r r!rJs zLayerMapping.coord_transformcCs|jj}||jS)zHReturn the GeometryField instance associated with the geographic column.)rBrhrird)rRoptsr r r!rFs zLayerMapping.geometry_fieldcCs|j|jvo|jjd|jkS)z Given the OGRGeomType for a geometry and its associated GeometryField, determine whether the geometry should be turned into a GeometryCollection. zMulti%s)rrrjrdjango)rRrlryr r r!ros zLayerMapping.make_multiFc s"|}rdusttsdn|ddffdd } jdur/| } jj} |rt|tr|| kr|rDtdd\} } } t|| |}t|}t |D]2\}}|d |krgt | d}nt | |}z | || | \} } |} WqWt y d d |fwdS| dS) a Save the contents from the OGR DataSource Layer into the database according to the mapping dictionary given at initialization. Keyword Parameters: verbose: If set, information will be printed subsequent to each model save executed on the database. fid_range: May be set with a slice or tuple of (begin, end) feature ID's to map from the data source. In other words, this keyword enables the user to selectively import a subset range of features in the geographic data source. step: If set with an integer, transactions will occur at every step interval. For example, if step=1000, a commit would occur after the 1,000th feature, the 2,000th feature etc. progress: When this keyword is set, status information will be printed giving the number of features processed and successfully saved. By default, progress information will pe printed every 1000 features processed, however, this default may be overridden by setting this keyword with an integer for the desired interval. stream: Status information will be written to this file handle. Defaults to using `sys.stdout`, but any object with a `write` method is supported. silent: By default, non-fatal error notifications are printed to stdout, but this keyword may be set to disable these notifications. strict: Execution of the model mapping will cease upon the first error encountered. The default behavior is to attempt to continue. Tirc s|rj|}nj}|D]}|d7}z|}Wn!ty;}zr%s1d|j|fWYd}~nd}~wwd}jrzE|}jj j j di|} d}t | j } | durit |j } n| j} t |j } | D]} | | qut| j | jWntyjdi|} Yn wjdi|} z| jj d|d7}rd|rdnd| fWn1ty}z%r؈s׈d |jd |sd ||fWYd}~nd}~wwr|d krd ||fq ||fS)Nr(z#Ignoring Feature ID %s because: %s FT)r>z%s: %s UpdatedSavedzOFailed to save the feature (id: %s) into the model with the keyword arguments: z%s z"Failed to save %s: %s Continuing rz$Processed %d features, saved %d ... r )r<rrwritefidrNrrBrr>rgetattrrdr ogrrsetattrrrsaver) feat_rangenum_feat num_saved layer_iterrrmsg is_updateu_kwargsm geom_valuernewrprogressprogress_intervalrRsilentstreamstrictverboser r!_savesp     z LayerMapping.save.._saveNzOThe `step` keyword may not be used in conjunction with the `fid_range` keyword.)rrrr(z%s Failed to save slice: %s z(=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-) rZr9rrPr<rrranger enumeraterWrr)rRrrYsteprrrr default_rangernfeatbegrrindicesn_iiend step_slicer rr!rs8 *  L        zLayerMapping.save)rNr4r5TNN)5rrr__doc__r rrr AutoFieldr BigAutoFieldrSmallAutoField BooleanFieldrrr FloatField DateFieldr DateTimeFieldr EmailField TimeFieldrrr SlugFieldrURLField UUIDFieldBigIntegerFieldSmallIntegerFieldPositiveBigIntegerFieldPositiveIntegerFieldPositiveSmallIntegerFieldrtrTrZrCrHrMrrrrrrJrFrosysstdoutrr r r r!r'0s               H a! 9 r')*rrdecimalrrrpathlibrdjango.contrib.gis.db.modelsrdjango.contrib.gis.gdalrrrr r r django.contrib.gis.gdal.fieldr r rrrrrdjango.core.exceptionsrr django.dbrrrrdjango.utils.encodingrrrr#r$r%r&r'r r r r!s    $