Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.89k stars 1.1k forks source link

Add Field length in to_featurelayer, or any sdf.spatial.to_ #1324

Open hildermesmedeiros opened 2 years ago

hildermesmedeiros commented 2 years ago

It would be greate to be possible to control string fields length using pandas dataframe and to_featurelayer or to featureCollection With methods like numpy astype('<u1000') works, but, if I'm not wrong, in pandas it will be string or str.

import pandas as pd
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.features import GeoAccessor

WGS84_WKID = 4326
EMPTY_POINT = Geometry({'x': .0, 'y': .0, 'spatialReference': {'wkid': WGS84_WKID}})
df = pd.DataFrame({'text_col': ['short text']}, 'SHAPE':[EMPTY_POINT ])
sdf = GeoAccessor.from_df(df, geometry_column='SHAPE')
##sdf.mensagem_monitoramento = sdf.mensagem_monitoramento.astype('<U1000') <----pandas will not use it
gis = GIS('pro')

sdf.spatial.to_featurelayer(
    title='String_test',
    gis=gis ,
    tags='test',
    folder=None,
    sanitize_columns=False,
    service_name=None
)

Describe the solution you'd like A parameter to parse a list of tuples ('col_name', col_length)

hildermesmedeiros commented 2 years ago

Just to be clear, any one can automate it with arcpy, but is not that pratical.

import arcpy
arcpy.env.workspace = 'https://url_to/FeatureServer'
#assuming one feature on service (in id 0)
layer_name = arcpy.ListFeatureClasses()[0]

arcpy.AddField_management(
    in_table=layer_name,
    field_name='temp_col',
    field_type='TEXT',
    field_precision=None,
    field_scale=None,
    field_length=1000,
    field_alias=None,
    field_is_nullable=None,
    field_is_required=None,
    field_domain=None,
)

And from there we copy data from one colum to other, dele the correct one, use addfield again to recreate it, copy data from temp_col to the new text field with correct size, and finally - delete de temp column.

achapkowski commented 2 years ago

Interesting idea we will think about it and let you know.